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

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

Embed Size (px)

Citation preview

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

IST210 1

STRUCTURED QUERY LANGUAGE SQL-IIIIST 210 Organization of Data

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

IST210 2

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

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

IST210 3

PreviouslySELECT * FROM PROJECT;

SELECT Department FROM EMPLOYEE;

SELECT DISTINCT Department FROM EMPLOYEE;

SELECT *FROM PROJECTWHERE Department = 'Finance’;

SELECT *FROM PROJECTWHERE Department = 'Finance' OR MaxHours > 135;

SELECT FirstName, LastName, DepartmentFROM EMPLOYEEWHERE Department IN ('Finance', 'Accounting', 'Marketing');

WHERE LastName LIKE 'J%';WHERE LastName LIKE 'J____';

ORDER BY LastName DESC, FirstName ASC;

SELECT COUNT(*) FROM PROJECT;

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

IST210 4

Running Example

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

IST210 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%';

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

IST210 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

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

IST210 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.

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

IST210 9

Review Questions of Previous ClassWhat is wrong with queries on the left?• Q9. Show employees with FirstName containing word “Ma”

SELECT *FROM EMPLOYEEWHERE FirstName = '%Ma%' ;

• Q10. Show employees without phone numbers in database

SELECT*FROM EMPLOYEEWHERE Phone = NULL;

• Q11. Count how many projects with MaxHours less than average MaxHours?

SELECTCOUNT(*)FROM PROJECTWHERE MaxHours < AVG(MaxHours);

SELECT *FROM EMPLOYEEWHERE FirstName LIKE '%Ma%' ;

SELECT*FROM EMPLOYEEWHERE Phone IS NULL;

SELECTCOUNT(*)FROM PROJECTWHERE MaxHours < (SELECT AVG(MaxHours) from PROJECT);

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

IST210 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 NumOfEmployeesFROM EMPLOYEEGROUP 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.

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

IST210 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 displayedSELECT Department, COUNT(*) AS NumOfEmployeesFROM EMPLOYEEGROUP BY DepartmentHAVING COUNT(*) > 1;

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

IST210 12

Exercise 5• Q1. Group assignments by employees, as each employee

can be involved in multiple projects. Show employee numbers and total hours work

SELECT EmployeeNumber, SUM(HoursWorked)FROM ASSIGNMENTGROUP BY EmployeeNumber;

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

IST210 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

SELECT EmployeeNumber, SUM(HoursWorked)FROM ASSIGNMENTGROUP BY EmployeeNumberHAVING SUM(HoursWorked) > 100;

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

IST210 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

SELECT EmployeeNumber, SUM(HoursWorked)FROM ASSIGNMENTGROUP BY EmployeeNumberHAVING EmployeeNumber < 5;

SELECT EmployeeNumber, SUM(HoursWorked)FROM ASSIGNMENTWHERE EmployeeNumber < 5GROUP BY EmployeeNumber;

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

IST210 15

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

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

IST210 16

Content in today’s class• Query: SELECT … FROM … WHERE

• Query across multiple tables• Two approaches: (1) Subquery and (2) Join

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

IST210 17

Question 1: Querying Two TablesThe names of employees who worked less than 20 hours in any project

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

EMPLOYEEASSIGNMENT

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

IST210 18

Question 1: Use Subquery • The names of employees who worked less than 20 hours

SELECTEmployeeNumberFROM ASSIGNMENTWHERE HoursWorked < 20;

SELECTFirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN (4, 5);

1. Check “worked less than 20 hours” in ASSIGNMENT table

2. Get names of the employees with employee number 4, 5

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

IST210 19

Question 1: Use Subquery • The names of employees who worked less than 20 hours

SELECTEmployeeNumberFROM ASSIGNMENTWHERE HoursWorked < 20;

SELECTFirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN (4, 5);

SELECTFirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN

(SELECTEmployeeNumber

FROM ASSIGNMENT WHERE HoursWorked <

20);

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

IST210 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

20Q2. Show the names of employees with the employee numbers from Q1

Q1. Find employee numbers who is assigned to projectID 1000

EMPLOYEE

ASSIGNMENT

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

IST210 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

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 EmployeeNumberFROM ASSIGNMENTWHERE ProjectID = 1000;

SELECT FirstName, LastnameFROM EMPLOYEEWHERE EmployeeNumber IN (1,8,10);

Step 3: Merge them together SELECT FirstName, LastnameFROM EMPLOYEEWHERE EmployeeNumber IN

(SELECT EmployeeNumberFROM ASSIGNMENTWHERE ProjectID =

1000);

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

IST210 22

Question 1: Use Join

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

AND A.HoursWorked < 20;

SELECT EmployeeNumberFROM ASSIGNMENTWHERE HoursWorked < 20;

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

• The names of employees who worked less than 20 hours

SELECTFirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN

(SELECTEmployeeNumber

FROM ASSIGNMENT WHERE HoursWorked <

20);

Subqueries are effective for processing multiple tables, as long as the resultscome from a single table.

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

IST210 23

Question 2: Use Join• The names of employees who worked less than 20 hours

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

AND A.HoursWorked < 20;

Shared column: EmployeeNumber

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

IST210 24

Exercise 1: Use Join

The names of employees who is assigned to ProjectID 1000

Shared column: EmployeeNumber

SELECT FirstName, LastnameFROM EMPLOYEE AS E, ASSIGNMENT AS AWHERE E.EmployeeNumber = A.EmployeeNumber AND A.ProjectID=1000;

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

IST210 25

Exercise 2: Use Subquery• The project names assigned to EmployeeNumber 4

ASSIGNMENT

PROJECT/* Q1. ProjectIDs assigned to Employee Number 4 */SELECT ProjectIDFROM ASSIGNMENTWHERE EmployeeNumber = 4;

/* Q2. Project names with project IDs from Q1 */SELECT ProjectNameFROM PROJECTWHERE ProjectID IN (1100, 1200, 1400);

SELECT ProjectNameFROM PROJECTWHERE ProjectID IN

(SELECT ProjectIDFROM ASSIGNMENTWHERE EmployeeNumber = 4);

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

IST210 26

Exercise 2: Use Join• The project names assigned to EmployeeNumber 4

Shared column: ProjectID

SELECT ProjectNameFROM PROJECT AS P, ASSIGNMENT AS AWHERE P.ProjectID = A.ProjectID AND A.EmployeeNumber=4;

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

IST210 27

Question 2: Querying Three Tables• 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 27: STRUCTURED QUERY LANGUAGE SQL-III IST 210 Organization of Data IST210 1

IST210 28

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

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

IST210 29

Question 2: Use Subquery

SELECT EmployeeNumberFROM ASSIGNMENTWHERE ProjectID IN (1100, 1400);

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

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

SELECT ProjectIDFROM PROJECTWHERE Department = '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}

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

IST210 30

Question 2: Use Subquery

SELECT EmployeeNumberFROM ASSIGNMENTWHERE ProjectID IN (1100, 1400);

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

SELECTFirstName, LastNameFROM EMPLOYEEWHERE EmployeeNumber IN

(SELECTEmployeeNumber

FROM ASSIGNMENT WHERE ProjectID IN

(SELECTProjectID

FROMPROJECT

WHEREDepartment = 'Finance'

));

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

SELECT ProjectIDFROM PROJECTWHERE Department = 'Finance';

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

IST210 31

Question 2: Use Join

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

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

Shared column: ProjectIDShared column: EmployeeNumber

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

IST210 32

Exercise 3• Show all the project names that are assigned to Ken

• Write 3 separate queries first and then merge them into 1

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

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

IST210 33

SELECT ProjectName

FROM PROJECT

WHERE ProjectID IN

(SELECT ProjectID

FROM ASSIGNMENT

WHERE EmployeeNumber IN

(SELECT EmployeeNumber

FROM EMPLOYEE

WHERE FirstName = 'Ken'

)

);

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

IST210 34

Exercise 3: Use Join• Show all the project names that are assigned to Ken

• Use join

Shared column: ProjectIDShared column: EmployeeNumber

SELECT ProjectNameFROM EMPLOYEE AS E, PROJECT AS P, ASSIGNMENT AS AWHERE E.EmployeeNumber = A.EmployeeNumber AND

P.ProjectID = A.ProjectID AND E.FirstName='Ken';

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

IST210 35

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

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

IST210 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!

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

IST210 37

Wednesday Class• Exercise Class• Assignment P1• Assignment P2• Assignment 3-1• Assignment 3-2