Top Banner
SQL queries ordering and grouping
31

SQL queries ordering and grouping. RHS SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

Jan 18, 2018

Download

Documents

Dana Anderson

RHS – SOC 3 SQL query - ordering SELECT FROM WHERE ORDER BY Which fields do I want From what table do I want the fields What conditions must the fields fulfill What order are the results sorted in
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

SQL queriesordering and grouping

Page 2: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 2

SQL query - ordering

• In a query producing a large result, it may be beneficial to order the result

• SQL allows us to order the result by any of the fields in the result

• We use the keyword ORDER BY

Page 3: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 3

SQL query - ordering

SELECT <fieldlist>FROM <tablename>WHERE <condition>ORDER BY <fieldname>

Which fields do I want

From what table do I want the fields

What conditions must the fields fulfill

What order are the results sorted in

Page 4: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 4

SQL query - ordering

• We use a movie information database as example

Movie movieid title country prodyear genre oscars

Actor actorid name country birth living oscars

Casting movieid actorid

Page 5: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 5

SQL query - orderingmovieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT *FROM MovieORDER BY title

Page 6: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 6

SQL query - orderingmovieid title country prodyear genre oscars

6 1984 UK 1984 Sci-Fi 2

1 E.T. USA 1982 Sci-Fi 4

5 Hard Boiled HK 1992 Action 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

7 Seven USA 1995 Thriller 1

2 Taxi France 1998 Comedy 0

SELECT *FROM MovieORDER BY title

Page 7: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 7

SQL query - orderingmovieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT title, prodyearFROM MovieWHERE oscars > 0ORDER BY title

Page 8: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 8

SQL query - orderingmovieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT title, prodyearFROM MovieWHERE oscars > 0ORDER BY title

Page 9: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 9

SQL query - orderingtitle prodyear

1984 1984

E.T. 1982

Hunger 1966

Seven 1995 SELECT title, prodyearFROM MovieWHERE oscars > 0ORDER BY title

Page 10: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 10

SQL query - ordering

• We can even specifiy more than one field for ordering – secondary fields used if primary fields are identical

• We can choose between descending and ascending order, using the keywords DESC and ASC, respectively

ORDER BY oscars DESC, prodyear ASC

Page 11: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 11

SQL query - functions

• We can even do some (simple) arithmetic in SQL, using a basic set of functions– COUNT– SUM– AVG– MIN– MAX

• These are called aggregate functions

Page 12: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 12

SQL query - functions

• A aggregate function works on the values of a specific field (column)

• The set of values is determined by the search conditions

SELECT COUNT(title)FROM MovieWHERE (oscars > 0)

How many movies have won an Oscar

Page 13: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 13

SQL query - functions

• This query can also be written asSELECT COUNT(title) AS OscarWinnersFROM MovieWHERE (oscars > 0)

• The AS keyword allows us to rename a column in the search result

• Only cosmetic, but useful…

NB!

Page 14: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 14

SQL query - functions

SELECT COUNT(title) AS OscarWinners,

AVG(oscars) AS averageOscars,

MAX(oscars) AS maximalOscars

FROM MovieWHERE (oscars > 0)

Page 15: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 15

SQL query - functions

OscarWinners averageOscars maximalOscars

4 2.0000 4

Page 16: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 16

Exercise 4 – SQL queries• Use the MovieInformation database, defined in exercise 1• With the data in place, run the below queries on the database

– SELECT * FROM Movie ORDER BY prodyear ASC– SELECT title, prodyear FROM Movie ORDER BY title– SELECT MAX(prodyear) AS maxUSA FROM Movie WHERE country = ’USA’– SELECT AVG(prodyear) AS loserYear FROM Movie WHERE (oscars = 0)

• Now formulate queries yourself, in order to retrieve the below data:– Get all oscar-winning movies ordered by year (oldest movie first)– Get a sorted list of movie title for movies made in France– Get the year for the oldest movie not winning any Oscars– Get the average number of Oscars for Sci-Fi movies– Get the total number of Oscars won by movies from USA

Page 17: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 17

SQL query - grouping

• The aggregate functions are good for calculating properties for the entire result of the search

• We may sometimes wish to find proper-ties for a certain group within the result

• This can be done using WHERE…• …but can be cumbersome if the groups

are very numerous

Page 18: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 18

SQL query - grouping

• Suppose we have a full movie database, with movies for more than 100 countries

• ”Find the total number of movies made in each country”

SELECT COUNT(title) AS MovieCountFROM MoviesWHERE country = ’…’

• More than 100 queries…

Page 19: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 19

SQL query - grouping

• A much easier way is to use GROUP BY• Syntax:

SELECT <fieldlist>FROM <tablename>GROUP BY <fieldlist>

• Produces a result for each group, specified in the field list

Page 20: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 20

SQL query - groupingmovieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT country,COUNT(title) AS MovieCount

FROM MovieGROUP BY country

Page 21: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 21

SQL query - groupingmovieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT country,COUNT(title) AS MovieCount

FROM MovieGROUP BY country

Page 22: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 22

SQL query - groupingCountry MovieCount

Denmark 1

France 2

HK 1

UK 1

USA 2

Page 23: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 23

SQL query - groupingCountry OscarTotal

Denmark 1

France 0

HK 0

UK 2

USA 5

SUM(oscars) AS …

Page 24: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 24

SQL query - grouping

• In the last example, it might be desirable to leave out results where total is zero

• In general; only include groups which fulfill some criteria

• This can be done using the HAVING keyword

Page 25: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 25

SQL query - grouping

• Syntax:SELECT <fieldlist>FROM <tablename>GROUP BY <fieldlist>HAVING <criteria>

Page 26: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 26

SQL query - groupingmovieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT country,SUM(oscars) AS OscarTotal

FROM MovieGROUP BY countryHAVING (SUM(oscars) > 0)

Page 27: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 27

SQL query - groupingmovieid title country prodyear genre oscars

1 E.T. USA 1982 Sci-Fi 4

2 Taxi France 1998 Comedy 0

3 Hunger Denmark 1966 Drama 1

4 Leon France 1994 Thriller 0

5 Hard Boiled HK 1992 Action 0

6 1984 UK 1984 Sci-Fi 2

7 Seven USA 1995 Thriller 1

SELECT country,SUM(oscars) AS OscarTotal

FROM MovieGROUP BY countryHAVING (SUM(oscars) > 0)

Page 28: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 28

SQL query - groupingCountry OscarTotal

USA 5

France 0

UK 2

HK 0

Denmark 1

Page 29: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 29

SQL query - grouping

Country OscarTotal

USA 5

UK 2

Denmark 1

Page 30: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 30

SQL query - grouping

• But wait…• …isn’t HAVING the same as WHERE..?• Not quite

– WHERE is for filtering out specific records– HAVING is for filtering out specific groups

from the final result• We cannot use an aggregate function in a

WHERE clause

Page 31: SQL queries ordering and grouping. RHS  SOC 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL.

RHS – SOC 31

Exercise 5 – SQL queries• Use the MovieInformation database, defined in exercise 1• With the data in place, run the below queries on the database

– SELECT genre, AVG(oscars) FROM Movie GROUP BY genre– SELECT oscars, MAX(prodyear) FROM Movie WHERE (oscars > 0) GROUP BY

oscars– SELECT genre, AVG(oscars) FROM Movie GROUP BY genre HAVING

(COUNT(title) > 1)

• Now formulate queries yourself, in order to retrieve the below data:– Get a list of the total count of movies, grouped by country. Within each

contry group, the count should be grouped by genre– Get a list of the average number of oscars won by movies, grouped by

country– Get a list of the total count of movies, grouped by country. Only include

movies made after 1985