SQL queries ordering and grouping
SWC – 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
SWC – 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
SWC – 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
SWC – SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie ORDER BY title
SWC – SQL query - ordering movieidtitlecountryprodyeargenreoscars 61984UK1984Sci-Fi2 1E.T.USA1982Sci-Fi4 5Hard BoiledHK1992Action0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 7SevenUSA1995Thriller1 2TaxiFrance1998Comedy0 SELECT * FROM Movie ORDER BY title
SWC – SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title
SWC – SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title
SWC – SQL query - ordering titleprodyear 1984 E.T.1982 Hunger1966 Seven1995 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title
SWC – 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
SWC – 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
SWC – 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
SWC – 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!
SWC – SQL query - functions SELECTCOUNT(title) AS OscarWinners, AVG(oscars) AS averageOscars, MAX(oscars) AS maximalOscars FROMMovie WHERE(oscars > 0)
SWC – SQL query - functions OscarWinnersaverageOscarsmaximalOscars
SWC – 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
SWC – 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…
SWC – 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
SWC – SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, COUNT(title) AS MovieCount FROM Movie GROUP BY country
SWC – SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, COUNT(title) AS MovieCount FROM Movie GROUP BY country
SWC – SQL query - grouping CountryMovieCount Denmark1 France2 HK1 UK1 USA2
SWC – SQL query - grouping CountryOscarTotal Denmark1 France0 HK0 UK2 USA5 SUM(oscars) AS …
SWC – 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
SWC – SQL query - grouping Syntax: SELECT FROM GROUP BY HAVING
SWC – SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, SUM(oscars) AS OscarTotal FROM Movie GROUP BY country HAVING(SUM(oscars) > 0)
SWC – SQL query - grouping movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT country, SUM(oscars) AS OscarTotal FROM Movie GROUP BY country HAVING(SUM(oscars) > 0)
SWC – SQL query - grouping CountryOscarTotal USA5 France0 UK2 HK0 Denmark1
SWC – SQL query - grouping CountryOscarTotal USA5 UK2 Denmark1
SWC – 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