32
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

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

Embed Size (px)

DESCRIPTION

Table used for examples 3

Citation preview

Page 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

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

Page 2: 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

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) FROM2) WHERE3) GROUP BY4) HAVING5) SELECT6) 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.

Page 3: 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

Table used for examples

3

Page 4: 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

Different SELECT options

4

SELECT DISTINCT deptnoFROM emp1

SELECT TOP 1 salaryFROM emp1ORDER BY salary

SELECT TOP 1 salaryFROM emp1ORDER BY salary

SELECT TOP 10 PERCENT salaryFROM emp1ORDER BY salary desc

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

Page 8: 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

Function Description 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

Page 9: 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

Calculating Averages

 SELECT AVG(salary)FROM emp1;

  

SELECT ROUND(AVG(salary),2)FROM emp1;

  

SELECT ROUND(AVG(salary),2)FROM emp1WHERE 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

Page 10: 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

Counting Rows

 

SELECT COUNT(*)FROM emp1;

SELECT COUNT(*)FROM emp1WHERE deptno = 10; SELECT COUNT(*)FROM emp1WHERE salary > 2000 and deptno = 10;

SELECT COUNT(DISTINCT deptno)FROM emp1; 

10

Page 11: 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

Finding Minimum and Maximum Values

SELECT MIN(hiredate)FROM emp1;  SELECT MAX(hiredate)FROM emp1;

SELECT MIN(ename)FROM emp1;  SELECT MAX(hiredate)FROM emp1WHERE deptno = 10;

11

Page 12: 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

Groups with Calculations/Functions

SELECT MAX(salary + ISNULL(comm,0))FROM emp1;

SELECT MAX(DATEDIFF(mm, hiredate, GETDATE())FROM emp1;

12

Page 13: 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

Combining group functions

SELECT COUNT(salary), SUM(salary), MIN(salary)

FROM emp1WHERE deptno = 10 and salary < 4000;

13

Page 14: 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

Group function issue…

Combining group functions with single row values - doesn't work!!

 SELECT deptno,

COUNT(salary), SUM(salary)

FROM emp1

14

Page 15: 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

Creating summary output by grouping

SELECT deptno, SUM(salary)

FROM emp1GROUP BY deptno;

SELECT deptno, SUM(salary)

FROM emp1WHERE salary > 2000GROUP BY deptno;

Eliminates rows before the grouping

occurs

15

Page 16: 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

Summary output with conditions

SELECT deptno, SUM(salary)FROM emp1GROUP BY deptnoHAVING SUM(salary) > 6000;

 SELECT deptno, SUM(salary)FROM emp1GROUP BY deptnoHAVING AVG(salary) > 2000;  

The HAVING statement uses

group functions for the condition or

grouped attributes

16

Page 17: 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

Multi-attribute grouping

SELECT deptno, job, SUM(salary), AVG(salary)

FROM emp1GROUP BY deptno,

job;

17

Page 18: 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

A group function isn’t a condition!!

SELECT MAX(salary)FROM emp1

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)

FROM emp1

Page 19: 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

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?

SELECT ename,MAX(salary)

FROM emp1GROUP BY ename;

This code does not accomplish that goal!

Page 20: 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

Thoughts about fixing the problem…

SELECT ename,max(salary)

FROM emp1WHERE salary = MAX(salary)GROUP BY ename;

SELECT ename,max(salary)

FROM emp1GROUP BY enameHAVING max(salary) = salary;

They don’t work!

Page 21: 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

Could use the TOP 1 SELECT...

21

SELECT TOP 1 ename,salary,deptno

FROM emp1ORDER BY salary desc

But this works only in the SQL Server environment because TOP 1 is not

ANSI-Standard SQL

Page 22: 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

ANSI–Standard SQLNeeds a sub-query to work correctly

SELECT ename,salary,deptno

FROM emp1WHERE salary =

(SELECT MAX(salary) FROM emp1)

Example 1 answer:

Page 23: 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

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.

Page 24: 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

Types of sub-queries

Non-correlated– Inner and outer queries do not exchange data.

Correlated– Inner and outer queries do exchange data.

24

Page 25: 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

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)

Page 26: 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

Let’s expand our sample data to include a new table for department

26

Page 27: 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

27

What if we want to see all the employees who are in deptno 20? No problem...

SELECT *FROM emp1WHERE deptno = 20

Relational operators in a single table

Page 28: 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

28

What if we want to see all employees who have any department that is in the dept1

table?

SELECT *FROM emp1WHERE deptno IN

(SELECT deptno FROM dept1)

Example 3 of a non-correlated sub-query:

Page 29: 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

29

What if we want to see all employees who don’t have a department that is in the

dept1 table?

SELECT *FROM emp1WHERE deptno NOT IN

(SELECT deptno FROM dept1)

Example 4 of a non-correlated sub-query:

Page 30: 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

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)

FROM emp1GROUP BY deptno;

SELECT empno,ename,deptno,salary

FROM emp1ORDER BY deptno

Page 31: 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

A Correlated Sub-Query

31

SELECT empno,ename,deptno,salary

FROM emp1 outqueryWHERE salary >

(SELECT AVG(salary) FROM emp1 inquery WHERE outquery.deptno = inquery.deptno)

Requires that the tables have aliases

Page 32: 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

What if we also want to see the average salary in the SELECT list?

32

SELECT empno,ename,deptno,salary,(SELECT AVG(salary) FROM emp1 squery WHERE squery.deptno = outquery.deptno)

FROM emp1 outqueryWHERE salary >

(SELECT AVG(salary) FROM emp1 inquery WHERE outquery.deptno = inquery.deptno)