SQL for Data Retrieval
Review Questions of Previous Class Q1. Show the sum of hours worked for project with ID 1200 (use ASSIGNMENT table) – Use “SUM” and “WHERE” slide #9 (3-SQL-4.pptx) Q2. Count how many distinct departments in project table – Use “COUNT” slide #9 (3-SQL-4.pptx) – Use “DISTINCT” IST2102
Review Questions of Previous Class Q3. Group projects by departments. Show department names and number of projects associated with each department – Use “group by”, slide #13 (3-SQL-4.pptx) Q4. Add a constraint to the question above. Only show the departments with number of projects more than 1 – Use ”group by” and “having”, slide #13 (3-SQL-4.pptx) IST2103
Retrieving Information from Multiple Tables Two approaches – Subqueries – Joins
Example 1: Querying Two Tables IST2105 Show the names of employees who worked less than 20 hours Q2. Get names of the employees with employee number 4, 5: Tom Caruthers, Heather Jones Q1. Check “worked less than 20 hours” in ASSIGNMENT table: employee number 4, 5 EMPLOYEE ASSIGNMENT
Example 1: Use Subquery IST2106 SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20; SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5); SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20); Show the names of employees who worked less than 20 hours
Exercise 1: Use Subquery IST2107 Show the names of employees who is assigned to ProjectID 1000 – First write two separate queries and then merge into one query 7 Step 2. Show the names of employees with the employee numbers from Q1 Q1. Find employee numbers who is assigned to ProjectID 1000 EMPLOYEE ASSIGNMENT
Example 1: Use Join IST2108 SELECTFirstName, LastName, HoursWorked FROMEMPLOYEE AS E, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDHoursWorked < 20; SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20; SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (4, 5); Show the names of employees who worked less than 20 hours SELECTFirstName, LastName FROMEMPLOYEE WHEREEmployeeNumber IN (SELECTEmployeeNumber FROMASSIGNMENT WHEREHoursWorked < 20); Show columns from multiple tables
Example 1: Use Join IST2109 SELECTFirstName, LastName, HoursWorked FROMEMPLOYEE AS E, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDHoursWorked < 20; Show the names of employees who worked less than 20 hours Shared column: EmployeeNumber
Exercise 1: Use Join Show the names of employees who is assigned to ProjectID 1000 IST21010 Shared column: EmployeeNumber
Exercise 2: Use Subquery Show the project names assigned to EmployeeNumber 4 IST21011 ASSIGNMENT PROJECT
Exercise 2: Use Join Show the project names assigned to EmployeeNumber 4 IST21012 Shared column: ProjectID
Example 2: Querying Three Tables Show the names of employees who are assigned with projects associated with Finance department IST21013 Names of employees Shared column: EmployeeNumber Associated with Finance department Shared column: ProjectID
Example 2: Use Subquery IST21014 Show the names of employees who are assigned with projects associated with Finance department ASSIGNMENT PROJECT Q1. Associated with Finance department: 1100, 1400 Q3. Employee names with number in {4,6,4,5,6} EMPLOYEE Q2. EmployeeNumber assigned to project 1100 or 1400: 4,6,4,5,6
Example 2: Use Subquery IST21015 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' ) ); Show the names of employees who are assigned with projects associated with Finance department SELECTProjectID FROMPROJECT WHEREDepartment = 'Finance';
Example 2: Use Join IST21016 SELECTDISTINCT FirstName, LastName FROMEMPLOYEE AS E, PROJECT AS P, ASSIGNMENT AS A WHEREE.EmployeeNumber = A.EmployeeNumber ANDP.ProjectID = A.ProjectID ANDP.Department = 'Finance'; Show the names of employees who are assigned with projects associated with Finance department Shared column: ProjectID Shared column: EmployeeNumber
Exercise 3: Use Subquery Show all the project names that are assigned to Ken – Write 3 separate queries first and then merge them into 1 IST21017 ASSIGNMENT PROJECT Q3. Names of the projects with ID in (1000, 1300) Q1. Employee numbers with first name as Ken: 10 EMPLOYEE Q2. ProjectID assigned to employee number 10: 1000, 1300
Exercise 3: Use Join Show all the project names that are assigned to Ken – Use join IST21018 Shared column: ProjectIDShared column: EmployeeNumber
More about Modifying Data Insert – Add a new row in a table Update – Update the data in a table that matches the specified criteria Delete – Delete the data in a table that matches the specified criteria
Changing Data Values: UPDATE To change the data values in an existing row (or set of rows) use the Update statement UPDATE EMPLOYEE SET Phone=' ' WHERE EmployeeNumber = 11; UPDATE PROJECT SET ProjectID = 2000 WHERE ProjectID = 1000 UPDATE DEPARTMENT SET DepartmentName = 'Public Relation' WHERE ProjectID = 'Marketing'
Deleting Data: DELETE To delete a row or set of rows from a table using the DELETE statement DELETE FROM EMPLOYEE WHERE EemplyeeNumber = 12; DELETE FROM EMPLOYEE WHERE EemplyeeNumber = 8;
Update/Delete Rules CASCADE – Affect the primary key as well as the foreign keys NO ACTION – Cannot be changed/deleted unless a record is NOT referred by any other table at all.