Download presentation
Presentation is loading. Please wait.
Published bySheena Rosamund Cummings Modified over 9 years ago
1
SQL for Data Retrieval
2
Running Example IST2102
3
Data Preparation Login to SQL server using your account Download three SQL script files from wiki page and open them in SQL Server Management Studio. – SQL-Delete-Tables.sql – SQL-Create-Tables.sql – SQL-Insert-Data.sql Select your database – Your database name is your PSU ID – Alternatively, you can add one statement at the first line of each script: USE [your PSU ID]; Execute scripts.
4
Review of Previous Class: Queries SELECT is the best known SQL statement SELECT will retrieve information from the database that matches the specified criteria using the SELECT / FROM / WHERE framework SELECT ColumnName FROM Table WHERECondition;
5
Exercise 1: Review Questions Q1. Show the phone number of Accounting department (use Department table) – Use DEPARTMENT table – Use “WHERE”, slide #13 (week4-3.pptx) Q2. Show employees with FirstName as ‘Mary’ and LastName starting with letter ‘A’ – Use “OR”, slide #16 – Use wildcard, slide #21 IST2105
6
SQL for Data Retrieval: Sorting the Results Query results may be sorted using the ORDER BY clause SELECT * FROM EMPLOYEE ORDER BY LastName;
7
SQL for Data Retrieval: Sorting the Results By default, SQL sorts in ascending order. To sort in descending order, use: SELECT FirstName, LastName, Phone, Department FROM EMPLOYEE ORDER BY Department DESC;
8
SQL for Data Retrieval: Sorting the Results Query FirstName, LastName, Phone, Department from EMPLOYEE table, sort the result by Department in descending order, and then by LastName in ascending order IST2108 SELECT FirstName, LastName, Phone, Department FROM EMPLOYEE ORDER BY Department DESC, LastName ASC;
9
SQL for Data Retrieval: Built-in SQL Functions SQL provides several built-in functions – COUNT Counts the number of rows that match the specified criteria – MIN Finds the minimum value for a specific column for those rows matching the criteria – MAX Finds the maximum value for a specific column for those rows matching the criteria – SUM Calculates the sum for a specific column for those rows matching the criteria – AVG Calculates the numerical average of a specific column for those rows matching the criteria
10
SQL for Data Retrieval: Built-in Function Examples Count the total number of employees: SELECT COUNT(*) FROM EMPLOYEE; Find the minimum, maximum, and average hours for all projects. SELECT MIN(MaxHours) AS MinimumHours, MAX(MaxHours) AS MaximumHours, AVG(MaxHours) AS AverageHours FROM PROJECT
11
SQL for Data Retrieval: Built-in Function Examples SQL standard does not allow column names to be mixed with built-in functions, except in certain uses of GROUP BY clause (to be discussed next) SELECTMaxHours, SUM(MaxHours) FROMPROJECT WHEREProjectID <= 1200; Bad query. MaxHours is not an aggregate result, but SUM() is.
12
Exercise 2 Q1. Count how many projects with MaxHours less than 130 – Use “WHERE” Q2. Show average MaxHours of all projects Q3. (Challenging) Count how many projects with MaxHours less than the average MaxHours? IST21012
13
SQL for Data Retrieval: Providing Subtotals: GROUP BY Subtotals may be calculated by using the GROUP BY clause – Show how many employees in each department SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department The HAVING clause may be used to restrict which data is displayed – Only show those departments with more than one employee SELECT Department, COUNT(*) AS NumOfEmployees FROM EMPLOYEE GROUP BY Department HAVING COUNT(*) > 1;
14
Exercise 3 Q1. Group assignments by employees. Show employee number and total hours worked Q2. Add a constraint to Q1: only show 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: (1) Use “WHERE”, (2) Use “HAVING” IST21014
15
Retrieving Information from Multiple Tables Two approaches – Subqueries – Joins
16
Example 1: Querying Two Tables IST21016 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
17
Example 1: Use Subquery IST21017 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
18
Exercise 4: Use Subquery IST21018 Show the names of employees who is assigned to ProjectID 1000 – First write two separate queries and then merge into one query 18 Q2. Show the names of employees with the employee numbers from Q1 Q1. Find employee numbers who is assigned to ProjectID 1000 EMPLOYEE ASSIGNMENT
19
Example 1: Use Join IST21019 SELECTFirstName, LastName 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);
20
Example 1: Use Join IST21020 SELECTFirstName, LastName 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
21
Exercise 4: Use Join Show the names of employees who is assigned to ProjectID 1000 IST21021 Shared column: EmployeeNumber
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.