Download presentation
Presentation is loading. Please wait.
Published byMarshall Mills Modified over 9 years ago
1
Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement. Introduce sub-queries. 1
2
2 Structure of the SELECT statement SELECT [all or distinct] FROM (table) WHERE (condition) GROUP BY (grouping fields) HAVING (condition) ORDER BY (sort fields) Order of Actual Execution: 1)FROM 2)WHERE 3)GROUP BY 4)HAVING 5)SELECT 6)ORDER BY Referred to as the “SELECT LIST” When a SELECT statement is executed, the result is referred to as a “result table”. It is a memory-based table.
3
Table used for examples 3
4
Different SELECT options 4 SELECT DISTINCT deptno FROMemp1 SELECT TOP 1 salary FROM emp1 ORDER BY salary SELECT TOP 1 salary FROM emp1 ORDER BY salary SELECT TOP 10 PERCENT salary FROMemp1 ORDER BY salary desc
5
What is a Group Function? A way to summarize data and provide more meaningful and informative output from the database. Sometimes referred to as “summary queries” or “group functions.” Group functions differ from single row SELECT statements: –A SELECT statement processes every row in the underlying table. The result table (unless a WHERE clause is used) contains one row per row in the underlying table. –A group function collects data from multiple rows and produces summarized data in the result table. There should be one row in the result table per group. 5
6
What do group functions produce? If a group function is run on the whole table, without grouping, it generates a single row result table. If a group function is run with grouping (the GROUP BY statement) then it generates one row per group in the result table. 6
7
Why are group functions important? They provide information rather than data for the end-users of technology. They help programmers understand large data sets. 7
8
FunctionDescription of What is Returned AVG Average value of a numeric column; ignores null values COUNT Number of rows. When * is used, all rows are returned (including null values and duplicate rows) MAX Maximum value of a column; ignores null values MIN Minimum value of a column; ignores null values SUM Totals the value of a numeric column; ignores null values 8
9
Calculating Averages SELECT AVG(salary) FROM emp1; SELECT ROUND(AVG(salary),2) FROM emp1; SELECT ROUND(AVG(salary),2) FROM emp1 WHERE deptno = 10; Use the ROUND function to perform both a mathematical rounding operation and truncate the result to a set number of digits after the decimal point 9
10
Counting Rows SELECT COUNT(*) FROM emp1; SELECT COUNT(*) FROM emp1 WHERE deptno = 10; SELECT COUNT(*) FROM emp1 WHERE salary > 2000 and deptno = 10; SELECT COUNT(DISTINCT deptno) FROM emp1; 10
11
Finding Minimum and Maximum Values SELECT MIN(hiredate) FROM emp1; SELECT MAX(hiredate) FROM emp1; SELECT MIN(ename) FROM emp1; SELECT MAX(hiredate) FROM emp1 WHERE deptno = 10; 11
12
Groups with Calculations/Functions SELECTMAX(salary + ISNULL(comm,0)) FROMemp1; SELECTMAX(DATEDIFF(mm, hiredate, GETDATE()) FROMemp1; 12
13
Combining group functions SELECT COUNT(salary), SUM(salary), MIN(salary) FROM emp1 WHERE deptno = 10 and salary < 4000; 13
14
Group function issue… Combining group functions with single row values - doesn't work!! SELECT deptno, COUNT(salary), SUM(salary) FROM emp1 14
15
Creating summary output by grouping SELECT deptno, SUM(salary) FROM emp1 GROUP BY deptno; SELECT deptno, SUM(salary) FROM emp1 WHERE salary > 2000 GROUP BY deptno; Eliminates rows before the grouping occurs 15
16
Summary output with conditions SELECT deptno, SUM(salary) FROM emp1 GROUP BY deptno HAVING SUM(salary) > 6000; SELECT deptno, SUM(salary) FROM emp1 GROUP BY deptno HAVING AVG(salary) > 2000; The HAVING statement uses group functions for the condition or grouped attributes 16
17
Multi-attribute grouping SELECT deptno, job, SUM(salary), AVG(salary) FROM emp1 GROUP BY deptno, job; 17
18
A group function isn’t a condition!! SELECT MAX(salary) FROMemp1 The statement above does identify the maximum salary from the table. Now try to add the name of the employee to that query: SELECT ename, MAX(salary) FROMemp1
19
GROUP BY may not work exactly as you expect… Example 1: What if you want to see the employee name of the employee who has the maximum salary? SELECTename, MAX(salary) FROMemp1 GROUP BYename; This code does not accomplish that goal!
20
Thoughts about fixing the problem… SELECTename, max(salary) FROMemp1 WHEREsalary = MAX(salary) GROUP BYename; SELECTename, max(salary) FROMemp1 GROUP BYename HAVINGmax(salary) = salary; They don’t work!
21
Could use the TOP 1 SELECT... 21 SELECTTOP 1 ename, salary, deptno FROMemp1 ORDER BY salary desc But this works only in the SQL Server environment because TOP 1 is not ANSI-Standard SQL
22
ANSI–Standard SQL Needs a sub-query to work correctly SELECTename, salary, deptno FROMemp1 WHEREsalary = (SELECT MAX(salary) FROMemp1) Example 1 answer:
23
What is a sub-query? A sub-query is a query embedded inside another query. The sub-query is executed in the normal operation of the query in which it is embedded. The sub-query will return an “answer” result table to the query in which it is embedded. A sub-query can be placed in the SELECT list, FROM statement, WHERE clause &/or HAVING clause. Two types of sub-queries: non-correlated and correlated.
24
Types of sub-queries Non-correlated –Inner and outer queries do not exchange data. Correlated –Inner and outer queries do exchange data. 24
25
Example 2 of a non-correlated sub-query 25 Which employee has been employed with the organization for the longest time? What is the name and salary of that employee? Or another way to look at it: What is the name and salary of the employee who was hired first in our organization Write a query to accomplish that goal!! (Use slide #22 as your example)
26
Let’s expand our sample data to include a new table for department 26
27
27 What if we want to see all the employees who are in deptno 20? No problem... SELECT* FROMemp1 WHEREdeptno = 20 Relational operators in a single table
28
28 What if we want to see all employees who have any department that is in the dept1 table ? SELECT* FROMemp1 WHEREdeptno IN (SELECT deptno FROM dept1) Example 3 of a non-correlated sub-query:
29
29 What if we want to see all employees who don’t have a department that is in the dept1 table ? SELECT* FROMemp1 WHEREdeptno NOT IN (SELECT deptno FROM dept1) Example 4 of a non-correlated sub-query:
30
Example of the need for a correlated sub-query Example 1 of a correlated sub-query: which employees have a higher salary than the average salary for their department? SELECT deptno, AVG(salary) FROMemp1 GROUP BY deptno; SELECT empno, ename, deptno, salary FROM emp1 ORDER BY deptno
31
A Correlated Sub-Query 31 SELECTempno, ename, deptno, salary FROMemp1 outquery WHEREsalary > (SELECTAVG(salary) FROMemp1 inquery WHEREoutquery.deptno = inquery.deptno) Requires that the tables have aliases
32
What if we also want to see the average salary in the SELECT list? 32 SELECTempno, ename, deptno, salary, (SELECT AVG(salary) FROMemp1 squery WHERE squery.deptno = outquery.deptno) FROMemp1 outquery WHEREsalary > (SELECTAVG(salary) FROMemp1 inquery WHEREoutquery.deptno = inquery.deptno)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.