Module 10 Summarizing Data
Module 10 Summarizing Data
Summarizing Data
GROUP BY Fundamentals
Aggregate
Aggregatefunction
function Description
Description
AVG
AVG Average
Averageofofvalues
valuesininaanumeric
numericexpression
expression
COUNT
COUNT Number
Numberofofvalues
valuesininan
anexpression
expression
COUNT
COUNT(*)
(*) Number
Numberofofselected
selectedrows
rows
MAX
MAX Highest
Highestvalue
valueininthe
theexpression
expression
MIN
MIN Lowest
Lowestvalue
valueininthe
theexpression
expression
SUM
SUM Total
Totalvalues
valuesininaanumeric
numericexpression
expression
Using Aggregate Functions with Null Values
Most Aggregate Functions Ignore Null Values
COUNT(*) Function Counts Rows with Null Values
USE northwind
SELECT COUNT (*)
FROM employees
Aggregate Query
productid
productid orderid
orderid quantity
quantity productid total_quantity
productid total_quantity
11 11 55 11 15
15
11 11 10
10 22 35
35
22 11 10
10 33 45
45
22 22 25
25
33 11 15 productid
productid total_quantity
total_quantity
15 Only rows that
33 22 30 satisfy the WHERE 22 35
35
30 clause are grouped
USE northwind
SELECT productid, SUM(quantity)
AS total_quantity
FROM [order details]
WHERE productid = 2
GROUP BY productid
Using the GROUP BY Clause with the HAVING Clause
USE northwind
productid
productid orderid
orderid quantity
quantity SELECT productid, SUM(quantity)
AS total_quantity
11 11 55 FROM [order details]
GROUP BY productid
11 11 10
10 HAVING SUM(quantity) >=30
22 11 10
10
productid
productid total_quantity
total_quantity
22 22 25
25
22 35
35
33 11 15
15
33 45
45
33 22 30
30
USE
USE northwind
northwind
SELECT
SELECT productid,
productid, orderid,
orderid, SUM(quantity)
SUM(quantity) AS
AS total_quantity
total_quantity
FROM [order details]
FROM [order details]
GROUP
GROUP BY
BY productid,
productid, orderid
orderid
WITH ROLLUP
WITH ROLLUP
ORDER
ORDER BY
BY productid,
productid, orderid
orderid
productid
productid orderid
orderid total_quantity
total_quantity Description
Description
NULL
NULL NULL
NULL 95
95 Grand
Grandtotal
total
11 NULL
NULL 15
15 Summarizes
Summarizesonly
onlyrows
rowsfor
forproductid
productid11
11 11 55 Detail
Detailvalue forproductid
valuefor productid1,1,orderid
orderid11
11 22 10
10 Detail
Detailvalue forproductid
valuefor productid1,1,orderid
orderid22
22 NULL
NULL 35
35 Summarizes
Summarizesonly onlyrows
rowsfor
forproductid
productid22
22 11 10
10 Detail
Detailvalue forproductid
valuefor productid2,2,orderid
orderid11
22 22 25
25 Detail
Detailvalue forproductid
valuefor productid2,2,orderid
orderid22
33 NULL
NULL 45
45 Summarizes
Summarizesonly onlyrows
rowsfor
forproductid
productid33
33 11 15
15 Detail
Detailvalue forproductid
valuefor productid3,3,orderid
orderid11
33 22 30
30 Detail
Detailvalue forproductid
valuefor productid3,3,orderid
orderid22
Using the GROUP BY Clause with the CUBE Operator
USE
USE northwind
northwind
SELECT
SELECT productid,
productid, orderid,
orderid, SUM(quantity)
SUM(quantity) AS
AS total_quantity
total_quantity
FROM [order details]
FROM [order details]
GROUP
GROUP BY
BY productid,
productid, orderid
orderid
WITH CUBE
WITH CUBE
ORDER
ORDER BY
BY productid,
productid, orderid
orderid
productid
productid orderid
orderid total_quantity
total_quantity Description
Description
The
The CUBE
CUBE operator
operator NULL
NULL NULL
NULL 95
95 Grand
Grandtotal
total
produces
produces two
two NULL
NULL 11 30
30 Summarizes
Summarizesall allrows
rowsfor
fororderid
orderid11
more
more summary
summary NULL
NULL 22 65
65 Summarizes
Summarizesall allrows
rowsfor
fororderid
orderid22
values
values than
than the
the 11 NULL
NULL 15
15 Summarizes
Summarizesonly onlyrows
rowsfor
forproductid
productid11
ROLLUP
ROLLUP operator
operator 11 11 55 Detail
Detailvalue forproductid
valuefor productid1,1,orderid
orderid11
11 22 10
10 Detail
Detailvalue forproductid
valuefor productid1,1,orderid
orderid22
22 NULL
NULL 35
35 Summarizes
Summarizesonly onlyrows
rowsfor
forproductid
productid22
22 11 10
10 Detail
Detailvalue forproductid
valuefor productid2,2,orderid
orderid11
22 22 25
25 Detail
Detailvalue forproductid
valuefor productid2,2,orderid
orderid22
33 NULL
NULL 45
45 Summarizes
Summarizesonly onlyrows
rowsfor
forproductid
productid33
33 11 15
15 Detail
Detailvalue forproductid
valuefor productid3,3,orderid
orderid11
33 22 30
30 Detail
Detailvalue forproductid
valuefor productid3,3,orderid
orderid22
Using the COMPUTE and COMPUTE BY Clauses
COMPUTE
COMPUTE COMPUTE
COMPUTE BY
BY
USE USE
USE northwind
USE northwind
northwind northwind
SELECT SELECT
SELECT productid,
productid, orderid,
orderid, quantity
SELECT productid,
productid, orderid,
orderid, quantity
quantity quantity
FROM [order details] FROM [order details]
FROM [order details]
FROM [order details]
ORDER ORDER
ORDER BY
BY productid,
productid, orderid
ORDER BY
BY productid,
productid, orderid
orderid orderid
COMPUTE SUM(quantity) COMPUTE
COMPUTE SUM(quantity) BY
SUM(quantity) BY productid
productid
COMPUTE SUM(quantity)
COMPUTE SUM(quantity)
COMPUTE SUM(quantity)
productid
productid orderid
orderid quantity
quantity productid
productid orderid
orderid quantity
quantity
11 11 55 11 11 55
11 22 10
10 11 22 10
10
22 11 10
10 sum
sum 15
15
22 22 25
25 22 11 10
10
33 11 15
15 22 22 25
25
33 22 30
30 sum
sum 35
35
sum
sum 95
95 33 11 15
15
33 22 30
30
sum
sum 45
45
sum
sum 95
95
Listing the TOP n Values
USE
USE northwind
northwind
SELECT
SELECT TOP
TOP 55 orderid,
orderid, productid,
productid, quantity
quantity
FROM
FROM [order
[order details]
details]
ORDER
ORDER BY
BY quantity
quantity DESC
DESC
USE
USE northwind
northwind
SELECT
SELECT TOP
TOP 55 WITH
WITH TIES
TIES orderid,
orderid, productid,
productid, quantity
quantity
FROM [order details]
FROM [order details]
ORDER
ORDER BY
BY quantity
quantity DESC
DESC
Recommended Practices
Index
Index Frequently
Frequently Aggregated
Aggregated Columns
Columns
Avoid
Avoid Using
Using Aggregate
Aggregate Functions
Functions with
with Null
Null Values
Values
Use
Use the
the ORDER
ORDER BY
BY Clause
Clause to
to Guarantee
Guarantee aa Sort
Sort Order
Order
Use
Use the
the ROLLUP
ROLLUP Operator
Operator Whenever
Whenever Possible
Possible
Avoid
Avoid Using
Using the
the COMPUTE
COMPUTE or
or COMPUTE
COMPUTE BY
BY Clause
Clause
Check Your Understanding.
Q.1 What is an Aggregate Function.? Give names of the
functions.
Q.2. What is an Aggregate Query. ?
Q.3. What is Group By.?
Q.4. What is the difference between Group By and Group
By with Having.?
Q.5. All Aggregate Functions Ignore Null Values.
1. True
2. False
Q.6. COUNT(*) Function Counts Rows with
Null Values.
1. True
2. False
Q.7. CUBE operator produces more rows of summary
values than ROLLUP operator.
1. True
2. False
Q.8. What is the difference between COMPUTE and
COMPUTE BY.?
Q.9 Consider the following table named Salary.
a. 5 b. 6 c. 4 d. 3
Q.10. Consider the following table named Salary.
a. 5 b. 6 c. 4 d. 3
Q.11. Consider the following table named Salary.
a. 1083.33 b.1625.00
c. 1803.33 d. None of the above
Q.12 What will COUNT(*) return if there are no rows in the
table matching the search criteria.
a. -1
b. Null
c. 0
d. None of the above
Q.13 SQL provides Six Aggregate Functions.
1. True
2. False
Q.14 Keyword Distinct can be used with Count(*).
1. Yes
2. No
Q.15. Consider the following table named Salary
NAME BASIC HRA DEDUCTIONS
Alexis 4500 1500 500
Mathews 5000 2000 Null
Joseph 2500 1000 Null
Ravi 200 Null Null
Abraham 4000 2000 1000
Samuel 3500 Null 500
GROUP BY Fundamentals
Thank You.