SlideShare a Scribd company logo
1 of 21
Yet More SQL SELECT
Database Systems
In This Lecture
• Yet more SQL
• ORDER BY
• Aggregate functions
• GROUP BY and HAVING
• UNION etc.
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
• You cannot order by a
column which isn’t in
the result
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
or 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

More Related Content

What's hot

oracle Sql constraint
oracle  Sql constraint oracle  Sql constraint
oracle Sql constraint
home
 

What's hot (20)

SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
oracle Sql constraint
oracle  Sql constraint oracle  Sql constraint
oracle Sql constraint
 
Database constraints
Database constraintsDatabase constraints
Database constraints
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
set operators.pptx
set operators.pptxset operators.pptx
set operators.pptx
 
integrity constraints
integrity constraintsintegrity constraints
integrity constraints
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
SQL commands
SQL commandsSQL commands
SQL commands
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
DML Commands
DML CommandsDML Commands
DML Commands
 
joins and subqueries in big data analysis
joins and subqueries in big data analysisjoins and subqueries in big data analysis
joins and subqueries in big data analysis
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
Types Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql ServerTypes Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql Server
 

Similar to SQL Queries

Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
HAMEEDHUSSAINBU21CSE
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
Tony Wong
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
Ashwin Dinoriya
 

Similar to SQL Queries (20)

Aggregate functions in sql
Aggregate functions in sqlAggregate functions in sql
Aggregate functions in sql
 
Sql server 2016 queries
Sql server 2016 queriesSql server 2016 queries
Sql server 2016 queries
 
DB2 Sql Query
DB2 Sql QueryDB2 Sql Query
DB2 Sql Query
 
Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)Oracle Database Advanced Querying (2016)
Oracle Database Advanced Querying (2016)
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
SQL
SQLSQL
SQL
 
ADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database SystemsADBMS - Joins , Sorting , Distributed Database Systems
ADBMS - Joins , Sorting , Distributed Database Systems
 
Oracle Database Advanced Querying
Oracle Database Advanced QueryingOracle Database Advanced Querying
Oracle Database Advanced Querying
 
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptxUnit 3-Select Options and Aggregate Functions in SQL (1).pptx
Unit 3-Select Options and Aggregate Functions in SQL (1).pptx
 
Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)Database Systems - SQL - DDL Statements (Chapter 3/3)
Database Systems - SQL - DDL Statements (Chapter 3/3)
 
The art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniquesThe art of querying – newest and advanced SQL techniques
The art of querying – newest and advanced SQL techniques
 
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
 
SQL select clause
SQL select clauseSQL select clause
SQL select clause
 
MySQL basics
MySQL basicsMySQL basics
MySQL basics
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
Subqueries, Backups, Users and Privileges
Subqueries, Backups, Users and PrivilegesSubqueries, Backups, Users and Privileges
Subqueries, Backups, Users and Privileges
 
Oracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic FunctionsOracle Advanced SQL and Analytic Functions
Oracle Advanced SQL and Analytic Functions
 
Data Retrival
Data RetrivalData Retrival
Data Retrival
 
1. dml select statement reterive data
1. dml select statement reterive data1. dml select statement reterive data
1. dml select statement reterive data
 

More from Nilt1234 (13)

Lisp tutorial
Lisp tutorialLisp tutorial
Lisp tutorial
 
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISP
 
Lec 09
Lec 09Lec 09
Lec 09
 
relational algebra (joins)
relational algebra (joins)relational algebra (joins)
relational algebra (joins)
 
relational algebra-(basics)
 relational algebra-(basics) relational algebra-(basics)
relational algebra-(basics)
 
Artificial Intelligence
Artificial IntelligenceArtificial Intelligence
Artificial Intelligence
 
Lec 17
Lec  17Lec  17
Lec 17
 
Lec 06
Lec 06Lec 06
Lec 06
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
introduction of Database
introduction of Databaseintroduction of Database
introduction of Database
 
Database Architecture
Database Architecture Database Architecture
Database Architecture
 
What is Artificial Intelligence
What is Artificial IntelligenceWhat is Artificial Intelligence
What is Artificial Intelligence
 
Entity Relationship Diagaram
Entity Relationship DiagaramEntity Relationship Diagaram
Entity Relationship Diagaram
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

SQL Queries

  • 1. Yet More SQL SELECT Database Systems
  • 2. In This Lecture • Yet more SQL • ORDER BY • Aggregate functions • GROUP BY and HAVING • UNION etc.
  • 3. 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 • You cannot order by a column which isn’t in the result SELECT <columns> FROM <tables> WHERE <condition> ORDER BY <cols> [ASCENDING | DESCENDING| ASC | DESC ]
  • 4. 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
  • 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 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
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. 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
  • 10. 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’
  • 11. 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>
  • 12. 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 or ORDER BY clauses as well as a GROUP BY clause
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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