sql for data retrieval. review questions of previous class q1. show the sum of hours worked for...

22
SQL for Data Retrieval

Upload: prudence-booth

Post on 04-Jan-2016

247 views

Category:

Documents


12 download

TRANSCRIPT

Page 1: 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”

SQL for Data Retrieval

Page 2: 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”

IST210 2

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”

Page 3: 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”

IST210 3

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)

Page 4: 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”

Retrieving Information from Multiple Tables

• Two approaches– Subqueries– Joins

Page 5: 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”

IST210 5

Example 1: Querying Two Tables• 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

Page 6: 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”

IST210 6

Example 1: Use Subquery

SELECT EmployeeNumberFROM ASSIGNMENTWHERE HoursWorked < 20;

SELECT FirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN (4, 5);

SELECT FirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN

(SELECT EmployeeNumber FROM ASSIGNMENT WHERE HoursWorked < 20);

• Show the names of employees who worked less than 20 hours

Page 7: 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”

IST210 7

Exercise 1: Use Subquery• Show the names of employees who is assigned to ProjectID 1000

– First write two separate queries and then merge into one query

7Step 2. Show the names of employees with the employee numbers from Q1

Q1. Find employee numbers who is assigned to ProjectID 1000

EMPLOYEE

ASSIGNMENT

Page 8: 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”

IST210 8

Example 1: Use Join

SELECT FirstName, LastName, HoursWorkedFROM EMPLOYEE AS E, ASSIGNMENT AS AWHERE E.EmployeeNumber = A.EmployeeNumber

AND HoursWorked < 20;

SELECT EmployeeNumberFROM ASSIGNMENTWHERE HoursWorked < 20;

SELECT FirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN (4, 5);

• Show the names of employees who worked less than 20 hours

SELECT FirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN

(SELECT EmployeeNumber FROM ASSIGNMENT WHERE HoursWorked < 20);

Show columns from multiple tables

Page 9: 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”

IST210 9

Example 1: Use Join

SELECT FirstName, LastName, HoursWorkedFROM EMPLOYEE AS E, ASSIGNMENT AS AWHERE E.EmployeeNumber = A.EmployeeNumber

AND HoursWorked < 20;

• Show the names of employees who worked less than 20 hours

Shared column: EmployeeNumber

Page 10: 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”

IST210 10

Exercise 1: Use Join

• Show the names of employees who is assigned to ProjectID 1000

Shared column: EmployeeNumber

Page 11: 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”

IST210 11

Exercise 2: Use Subquery

• Show the project names assigned to EmployeeNumber 4

ASSIGNMENT

PROJECT

Page 12: 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”

IST210 12

Exercise 2: Use Join

• Show the project names assigned to EmployeeNumber 4

Shared column: ProjectID

Page 13: 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”

IST210 13

Example 2: Querying Three Tables

• Show the names of employees who are assigned with projects associated with Finance department

Names of employees

Shared column: EmployeeNumber

Associated with Finance department

Shared column: ProjectID

Page 14: 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”

IST210 14

Example 2: Use Subquery • 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

Page 15: 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”

IST210 15

Example 2: Use Subquery

SELECT EmployeeNumberFROM ASSIGNMENTWHERE ProjectID IN (1100, 1400);

SELECT FirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN (4, 5, 6);

SELECT FirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN

(SELECT EmployeeNumber FROM ASSIGNMENT

WHERE ProjectID IN(SELECT

ProjectIDFROM PROJECTWHERE

Department = 'Finance')

);

• Show the names of employees who are assigned with projects associated with Finance department

SELECT ProjectIDFROM PROJECTWHERE Department = 'Finance';

Page 16: 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”

IST210 16

Example 2: Use Join

SELECT DISTINCT FirstName, LastNameFROM EMPLOYEE AS E, PROJECT AS P, ASSIGNMENT AS AWHERE E.EmployeeNumber = A.EmployeeNumber AND P.ProjectID = A.ProjectID AND P.Department = 'Finance';

• Show the names of employees who are assigned with projects associated with Finance department

Shared column: ProjectIDShared column: EmployeeNumber

Page 17: 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”

IST210 17

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

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

Page 18: 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”

IST210 18

Exercise 3: Use Join

• Show all the project names that are assigned to Ken– Use join

Shared column: ProjectIDShared column: EmployeeNumber

Page 19: 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”

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

Page 20: 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”

Changing Data Values: UPDATE

• To change the data values in an existing row (or set of rows) use the Update statement

UPDATE EMPLOYEESET Phone='360-555-1234'WHERE EmployeeNumber = 11;

UPDATE PROJECTSET ProjectID = 2000WHERE ProjectID = 1000

UPDATE DEPARTMENTSET DepartmentName = 'Public Relation'WHERE ProjectID = 'Marketing'

Page 21: 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”

Deleting Data: DELETE

• To delete a row or set of rows from a table using the DELETE statement

DELETE FROM EMPLOYEEWHERE EemplyeeNumber = 12;

DELETE FROM EMPLOYEEWHERE EemplyeeNumber = 8;

Page 22: 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”

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.