Docsity

Database Systems: ORDER BY, Constants and Arithmetic, Aggregate Functions, and GROUP BY, Slides of Introduction to Database Management Systems

Toggle sidebar
bg1
1
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
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 ]
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
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 PR 1 43
Jane IAI 54
John DBS 56
Mary DBS 60
John IAI 72
SELECT * FROM Grades
ORDER BY Mark
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
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 PR 1 43
Mark PR2 35
SELECT * FROM Grades
ORDER BY Code ASC,
Mark DESC
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
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
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
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
G52DBS – Database Systems
www.cs.nott.ac.uk/~smx/DBS
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
pf3
pf4
pf5

Partial preview of the text

Download Database Systems: ORDER BY, Constants and Arithmetic, Aggregate Functions, and GROUP BY and more Slides Introduction to Database Management Systems in PDF only on Docsity!

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

FROM

WHERE

ORDER BY

[ASCENDING |

DESCENDING|

ASC | DESC ]

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Constants and Arithmetic

  • As well as column

names, you can

select constants,

compute arithmetic

expressions and

evaluate functions in

a SELECT statement

SELECT Mark/ FROM Grades

SELECT

Salary + Bonus FROM Employee

SELECT 1.175Price FROM Products*

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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(MarkCredits)/SUM(Mark) FROM Modules, Grades WHERE Modules.Code=Grades.Code AND Grades.Name = ‘John’*

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

FROM

GROUP BY

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

GROUP BY

SELECT

FROM

GROUP BY

  • Every entry in

must be in

, be a

constant, or be an

aggregate function

  • You can have WHERE

or ORDER BY clauses

as well as a GROUP

BY clause

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Tables for the Example

Student

ID First Last Year

Module

Code Title Credits

Grade

ID Code Mark YearTaken

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

UNION

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

The Query So Far

SELECT

FROM Student, Module, Grade

WHERE Student.ID = Grade.ID

AND Module.Code = Grade.Code

AND

UNION

SELECT

FROM Student, Module, Grade

WHERE Student.ID = Grade.ID

AND Module.Code = Grade.Code

AND

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Information for Finalists

•We need to retrieve

  • Compute average mark, weighted 40- 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* and 60 = 20*3, so YearTaken and the weighting have a simple relationship

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

Information for Finalists

SELECT Year, Student.ID, Last, First,

SUM((20YearTaken/100)Mark*Credits)/

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

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

G52DBS – Database Systems www.cs.nott.ac.uk/~smx/DBS

The Final Query

SELECT Year, Student.ID, Last, First, SUM((20YearTaken/100)MarkCredits)/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(MarkCredits)/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