Download presentation
Presentation is loading. Please wait.
Published byDana Anderson Modified over 9 years ago
1
SQL queries ordering and grouping
2
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
3
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
4
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
5
RHS – SOC 5 SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie ORDER BY title
6
RHS – SOC 6 SQL query - ordering movieidtitlecountryprodyeargenreoscars 61984UK1984Sci-Fi2 1E.T.USA1982Sci-Fi4 5Hard BoiledHK1992Action0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 7SevenUSA1995Thriller1 2TaxiFrance1998Comedy0 SELECT * FROM Movie ORDER BY title
7
RHS – SOC 7 SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title
8
RHS – SOC 8 SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title
9
RHS – SOC 9 SQL query - ordering titleprodyear 1984 E.T.1982 Hunger1966 Seven1995 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title
10
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
11
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
12
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 Movie WHERE (oscars > 0) How many movies have won an Oscar
13
RHS – SOC 13 SQL query - functions This query can also be written as SELECT COUNT(title) AS OscarWinners FROM Movie WHERE (oscars > 0) The AS keyword allows us to rename a column in the search result Only cosmetic, but useful… NB!
14
RHS – SOC 14 SQL query - functions SELECTCOUNT(title) AS OscarWinners, AVG(oscars) AS averageOscars, MAX(oscars) AS maximalOscars FROMMovie WHERE(oscars > 0)
15
RHS – SOC 15 SQL query - functions OscarWinnersaverageOscarsmaximalOscars 42.00004
16
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
17
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
18
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 MovieCount FROM Movies WHERE country = ’…’ More than 100 queries…
19
RHS – SOC 19 SQL query - grouping A much easier way is to use GROUP BY Syntax: SELECT FROM GROUP BY Produces a result for each group, specified in the field list
20
RHS – SOC 20 SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, COUNT(title) AS MovieCount FROM Movie GROUP BY country
21
RHS – SOC 21 SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, COUNT(title) AS MovieCount FROM Movie GROUP BY country
22
RHS – SOC 22 SQL query - grouping CountryMovieCount Denmark1 France2 HK1 UK1 USA2
23
RHS – SOC 23 SQL query - grouping CountryOscarTotal Denmark1 France0 HK0 UK2 USA5 SUM(oscars) AS …
24
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
25
RHS – SOC 25 SQL query - grouping Syntax: SELECT FROM GROUP BY HAVING
26
RHS – SOC 26 SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, SUM(oscars) AS OscarTotal FROM Movie GROUP BY country HAVING(SUM(oscars) > 0)
27
RHS – SOC 27 SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, SUM(oscars) AS OscarTotal FROM Movie GROUP BY country HAVING(SUM(oscars) > 0)
28
RHS – SOC 28 SQL query - grouping CountryOscarTotal USA5 France0 UK2 HK0 Denmark1
29
RHS – SOC 29 SQL query - grouping CountryOscarTotal USA5 UK2 Denmark1
30
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
31
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.