Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is.

Similar presentations


Presentation on theme: "SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is."— Presentation transcript:

1 SQL for Data Retrieval

2 Running Example IST2102

3 Data Preparation Login to SQL server using your account 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]; Download three SQL script files from wiki page and open them in SQL Server Management Studio. 1.SQL-Delete-Tables.sql 2.SQL-Create-Tables.sql 3.SQL-Insert-Data.sql Execute scripts (1  2  3).

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 (3-SQL-3.pptx) Q2. Show employees with FirstName as ‘Mary’ or LastName starting with letter ‘A’ – Use “OR”, slide #16 – Use wildcard, slide #22 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 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? IST21011

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

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


Download ppt "SQL for Data Retrieval. Running Example IST2102 Data Preparation Login to SQL server using your account Select your database – Your database name is."

Similar presentations


Ads by Google