SlideShare a Scribd company logo
Yet More SQL SELECT
Database Systems Lecture 9
Natasha Alechina
In This Lecture
• Yet more SQL
• ORDER BY
• Aggregate functions
• GROUP BY and HAVING
• UNION etc.
• For more information
• Connoly and Begg Chapter 5
• Ullman and Widom Chapter 6.4
SQL SELECT Overview
SELECT
[DISTINCT | ALL] <column-list>
FROM <table-names>
[WHERE <condition>]
[ORDER BY <column-list>]
[GROUP BY <column-list>]
[HAVING <condition>]
([]- optional, | - or)
ORDER BY
• The ORDER BY clause
sorts the results of a
query
• You can sort in
ascending (default) or
descending order
• Multiple columns can
be given
SELECT <columns>
FROM <tables>
WHERE <condition>
ORDER BY <cols>
[ASCENDING |
DESCENDING|
ASC | DESC ]
ORDER BY Example
Grades
Name Code Mark
John DBS 56
John IAI 72
Mary DBS 60
Mark PR1 43
Mark PR2 35
Jane IAI 54
Name Code Mark
Mark PR2 35
Mark PR1 43
Jane IAI 54
John DBS 56
Mary DBS 60
John IAI 72
SELECT * FROM Grades
ORDER BY Mark
ORDER BY Example
Grades
Name Code Mark
John DBS 56
John IAI 72
Mary DBS 60
Mark PR1 43
Mark PR2 35
Jane IAI 54
Name Code Mark
Mary DBS 60
John DBS 56
John IAI 72
Jane IAI 54
Mark PR1 43
Mark PR2 35
SELECT * FROM Grades
ORDER BY Code ASC,
Mark DESC
Constants and Arithmetic
• As well as column
names, you can
select constants,
compute arithmetic
expressions and
evaluate functions in
a SELECT statement
SELECT Mark/100
FROM Grades
SELECT
Salary + Bonus
FROM Employee
SELECT 1.175*Price
FROM Products
Aggregate Functions
• Aggregate functions
compute summaries
of data in a table
• Most aggregate
functions (all except
COUNT) work on a
single column of
numeric data
• Use an alias to name
the result
• Aggregate functions
• COUNT: The number of
rows
• SUM: The sum of the
entries in a column
• AVG: The average
entry in a column
• MIN, MAX: The
minimum and
maximum entries in a
column
Aggregate Functions
Grades
Name Code Mark
John DBS 56
John IAI 72
Mary DBS 60
Mark PR1 43
Mark PR2 35
Jane IAI 54
SELECT
COUNT(*) AS Count
FROM Grades
SELECT
SUM(Mark) AS Total
FROM Grades
SELECT
MAX(Mark) AS Best
FROM Grades
Count
6
Total
320
Best
72
Aggregate Functions
• You can combine
aggregate functions
using arithmetic
SELECT
MAX(Mark)-MIN(Mark)
AS Range
FROM Grades
Grades
Name Code Mark
John DBS 56
John IAI 72
Mary DBS 60
Mark PR1 43
Mark PR2 35
Jane IAI 54
Range
37
MAX(Mark) = 72
MIN(Mark) = 35
Example
• Find John’s average
mark, weighted by
the credits of each
module
Modules
Code Title Credits
DBS Database Sys. 10
GRP Group Project 20
PRG Programming 10
Grades
Name Code Mark
John DBS 60
Mark GRP 47
Mary PRG 56
SELECT
SUM(Mark*Credits)/SUM(Credits)
FROM Modules, Grades
WHERE Modules.Code=Grades.Code
AND Grades.Name = ‘John’
GROUP BY
• Sometimes we want
to apply aggregate
functions to groups
of rows
• Example, find the
average mark of
each student
• The GROUP BY clause
does this
SELECT <cols1>
FROM <tables>
GROUP BY <cols2>
GROUP BY
SELECT <cols1>
FROM <tables>
GROUP BY <cols2>
• Every entry in
<cols1> must be in
<cols2>, be a
constant, or be an
aggregate function
• You can have WHERE
and ORDER BY
clauses as well as a
GROUP BY clause
GROUP BY
Grades
Name Code Mark
John DBS 56
John IAI 72
Mary DBS 60
Mark PR1 43
Mark PR2 35
Jane IAI 54
SELECT Name,
AVG(Mark) AS Average
FROM Grades
GROUP BY Name
Name Average
John 64
Mary 60
Mark 39
Jane 54
GROUP BY
• Find the total value
of the sales for each
department in each
month
• Can group by Month
then Department or
Department then
Month
• Same results, but in a
different order
Month Department Value
March Fiction 20
March Travel 30
March Technical 40
April Fiction 10
April Fiction 30
April Travel 25
April Fiction 20
May Fiction 20
May Technical 50
Sales
GROUP BY
Month Department Total
April Fiction 60
April Travel 25
March Fiction 20
March Technical 40
March Travel 30
May Fiction 20
May Technical 50
SELECT Month, Department,
SUM(Value) AS Total
FROM Sales
GROUP BY Month, Department
Month Department Total
April Fiction 60
March Fiction 20
May Fiction 20
March Technical 40
May Technical 50
April Travel 25
March Travel 30
SELECT Month, Department,
SUM(Value) AS Total
FROM Sales
GROUP BY Department, Month
HAVING
• HAVING is like a
WHERE clause,
except that it applies
to the results of a
GROUP BY query
• It can be used to
select groups which
satisfy a given
condition
SELECT Name,
AVG(Mark) AS Average
FROM Grades
GROUP BY Name
HAVING AVG(Mark) >= 40
Name Average
John 64
Mary 60
Jane 54
WHERE and HAVING
• WHERE refers to the
rows of tables, and
so cannot use
aggregate functions
• HAVING refers to the
groups of rows, and
so cannot use
columns which are
not in the GROUP BY
• Think of a query
being processed as
follows:
• Tables are combined
• WHERE clauses
• GROUP BY and
Aggregates
• Column selection
• HAVING clauses
• ORDER BY
UNION, etc.
• UNION, INTERSECT,
and EXCEPT
• These treat the tables
as sets and are the
usual set operators of
union, intersection,
and difference
• We’ll concentrate on
UNION
• Oracle has MINUS
instead of EXCEPT
• They all combine the
results from two
select statements
• The results of the
two selects must
have the same
columns and data
types
UNION
• Find, in a single
query, the average
mark for each
student, and the
average mark overall
Grades
Name Code Mark
Jane IAI 52
John DBS 56
John IAI 72
Mark PR1 43
Mark PR2 35
Mary DBS 60
UNION
• The average for each
student:
SELECT Name,
AVG(Mark) AS Average
FROM Grades
GROUP BY Name
• The average overall
SELECT
‘Total’ AS Name,
AVG(Mark) AS Average
FROM Grades
• Note - this has the
same columns as the
average by student
UNION
SELECT Name
AVG(Mark) AS Average
FROM Grades
GROUP BY Name
UNION
SELECT
'Total' as Name,
AVG(Mark) AS Average
FROM Grades
Name Average
Jane 52
John 64
Mark 39
Mary 60
Total 53
A Final Example
• Examiners’ reports
• We want a list of
students and their
average mark
• For first and second
years the average is
for that year
• For finalists it is 40%
of the second year
plus 60% of the final
year average.
• We want the results
• Sorted by year then
average mark (High
to low) then last
name, first name, and
finally ID
• To take into account
the number of credits
each module is worth
• Produced by a single
query
Tables for the Example
Student
ID First Last Year
Module
Code Title Credits
Grade
ID Code Mark YearTaken
We’ll Need a UNION
• Finalists are treated
differently
• Write one query for
the finalists
• Write a second query
for the first and
second years
• Use a UNION to join
them together
<QUERY FOR FINALISTS>
UNION
<QUERY FOR OTHERS>
We’ll need to Join the Tables
• Both of the
subqueries need
information from all
the tables
• The student ID, name
and year
• The marks for each
module and the year
taken
• The number of credits
for each module
• This is a natural join
operation
• We could use a
NATURAL JOIN
statement, and hope
that our version of
SQL can do it
• Safer to just use a
WHERE clause
The Query So Far
SELECT <some information>
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID
AND Module.Code = Grade.Code
AND <student is in third year>
UNION
SELECT <some information>
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID
AND Module.Code = Grade.Code
AND <student is in first or second year>
Information for Finalists
•We need to retrieve
• Compute average
mark, weighted 40-60
across years 2 and 3
• First year marks need
to be ignored
• The ID, Name, and
Year are needed as
they are used for
ordering
• The average is hard
• We don’t have any
statement to separate
years 2 and 3 easily
• We can exploit the
fact that 40 = 20*2
and 60 = 20*3, so
YearTaken and the
weighting have a
simple relationship
Information for Finalists
SELECT Year, Student.ID, Last, First,
SUM((20*YearTaken/100)*Mark*Credits)/120
AS AverageMark
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID
AND Module.Code = Grade.Code
AND YearTaken IN (2,3)
AND Year = 3
GROUP BY Year, Student.ID, First, Last
Information for Other
Students
• Other students are easier than finalists
• We just need to average their marks where
YearTaken and Year are the same
• As before we need the ID, Name, and Year
for ordering
Information for Other
Students
SELECT Year, Student.ID, Last, First,
SUM(Mark*Credits)/120 AS AverageMark
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID
AND Module.Code = Grade.Code
AND YearTaken = Year
AND Year IN (1,2)
GROUP BY Year, Student.ID, First, Last
The Final Query
SELECT Year, Student.ID, Last, First,
SUM((20*YearTaken/100)*Mark*Credits)/120 AS AverageMark
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code
AND YearTaken IN (2,3) AND Year = 3
GROUP BY Year, Student.ID, First, Last
UNION
SELECT Year, Student.ID, Last, First,
SUM(Mark*Credits)/120 AS AverageMark
FROM Student, Module, Grade
WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code
AND YearTaken = Year AND Year IN (1,2)
GROUP BY Year, Student.ID, First, Last
ORDER BY Year desc, AverageMark desc, First, Last, ID
Yet More SQL SELECT
Next Lecture
• Missing Information
• NULLs and three-valued logic
• NULLs and the relational model
• OUTER JOINs
• Default values
• For more information
• Ullman and Widom 6.1.5, 6.1.6, 6.3.8

More Related Content

Similar to Aggregate functions in sql

unit01-Activity-SQL-Query-Review-1.pptx
unit01-Activity-SQL-Query-Review-1.pptxunit01-Activity-SQL-Query-Review-1.pptx
unit01-Activity-SQL-Query-Review-1.pptx
AnesElmouaddeb
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
Edureka!
 
Dbms
DbmsDbms
Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL
MuhammadWaheed44
 
Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)
Zohar Elkayam
 
DML using oracle
 DML using oracle DML using oracle
DML using oracle
Farhan Aslam
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
Product School
 
Foundations of Data Analytics
Foundations of Data AnalyticsFoundations of Data Analytics
Foundations of Data Analytics
mrichards1
 
Comm202 - Lecture One
Comm202 - Lecture OneComm202 - Lecture One
Comm202 - Lecture One
Victoria Welsby
 
Oracle Database Advanced Querying
Oracle Database Advanced QueryingOracle Database Advanced Querying
Oracle Database Advanced Querying
Zohar Elkayam
 
cpp-2013 #15 Databases
cpp-2013 #15 Databasescpp-2013 #15 Databases
cpp-2013 #15 Databases
Amazon Web Services
 
Office 2013 – myitlabgrader – InstructionsA_MIS_301MIS 301.docx
Office 2013 – myitlabgrader – InstructionsA_MIS_301MIS 301.docxOffice 2013 – myitlabgrader – InstructionsA_MIS_301MIS 301.docx
Office 2013 – myitlabgrader – InstructionsA_MIS_301MIS 301.docx
cherishwinsland
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
Amrit Kaur
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ssuser0562f1
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
ssuser0562f1
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
Zohar Elkayam
 
Database_Systems_Lab_5_Presentation.pptx
Database_Systems_Lab_5_Presentation.pptxDatabase_Systems_Lab_5_Presentation.pptx
Database_Systems_Lab_5_Presentation.pptx
khaqan2
 
An hour with Database and SQL
An hour with Database and SQLAn hour with Database and SQL
An hour with Database and SQL
Iraj Hedayati
 
SQL
SQLSQL

Similar to Aggregate functions in sql (20)

unit01-Activity-SQL-Query-Review-1.pptx
unit01-Activity-SQL-Query-Review-1.pptxunit01-Activity-SQL-Query-Review-1.pptx
unit01-Activity-SQL-Query-Review-1.pptx
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
 
Dbms
DbmsDbms
Dbms
 
Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL
 
Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)
 
DML using oracle
 DML using oracle DML using oracle
DML using oracle
 
Intro to SQL for Beginners
Intro to SQL for BeginnersIntro to SQL for Beginners
Intro to SQL for Beginners
 
Foundations of Data Analytics
Foundations of Data AnalyticsFoundations of Data Analytics
Foundations of Data Analytics
 
Comm202 - Lecture One
Comm202 - Lecture OneComm202 - Lecture One
Comm202 - Lecture One
 
Oracle Database Advanced Querying
Oracle Database Advanced QueryingOracle Database Advanced Querying
Oracle Database Advanced Querying
 
cpp-2013 #15 Databases
cpp-2013 #15 Databasescpp-2013 #15 Databases
cpp-2013 #15 Databases
 
Office 2013 – myitlabgrader – InstructionsA_MIS_301MIS 301.docx
Office 2013 – myitlabgrader – InstructionsA_MIS_301MIS 301.docxOffice 2013 – myitlabgrader – InstructionsA_MIS_301MIS 301.docx
Office 2013 – myitlabgrader – InstructionsA_MIS_301MIS 301.docx
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic FunctionsExploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
 
Database_Systems_Lab_5_Presentation.pptx
Database_Systems_Lab_5_Presentation.pptxDatabase_Systems_Lab_5_Presentation.pptx
Database_Systems_Lab_5_Presentation.pptx
 
An hour with Database and SQL
An hour with Database and SQLAn hour with Database and SQL
An hour with Database and SQL
 
SQL
SQLSQL
SQL
 

More from bharati k

La &amp; edm in practice
La &amp; edm in practiceLa &amp; edm in practice
La &amp; edm in practice
bharati k
 
Application deployment for iot
Application deployment for iotApplication deployment for iot
Application deployment for iot
bharati k
 
Smart city1
Smart city1Smart city1
Smart city1
bharati k
 
Bi and big data
Bi and big dataBi and big data
Bi and big data
bharati k
 
Big data challenges for e governance
Big data challenges for e governanceBig data challenges for e governance
Big data challenges for e governance
bharati k
 
Comparison between rdbms and nosql
Comparison between rdbms and nosqlComparison between rdbms and nosql
Comparison between rdbms and nosql
bharati k
 
01072013 e governance
01072013 e governance01072013 e governance
01072013 e governance
bharati k
 
Mof
MofMof

More from bharati k (8)

La &amp; edm in practice
La &amp; edm in practiceLa &amp; edm in practice
La &amp; edm in practice
 
Application deployment for iot
Application deployment for iotApplication deployment for iot
Application deployment for iot
 
Smart city1
Smart city1Smart city1
Smart city1
 
Bi and big data
Bi and big dataBi and big data
Bi and big data
 
Big data challenges for e governance
Big data challenges for e governanceBig data challenges for e governance
Big data challenges for e governance
 
Comparison between rdbms and nosql
Comparison between rdbms and nosqlComparison between rdbms and nosql
Comparison between rdbms and nosql
 
01072013 e governance
01072013 e governance01072013 e governance
01072013 e governance
 
Mof
MofMof
Mof
 

Recently uploaded

Econ3060_Screen Time and Success_ final_GroupProject.pdf
Econ3060_Screen Time and Success_ final_GroupProject.pdfEcon3060_Screen Time and Success_ final_GroupProject.pdf
Econ3060_Screen Time and Success_ final_GroupProject.pdf
blueshagoo1
 
[VCOSA] Monthly Report - Cotton & Yarn Statistics May 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics May 2024[VCOSA] Monthly Report - Cotton & Yarn Statistics May 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics May 2024
Vietnam Cotton & Spinning Association
 
06-20-2024-AI Camp Meetup-Unstructured Data and Vector Databases
06-20-2024-AI Camp Meetup-Unstructured Data and Vector Databases06-20-2024-AI Camp Meetup-Unstructured Data and Vector Databases
06-20-2024-AI Camp Meetup-Unstructured Data and Vector Databases
Timothy Spann
 
Telemetry Solution for Gaming (AWS Summit'24)
Telemetry Solution for Gaming (AWS Summit'24)Telemetry Solution for Gaming (AWS Summit'24)
Telemetry Solution for Gaming (AWS Summit'24)
GeorgiiSteshenko
 
8 things to know before you start to code in 2024
8 things to know before you start to code in 20248 things to know before you start to code in 2024
8 things to know before you start to code in 2024
ArianaRamos54
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
z6osjkqvd
 
Cell The Unit of Life for NEET Multiple Choice Questions.docx
Cell The Unit of Life for NEET Multiple Choice Questions.docxCell The Unit of Life for NEET Multiple Choice Questions.docx
Cell The Unit of Life for NEET Multiple Choice Questions.docx
vasanthatpuram
 
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
hyfjgavov
 
一比一原版雷丁大学毕业证(UoR毕业证书)学历如何办理
一比一原版雷丁大学毕业证(UoR毕业证书)学历如何办理一比一原版雷丁大学毕业证(UoR毕业证书)学历如何办理
一比一原版雷丁大学毕业证(UoR毕业证书)学历如何办理
mbawufebxi
 
一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理
zsafxbf
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
bmucuha
 
一比一原版斯威本理工大学毕业证(swinburne毕业证)如何办理
一比一原版斯威本理工大学毕业证(swinburne毕业证)如何办理一比一原版斯威本理工大学毕业证(swinburne毕业证)如何办理
一比一原版斯威本理工大学毕业证(swinburne毕业证)如何办理
actyx
 
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
oaxefes
 
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
mkkikqvo
 
Build applications with generative AI on Google Cloud
Build applications with generative AI on Google CloudBuild applications with generative AI on Google Cloud
Build applications with generative AI on Google Cloud
Márton Kodok
 
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
agdhot
 
社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .
NABLAS株式会社
 
Data Scientist Machine Learning Profiles .pdf
Data Scientist Machine Learning  Profiles .pdfData Scientist Machine Learning  Profiles .pdf
Data Scientist Machine Learning Profiles .pdf
Vineet
 
A gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented GenerationA gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented Generation
dataschool1
 
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCAModule 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
yuvarajkumar334
 

Recently uploaded (20)

Econ3060_Screen Time and Success_ final_GroupProject.pdf
Econ3060_Screen Time and Success_ final_GroupProject.pdfEcon3060_Screen Time and Success_ final_GroupProject.pdf
Econ3060_Screen Time and Success_ final_GroupProject.pdf
 
[VCOSA] Monthly Report - Cotton & Yarn Statistics May 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics May 2024[VCOSA] Monthly Report - Cotton & Yarn Statistics May 2024
[VCOSA] Monthly Report - Cotton & Yarn Statistics May 2024
 
06-20-2024-AI Camp Meetup-Unstructured Data and Vector Databases
06-20-2024-AI Camp Meetup-Unstructured Data and Vector Databases06-20-2024-AI Camp Meetup-Unstructured Data and Vector Databases
06-20-2024-AI Camp Meetup-Unstructured Data and Vector Databases
 
Telemetry Solution for Gaming (AWS Summit'24)
Telemetry Solution for Gaming (AWS Summit'24)Telemetry Solution for Gaming (AWS Summit'24)
Telemetry Solution for Gaming (AWS Summit'24)
 
8 things to know before you start to code in 2024
8 things to know before you start to code in 20248 things to know before you start to code in 2024
8 things to know before you start to code in 2024
 
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
一比一原版英属哥伦比亚大学毕业证(UBC毕业证书)学历如何办理
 
Cell The Unit of Life for NEET Multiple Choice Questions.docx
Cell The Unit of Life for NEET Multiple Choice Questions.docxCell The Unit of Life for NEET Multiple Choice Questions.docx
Cell The Unit of Life for NEET Multiple Choice Questions.docx
 
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
一比一原版兰加拉学院毕业证(Langara毕业证书)学历如何办理
 
一比一原版雷丁大学毕业证(UoR毕业证书)学历如何办理
一比一原版雷丁大学毕业证(UoR毕业证书)学历如何办理一比一原版雷丁大学毕业证(UoR毕业证书)学历如何办理
一比一原版雷丁大学毕业证(UoR毕业证书)学历如何办理
 
一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理一比一原版莱斯大学毕业证(rice毕业证)如何办理
一比一原版莱斯大学毕业证(rice毕业证)如何办理
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
一比一原版斯威本理工大学毕业证(swinburne毕业证)如何办理
一比一原版斯威本理工大学毕业证(swinburne毕业证)如何办理一比一原版斯威本理工大学毕业证(swinburne毕业证)如何办理
一比一原版斯威本理工大学毕业证(swinburne毕业证)如何办理
 
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
一比一原版卡尔加里大学毕业证(uc毕业证)如何办理
 
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
原版一比一多伦多大学毕业证(UofT毕业证书)如何办理
 
Build applications with generative AI on Google Cloud
Build applications with generative AI on Google CloudBuild applications with generative AI on Google Cloud
Build applications with generative AI on Google Cloud
 
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
一比一原版加拿大麦吉尔大学毕业证(mcgill毕业证书)如何办理
 
社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .社内勉強会資料_Hallucination of LLMs               .
社内勉強会資料_Hallucination of LLMs               .
 
Data Scientist Machine Learning Profiles .pdf
Data Scientist Machine Learning  Profiles .pdfData Scientist Machine Learning  Profiles .pdf
Data Scientist Machine Learning Profiles .pdf
 
A gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented GenerationA gentle exploration of Retrieval Augmented Generation
A gentle exploration of Retrieval Augmented Generation
 
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCAModule 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
Module 1 ppt BIG DATA ANALYTICS_NOTES FOR MCA
 

Aggregate functions in sql

  • 1. Yet More SQL SELECT Database Systems Lecture 9 Natasha Alechina
  • 2. In This Lecture • Yet more SQL • ORDER BY • Aggregate functions • GROUP BY and HAVING • UNION etc. • For more information • Connoly and Begg Chapter 5 • Ullman and Widom Chapter 6.4
  • 3. SQL SELECT Overview SELECT [DISTINCT | ALL] <column-list> FROM <table-names> [WHERE <condition>] [ORDER BY <column-list>] [GROUP BY <column-list>] [HAVING <condition>] ([]- optional, | - or)
  • 4. ORDER BY • The ORDER BY clause sorts the results of a query • You can sort in ascending (default) or descending order • Multiple columns can be given SELECT <columns> FROM <tables> WHERE <condition> ORDER BY <cols> [ASCENDING | DESCENDING| ASC | DESC ]
  • 5. ORDER BY Example Grades Name Code Mark John DBS 56 John IAI 72 Mary DBS 60 Mark PR1 43 Mark PR2 35 Jane IAI 54 Name Code Mark Mark PR2 35 Mark PR1 43 Jane IAI 54 John DBS 56 Mary DBS 60 John IAI 72 SELECT * FROM Grades ORDER BY Mark
  • 6. ORDER BY Example Grades Name Code Mark John DBS 56 John IAI 72 Mary DBS 60 Mark PR1 43 Mark PR2 35 Jane IAI 54 Name Code Mark Mary DBS 60 John DBS 56 John IAI 72 Jane IAI 54 Mark PR1 43 Mark PR2 35 SELECT * FROM Grades ORDER BY Code ASC, Mark DESC
  • 7. Constants and Arithmetic • As well as column names, you can select constants, compute arithmetic expressions and evaluate functions in a SELECT statement SELECT Mark/100 FROM Grades SELECT Salary + Bonus FROM Employee SELECT 1.175*Price FROM Products
  • 8. Aggregate Functions • Aggregate functions compute summaries of data in a table • Most aggregate functions (all except COUNT) work on a single column of numeric data • Use an alias to name the result • Aggregate functions • COUNT: The number of rows • SUM: The sum of the entries in a column • AVG: The average entry in a column • MIN, MAX: The minimum and maximum entries in a column
  • 9. Aggregate Functions Grades Name Code Mark John DBS 56 John IAI 72 Mary DBS 60 Mark PR1 43 Mark PR2 35 Jane IAI 54 SELECT COUNT(*) AS Count FROM Grades SELECT SUM(Mark) AS Total FROM Grades SELECT MAX(Mark) AS Best FROM Grades Count 6 Total 320 Best 72
  • 10. Aggregate Functions • You can combine aggregate functions using arithmetic SELECT MAX(Mark)-MIN(Mark) AS Range FROM Grades Grades Name Code Mark John DBS 56 John IAI 72 Mary DBS 60 Mark PR1 43 Mark PR2 35 Jane IAI 54 Range 37 MAX(Mark) = 72 MIN(Mark) = 35
  • 11. Example • Find John’s average mark, weighted by the credits of each module Modules Code Title Credits DBS Database Sys. 10 GRP Group Project 20 PRG Programming 10 Grades Name Code Mark John DBS 60 Mark GRP 47 Mary PRG 56 SELECT SUM(Mark*Credits)/SUM(Credits) FROM Modules, Grades WHERE Modules.Code=Grades.Code AND Grades.Name = ‘John’
  • 12. GROUP BY • Sometimes we want to apply aggregate functions to groups of rows • Example, find the average mark of each student • The GROUP BY clause does this SELECT <cols1> FROM <tables> GROUP BY <cols2>
  • 13. GROUP BY SELECT <cols1> FROM <tables> GROUP BY <cols2> • Every entry in <cols1> must be in <cols2>, be a constant, or be an aggregate function • You can have WHERE and ORDER BY clauses as well as a GROUP BY clause
  • 14. GROUP BY Grades Name Code Mark John DBS 56 John IAI 72 Mary DBS 60 Mark PR1 43 Mark PR2 35 Jane IAI 54 SELECT Name, AVG(Mark) AS Average FROM Grades GROUP BY Name Name Average John 64 Mary 60 Mark 39 Jane 54
  • 15. GROUP BY • Find the total value of the sales for each department in each month • Can group by Month then Department or Department then Month • Same results, but in a different order Month Department Value March Fiction 20 March Travel 30 March Technical 40 April Fiction 10 April Fiction 30 April Travel 25 April Fiction 20 May Fiction 20 May Technical 50 Sales
  • 16. GROUP BY Month Department Total April Fiction 60 April Travel 25 March Fiction 20 March Technical 40 March Travel 30 May Fiction 20 May Technical 50 SELECT Month, Department, SUM(Value) AS Total FROM Sales GROUP BY Month, Department Month Department Total April Fiction 60 March Fiction 20 May Fiction 20 March Technical 40 May Technical 50 April Travel 25 March Travel 30 SELECT Month, Department, SUM(Value) AS Total FROM Sales GROUP BY Department, Month
  • 17. HAVING • HAVING is like a WHERE clause, except that it applies to the results of a GROUP BY query • It can be used to select groups which satisfy a given condition SELECT Name, AVG(Mark) AS Average FROM Grades GROUP BY Name HAVING AVG(Mark) >= 40 Name Average John 64 Mary 60 Jane 54
  • 18. WHERE and HAVING • WHERE refers to the rows of tables, and so cannot use aggregate functions • HAVING refers to the groups of rows, and so cannot use columns which are not in the GROUP BY • Think of a query being processed as follows: • Tables are combined • WHERE clauses • GROUP BY and Aggregates • Column selection • HAVING clauses • ORDER BY
  • 19. UNION, etc. • UNION, INTERSECT, and EXCEPT • These treat the tables as sets and are the usual set operators of union, intersection, and difference • We’ll concentrate on UNION • Oracle has MINUS instead of EXCEPT • They all combine the results from two select statements • The results of the two selects must have the same columns and data types
  • 20. UNION • Find, in a single query, the average mark for each student, and the average mark overall Grades Name Code Mark Jane IAI 52 John DBS 56 John IAI 72 Mark PR1 43 Mark PR2 35 Mary DBS 60
  • 21. UNION • The average for each student: SELECT Name, AVG(Mark) AS Average FROM Grades GROUP BY Name • The average overall SELECT ‘Total’ AS Name, AVG(Mark) AS Average FROM Grades • Note - this has the same columns as the average by student
  • 22. UNION SELECT Name AVG(Mark) AS Average FROM Grades GROUP BY Name UNION SELECT 'Total' as Name, AVG(Mark) AS Average FROM Grades Name Average Jane 52 John 64 Mark 39 Mary 60 Total 53
  • 23. A Final Example • Examiners’ reports • We want a list of students and their average mark • For first and second years the average is for that year • For finalists it is 40% of the second year plus 60% of the final year average. • We want the results • Sorted by year then average mark (High to low) then last name, first name, and finally ID • To take into account the number of credits each module is worth • Produced by a single query
  • 24. Tables for the Example Student ID First Last Year Module Code Title Credits Grade ID Code Mark YearTaken
  • 25. We’ll Need a UNION • Finalists are treated differently • Write one query for the finalists • Write a second query for the first and second years • Use a UNION to join them together <QUERY FOR FINALISTS> UNION <QUERY FOR OTHERS>
  • 26. We’ll need to Join the Tables • Both of the subqueries need information from all the tables • The student ID, name and year • The marks for each module and the year taken • The number of credits for each module • This is a natural join operation • We could use a NATURAL JOIN statement, and hope that our version of SQL can do it • Safer to just use a WHERE clause
  • 27. The Query So Far SELECT <some information> FROM Student, Module, Grade WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code AND <student is in third year> UNION SELECT <some information> FROM Student, Module, Grade WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code AND <student is in first or second year>
  • 28. Information for Finalists •We need to retrieve • Compute average mark, weighted 40-60 across years 2 and 3 • First year marks need to be ignored • The ID, Name, and Year are needed as they are used for ordering • The average is hard • We don’t have any statement to separate years 2 and 3 easily • We can exploit the fact that 40 = 20*2 and 60 = 20*3, so YearTaken and the weighting have a simple relationship
  • 29. Information for Finalists SELECT Year, Student.ID, Last, First, SUM((20*YearTaken/100)*Mark*Credits)/120 AS AverageMark FROM Student, Module, Grade WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code AND YearTaken IN (2,3) AND Year = 3 GROUP BY Year, Student.ID, First, Last
  • 30. Information for Other Students • Other students are easier than finalists • We just need to average their marks where YearTaken and Year are the same • As before we need the ID, Name, and Year for ordering
  • 31. Information for Other Students SELECT Year, Student.ID, Last, First, SUM(Mark*Credits)/120 AS AverageMark FROM Student, Module, Grade WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code AND YearTaken = Year AND Year IN (1,2) GROUP BY Year, Student.ID, First, Last
  • 32. The Final Query SELECT Year, Student.ID, Last, First, SUM((20*YearTaken/100)*Mark*Credits)/120 AS AverageMark FROM Student, Module, Grade WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code AND YearTaken IN (2,3) AND Year = 3 GROUP BY Year, Student.ID, First, Last UNION SELECT Year, Student.ID, Last, First, SUM(Mark*Credits)/120 AS AverageMark FROM Student, Module, Grade WHERE Student.ID = Grade.ID AND Module.Code = Grade.Code AND YearTaken = Year AND Year IN (1,2) GROUP BY Year, Student.ID, First, Last ORDER BY Year desc, AverageMark desc, First, Last, ID
  • 33. Yet More SQL SELECT Next Lecture • Missing Information • NULLs and three-valued logic • NULLs and the relational model • OUTER JOINs • Default values • For more information • Ullman and Widom 6.1.5, 6.1.6, 6.3.8