Download presentation
Presentation is loading. Please wait.
Published byJanel Daniel Modified over 9 years ago
1
SQL-5 In-Class Exercise Answer IST 210 Organization of Data IST2101
2
Review Questions of Previous Class Q1. Show the total hours worked for project with ID 1200 (from assignment table) Q2. Count how many distinct departments in project table IST2102
3
3 /* Q1. Show the total hours worked for project with ID 1200 (from assignment table) */ SELECT SUM(HoursWorked) FROM ASSIGNMENT WHERE ProjectID = 1200; /* Q2. Count how many distinct departments in project table */ SELECT COUNT(distinct Department) FROM PROJECT;
4
Review Questions of Previous Class Q3. Group projects by departments. Show department names and number of projects associated with each department Q4. Add a constraint to Q3. Only show the departments with number of projects more than 1 IST2104
5
5 /* Q3 Group projects by departments. Show department names and number of projects associated with each department */ SELECT Department, COUNT(*) FROM PROJECT GROUP BY Department; /* Q4 Add a constraint to Q3. Only show the departments with number of projects more than 1 */ SELECT Department, COUNT(*) FROM PROJECT GROUP BY Department HAVING COUNT(*) > 1;
6
Exercise 1 IST2106 Show the names of employees who is assigned to ProjectID 1000 – First write two separate queries and then merge into one query 6 Q2. Show the names of employees with the employee numbers from Q1 Q1. Find employee numbers who is assigned to projectID 1000 EMPLOYEE ASSIGNMENT
7
7 /*** Exercise 1: Show the names of employees who is assigned to ProjectID 1000 ***/ /* Q1. Find employee numbers who is assigned to projectID 1000 */ SELECT EmployeeNumber FROM ASSIGNMENT WHERE ProjectID = 1000; /* Q2. Show the names of employees with the employee numbers from Q1 */ SELECT FirstName, Lastname FROM EMPLOYEE WHERE EmployeeNumber IN (1,8,10); /* Use subquery to answer exercise 1 */ SELECT FirstName, Lastname FROM EMPLOYEE WHERE EmployeeNumber IN (SELECT EmployeeNumber FROM ASSIGNMENT WHERE ProjectID = 1000);
8
Exercise 1: use join Show the names of employees who is assigned to ProjectID 1000 IST2108 Shared column: EmployeeNumber
9
9 /* Use join to answer exercise 1 */ SELECT FirstName, Lastname FROM EMPLOYEE AS E, ASSIGNMENT AS A WHERE E.EmployeeNumber = A.EmployeeNumber AND A.ProjectID=1000;
10
Exercise 2 Show the project names assigned to EmployeeNumber 4 IST21010 ASSIGNMENT PROJECT
11
IST21011 /*** Exercise 2: Show the project names assigned to EmployeeNumber 4 ***/ /* 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); /* Use subquery to answer exercise 2 */ SELECT ProjectName FROM PROJECT WHERE ProjectID IN (SELECT ProjectID FROM ASSIGNMENT WHERE EmployeeNumber = 4);
12
Exercise 2: use join Show the project names assigned to EmployeeNumber 4 IST21012 Shared column: ProjectID
13
IST21013 /* Use join to answer exercise 2 */ SELECT ProjectName FROM PROJECT AS P, ASSIGNMENT AS A WHERE P.ProjectID = A.ProjectID AND A.EmployeeNumber=4;
14
Exercise 3 Show all the project names that are assigned to Ken – Write 3 separate queries first and then merge them into 1 IST21014 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
15
IST21015 /* use subquery to answer exercise 3*/ SELECT ProjectName FROM PROJECT WHERE ProjectID IN (SELECT ProjectID FROM ASSIGNMENT WHERE EmployeeNumber IN (SELECT EmployeeNumber FROM EMPLOYEE WHERE FirstName = 'Ken' ) );
16
Exercise 3: Use Join Show all the project names that are assigned to Ken – Use join IST21016 Shared column: ProjectIDShared column: EmployeeNumber
17
IST21017 /* use join to answer exercise 3*/ 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';
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.