Presentation is loading. Please wait.

Presentation is loading. Please wait.

STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1.

Similar presentations


Presentation on theme: "STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1."— Presentation transcript:

1 STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1

2 Overview SQL-1: Create tables and insert data SQL-2: Query one single table SQL-3: Query multiple tables IST210 2

3 Previously SELECT * FROM PROJECT; SELECT DepartmentFROM EMPLOYEE; SELECT DISTINCT Department FROM EMPLOYEE; SELECT* FROMPROJECT WHERE Department = 'Finance’; SELECT* FROMPROJECT WHEREDepartment = 'Finance' OR MaxHours > 135; SELECT FirstName, LastName, Department FROM EMPLOYEE WHERE Department IN ('Finance', 'Accounting', 'Marketing'); WHERE LastName LIKE 'J%'; WHERE LastName LIKE 'J____'; ORDER BY LastName DESC, FirstName ASC; SELECT COUNT(*)FROM PROJECT; IST210 3

4 Running Example IST210 4

5 Review Questions of Previous Class Q1. Show all the data in DEPARTMENT table SELECT * FROM DEPARTMENT; Q2. Show the phone number of Accounting department (use DEPARTMENT table) SELECT Phone FROM DEPARTMENT WHERE DepartmentName = 'Accounting'; Q3. Show employees with firstname as ‘Mary’ and lastname starting with letter ‘A’ (use EMPLOYEE table) SELECT * FROM EMPLOYEE WHERE FirstName = 'Mary' and LastName LIKE 'A%'; IST210 5

6 Review Questions of Previous Class Q4. Count how many employees with first name ‘George’ (use EMPLOYEE table) SELECT COUNT(*) from EMPLOYEE WHERE FirstName = ‘George’; Q5. Show the sum of hours worked (HoursWorked) for project with ID 1200 (use ASSIGNMENT table) SELECT SUM(HoursWorked) from ASSIGNMENT WHERE ProjectID=1200; Q6. (challenging) Show the projects with MaxHours equal to minimum of MaxHours (use PROJECT table) Write two queries first: one shows the minimum MaxHours; another one shows the projects with maxHours=120 Then replace 120 with the subquery: see Exercise 4 from last class IST210 6

7 Review Questions of Previous Class Q4. Count how many employees with first name ‘George’ (use EMPLOYEE table) SELECT COUNT(*) FROM EMPLOYEE WHERE FirstName = ‘George’; Q5. Show the sum of hours worked (HoursWorked) for project with ID 1200 (use ASSIGNMENT table) SELECT SUM(HoursWorked) FROM ASSIGNMENT WHERE ProjectID=1200; Q6. (challenging) Show the projects with MaxHours equal to minimum of MaxHours (use PROJECT table) SELECT * FROM PROJECT WHERE MaxHours=(SELECT MIN(MaxHours) FROM PROJECT); Because build-in functions such as MIN, AVG, etc cannot be used in WHERE clause. IST210 7

8 Review Questions of Previous Class Q7. Group projects by departments (use PROJECT table). Show departments and number of projects associated with each department Use “group by”, slide #30 Q8. Add a constraint to the question above. Only show the departments with number of projects more than 1 Use ”group by” and “having”, slide #30 IST210 8

9 Review Questions of Previous Class What is wrong with queries on the left? Q9. Show employees with FirstName containing word “Ma” IST210 9 SELECT * FROM EMPLOYEE WHERE FirstName = '%Ma%' ; Q10. Show employees without phone numbers in database SELECT* FROMEMPLOYEE WHEREPhone = NULL; Q11. Count how many projects with MaxHours less than average MaxHours? SELECTCOUNT(*) FROMPROJECT WHEREMaxHours < AVG(MaxHours); SELECT * FROM EMPLOYEE WHERE FirstName LIKE '%Ma%' ; SELECT* FROMEMPLOYEE WHEREPhone IS NULL; SELECTCOUNT(*) FROMPROJECT WHEREMaxHours < (SELECT AVG(MaxHours) from PROJECT);

10 SQL for Data Retrieval: Providing Subtotals: GROUP BY How many employees each department has? Subtotals may be calculated by using the GROUP BY clause Show how many employees in each department SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department The GROUP BY keyword tells the DBMS to sort the table by the named column and then to apply the build-in function to groups of rows that have the same value for the named column. IST210 10

11 SQL for Data Retrieval: Providing Subtotals: GROUP BY Further restrict results by using the HAVING clause Only show those departments with more than one employee The HAVING clause may be used to restrict which data is displayed SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department HAVING COUNT(*) > 1; IST210 11

12 Exercise 5 Q1. Group assignments by employees, as each employee can be involved in multiple projects. Show employee numbers and total hours work IST210 12 SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber;

13 Exercise 5 Q1. Group assignments by employees. Show employee numbers and total hours work Q2. Add a constraint to Q1: the employees with total hours worked more than 100 IST210 13 SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber HAVING SUM(HoursWorked) > 100;

14 Exercise 5 Q1. Group assignments by employees. Show employee numbers and total hours work Q3. Add a constraint to Q1: only show the employees with employee number less than 5 Try to use two ways to answer this query IST210 14 SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber HAVING EmployeeNumber < 5; SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT WHERE EmployeeNumber < 5 GROUP BY EmployeeNumber;

15 Overview SQL-1: Create tables and insert data SQL-2: Query one single table SQL-3: Query multiple tables IST210 15

16 Content in today’s class Query: SELECT … FROM … WHERE Query across multiple tables Two approaches: (1) Subquery and (2) Join IST210 16

17 Question 1: Querying Two Tables The names of employees who worked less than 20 hours in any project IST210 17 2. Get names of the employees with employee number 4, 5: Tom Caruthers, Heather Jones 1. Check “worked less than 20 hours” in ASSIGNMENT table: employee number 4, 5 EMPLOYEE ASSIGNMENT

18 Question 1: Use Subquery The names of employees who worked less than 20 hours IST210 18 SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20; SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5); 1. Check “worked less than 20 hours” in ASSIGNMENT table 2. Get names of the employees with employee number 4, 5

19 Question 1: Use Subquery The names of employees who worked less than 20 hours IST210 19 SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20; SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5); SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20);

20 Exercise 1: Use Subquery The names of employees who is assigned to ProjectID 1000 First write two separate queries and then merge into one query IST210 20 Q2. Show the names of employees with the employee numbers from Q1 Q1. Find employee numbers who is assigned to projectID 1000 EMPLOYEE ASSIGNMENT

21 Exercise 1: Use Subquery The names of employees who is assigned to ProjectID 1000 First write two separate queries and then merge into one query IST210 21 Step 2: Show the names of employees with the employee numbers from Step 1 Answer to Step 1 is (1,8,10) Step 1: Find employee numbers who is assigned to projectID 1000 SELECT EmployeeNumber FROM ASSIGNMENT WHERE ProjectID = 1000; SELECT FirstName, Lastname FROM EMPLOYEE WHERE EmployeeNumber IN (1,8,10); Step 3: Merge them together SELECT FirstName, Lastname FROM EMPLOYEE WHERE EmployeeNumber IN (SELECT EmployeeNumber FROM ASSIGNMENT WHERE ProjectID = 1000);

22 Question 1: Use Join IST210 22 SELECTFirstName, LastName FROMEMPLOYEE AS E, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDA.HoursWorked < 20; SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20; SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5); The names of employees who worked less than 20 hours SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20); Subqueries are effective for processing multiple tables, as long as the results come from a single table.

23 Question 2: Use Join The names of employees who worked less than 20 hours IST210 23 SELECTFirstName, LastName, HoursWorked FROMEMPLOYEE AS E, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDA.HoursWorked < 20; Shared column: EmployeeNumber

24 Exercise 1: Use Join The names of employees who is assigned to ProjectID 1000 IST210 24 Shared column: EmployeeNumber SELECT FirstName, Lastname FROM EMPLOYEE AS E, ASSIGNMENT AS A WHERE E.EmployeeNumber = A.EmployeeNumber AND A.ProjectID=1000;

25 Exercise 2: Use Subquery The project names assigned to EmployeeNumber 4 IST210 25 ASSIGNMENT PROJECT /* Q1. ProjectIDs assigned to Employee Number 4 */ SELECT ProjectID FROM ASSIGNMENT WHERE EmployeeNumber = 4; /* Q2. Project names with project IDs from Q1 */ SELECT ProjectName FROM PROJECT WHERE ProjectID IN (1100, 1200, 1400); SELECT ProjectName FROM PROJECT WHERE ProjectID IN (SELECT ProjectID FROM ASSIGNMENT WHERE EmployeeNumber = 4);

26 Exercise 2: Use Join The project names assigned to EmployeeNumber 4 IST210 26 Shared column: ProjectID SELECT ProjectName FROM PROJECT AS P, ASSIGNMENT AS A WHERE P.ProjectID = A.ProjectID AND A.EmployeeNumber=4;

27 Question 2: Querying Three Tables The names of employees who are assigned with projects associated with Finance department IST210 27 Names of employees Shared column: EmployeeNumber Associated with Finance department Shared column: ProjectID

28 Question 2: Use Subquery IST210 28 The names of employees who are assigned with projects associated with Finance department ASSIGNMENT PROJECT 1. Associated with Finance department: 1100, 1400 3. Employee names with number in {4,6,4,5,6} EMPLOYEE 2. EmployeeNumber assigned to project 1100 or 1400: 4,6,4,5,6

29 Question 2: Use Subquery IST210 29 SELECTEmployeeNumber FROMASSIGNMENT WHEREProjectID IN (1100, 1400); SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5, 6); The names of employees who are assigned with projects associated with Finance department SELECTProjectID FROMPROJECT WHEREDepartment = 'Finance'; 1. Associated with Finance department: 1100, 1400 2. EmployeeNumber assigned to project 1100 or 1400: 4,6,4,5,6 3. Employee names with number in {4,6,4,5,6}

30 Question 2: Use Subquery IST210 30 SELECTEmployeeNumber FROMASSIGNMENT WHEREProjectID IN (1100, 1400); SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5, 6); SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTEmployeeNumber FROMASSIGNMENT WHEREProjectID IN (SELECTProjectID FROMPROJECT WHEREDepartment = 'Finance' ) ); The names of employees who are assigned with projects associated with Finance department SELECTProjectID FROMPROJECT WHEREDepartment = 'Finance';

31 Question 2: Use Join IST210 31 SELECTDISTINCT FirstName, LastName FROMEMPLOYEE AS E, PROJECT AS P, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDP.ProjectID = A.ProjectID ANDP.Department = 'Finance'; The names of employees who are assigned with projects associated with Finance department Shared column: ProjectID Shared column: EmployeeNumber

32 Exercise 3 Show all the project names that are assigned to Ken Write 3 separate queries first and then merge them into 1 IST210 32 ASSIGNMENT PROJECT 3. Names of the projects with ID in (1000, 1300) 1. Employee numbers with first name as Ken: 10 EMPLOYEE 2. ProjectID assigned to employee number 10: 1000, 1300

33 IST210 33 SELECT ProjectName FROM PROJECT WHERE ProjectID IN (SELECT ProjectID FROM ASSIGNMENT WHERE EmployeeNumber IN (SELECT EmployeeNumber FROM EMPLOYEE WHERE FirstName = 'Ken' ) );

34 Exercise 3: Use Join Show all the project names that are assigned to Ken Use join IST210 34 Shared column: ProjectID Shared column: EmployeeNumber SELECT ProjectName FROM EMPLOYEE AS E, PROJECT AS P, ASSIGNMENT AS A WHERE E.EmployeeNumber = A.EmployeeNumber AND P.ProjectID = A.ProjectID AND E.FirstName='Ken';

35 Summary SQL-1: Create tables and insert data SQL-2: Query one single table SQL-3: Query multiple tables IST210 35

36 Assignment 3-2 Assignment 3 is divided into two parts Part 2 (60 points) is on course website now, and is due in a week (after assignment 3-1, i.e. Sunday Feb 22) Prepare tables and data: Please download Assignment-3-2-SQL-scripts.zip from course website assign-3-create-tables.sql, assign-3-insert-data.sql, and assign-3-delete-tables.sql Delete the tables you have created in your database using assign-3-delete- tables.sql Create tables using assign-3-create-tables.sql Insert data using assign-3-insert-data.sql You should be able to successfully execute your query in Microsoft SQL server for each question in part 2 How to access SQL? Come and work in the classroom Use remote desktop (if working outside of IST) https://www.up.ist.psu.edu/vlabs/ Requires SQL programming, so start early! IST210 36

37 Wednesday Class Exercise Class Assignment P1 Assignment P2 Assignment 3-1 Assignment 3-2 IST210 37


Download ppt "STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1."

Similar presentations


Ads by Google