Download presentation
Presentation is loading. Please wait.
Published byHeriberto Smartt Modified over 9 years ago
1
SQL-week5-1 In-Class Exercise Answer IST 210 Organization of Data IST2101
2
Exercise 1: Review Questions Q1. Show the phone number of Accounting department (use Department table) – Use DEPARTMENT table – Use “WHERE”, slide #12 Q2. Show employees with FirstName as ‘Mary’ and LastName starting with letter ‘A’ – Use “OR”, slide #15 – Use wildcard, slide #20 IST2102
3
3 /* Q1. Show the phone number of Accounting department */ SELECT Phone FROM DEPARTMENT WHERE DepartmentName = 'Accounting'; /* Q2. Show employees with firstname as ‘Mary’ and lastname starting with letter ‘A’ */ SELECT * FROM EMPLOYEE WHERE FirstName = 'Mary' and LastName LIKE 'A%';
4
Exercise 2 Q1. Count how many projects with MaxHours less than 130 Q2. Show average MaxHours of all projects (Challenging question) Can you use one SQL query to answer: – Count how many projects with MaxHours less than average MaxHours? IST2104
5
5 /*** Count how many projects with MaxHours less than 130 ***/ SELECT COUNT(*) FROM PROJECT WHEREMaxHours < 130; /*** Show average MaxHours of all projects ***/ SELECTAVG(MaxHours) FROMPROJECT; /*** Count how many projects with MaxHours less than average MaxHours. ***/ SELECTCOUNT(*) FROMPROJECT WHEREMaxHours < (SELECT AVG(MaxHours) FROM PROJECT);
6
Exercise 3 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 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 IST2106
7
7 /*** Group assignments by employees. Show employee numbers and total hours worked. ***/ SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber; /*** Group assignments by employees. Show employee numbers and total hours worked for those employees with hours more than 100. ***/ SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber HAVING SUM(HoursWorked) > 100;
8
IST2108 /*** Add a constraint to Q1: only show the employees with number less than 5. ***/ SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT GROUP BY EmployeeNumber HAVING EmployeeNumber < 5; SELECT EmployeeNumber, SUM(HoursWorked) FROM ASSIGNMENT WHERE EmployeeNumber < 5 GROUP BY EmployeeNumber;
9
Exercise 4 IST2109 The names of employees who is assigned to ProjectID 1000 – First write two separate queries and then merge into one query 9 Q2. Show the names of employees with the employee numbers from Q1 Q1. Find employee numbers who is assigned to projectID 1000 EMPLOYEE ASSIGNMENT
10
10 /*** Exercise 4: 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);
11
Exercise 4: use join Show the names of employees who is assigned to ProjectID 1000 IST21011 Shared column: EmployeeNumber
12
12 /* Use join to answer exercise 4 */ SELECT FirstName, Lastname FROM EMPLOYEE AS E, ASSIGNMENT AS A WHERE E.EmployeeNumber = A.EmployeeNumber AND A.ProjectID=1000;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.