SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your...
-
Upload
sheena-rosamund-cummings -
Category
Documents
-
view
222 -
download
0
Transcript of SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your...
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.
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 TableWHERE Condition;
IST210 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
SQL for Data Retrieval:Sorting the Results
• Query results may be sorted using the ORDER BY clause
SELECT *FROM EMPLOYEEORDER BY LastName;
SQL for Data Retrieval:Sorting the Results
• By default, SQL sorts in ascending order. • To sort in descending order, use:
SELECT FirstName, LastName, Phone, DepartmentFROM EMPLOYEEORDER BY Department DESC;
IST210 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
SELECT FirstName, LastName, Phone, DepartmentFROM EMPLOYEEORDER BY Department DESC, LastName ASC;
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
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
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)
SELECT MaxHours, SUM(MaxHours)FROM PROJECTWHERE ProjectID <= 1200;
Bad query. MaxHours is not an aggregate result, but SUM() is.
IST210 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?
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 EMPLOYEEGROUP BY Department
• The HAVING clause may be used to restrict which data is displayed– Only show those departments with more than one
employeeSELECT Department,
COUNT(*) AS NumOfEmployeesFROM EMPLOYEEGROUP BY Department
HAVING COUNT(*) > 1;
IST210 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”
IST210 16
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
IST210 17
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
IST210 18
Exercise 4: Use Subquery• Show the names of employees who is assigned to ProjectID 1000
– First write two separate queries and then merge into one query
18Q2. Show the names of employees with the employee numbers from Q1
Q1. Find employee numbers who is assigned to ProjectID 1000
EMPLOYEE
ASSIGNMENT
IST210 19
Example 1: Use Join
SELECT FirstName, LastNameFROM 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);
IST210 20
Example 1: Use Join
SELECT FirstName, LastNameFROM 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