Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "SQL queries ordering and grouping. SWC – 2011 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL."— Presentation transcript:

1 SQL queries ordering and grouping

2 SWC – 2011 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 SWC – 2011 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 SWC – 2011 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 SWC – 2011 5 SQL query - ordering movieidtitlecountryprodyeargenreoscars 1E.T.USA1982Sci-Fi4 2TaxiFrance1998Comedy0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 5Hard BoiledHK1992Action0 61984UK1984Sci-Fi2 7SevenUSA1995Thriller1 SELECT * FROM Movie ORDER BY title

6 SWC – 2011 6 SQL query - ordering movieidtitlecountryprodyeargenreoscars 61984UK1984Sci-Fi2 1E.T.USA1982Sci-Fi4 5Hard BoiledHK1992Action0 3HungerDenmark1966Drama1 4LeonFrance1994Thriller0 7SevenUSA1995Thriller1 2TaxiFrance1998Comedy0 SELECT * FROM Movie ORDER BY title

7 SWC – 2011 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 SWC – 2011 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 SWC – 2011 9 SQL query - ordering titleprodyear 1984 E.T.1982 Hunger1966 Seven1995 SELECT title, prodyear FROM Movie WHEREoscars > 0 ORDER BY title

10 SWC – 2011 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 SWC – 2011 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 SWC – 2011 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 SWC – 2011 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 SWC – 2011 14 SQL query - functions SELECTCOUNT(title) AS OscarWinners, AVG(oscars) AS averageOscars, MAX(oscars) AS maximalOscars FROMMovie WHERE(oscars > 0)

15 SWC – 2011 15 SQL query - functions OscarWinnersaverageOscarsmaximalOscars 42.00004

16 SWC – 2011 16 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

17 SWC – 2011 17 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…

18 SWC – 2011 18 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

19 SWC – 2011 19 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

20 SWC – 2011 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 SWC – 2011 21 SQL query - grouping CountryMovieCount Denmark1 France2 HK1 UK1 USA2

22 SWC – 2011 22 SQL query - grouping CountryOscarTotal Denmark1 France0 HK0 UK2 USA5 SUM(oscars) AS …

23 SWC – 2011 23 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

24 SWC – 2011 24 SQL query - grouping Syntax: SELECT FROM GROUP BY HAVING

25 SWC – 2011 25 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)

26 SWC – 2011 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 SWC – 2011 27 SQL query - grouping CountryOscarTotal USA5 France0 UK2 HK0 Denmark1

28 SWC – 2011 28 SQL query - grouping CountryOscarTotal USA5 UK2 Denmark1

29 SWC – 2011 29 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


Download ppt "SQL queries ordering and grouping. SWC – 2011 2 SQL query - ordering In a query producing a large result, it may be beneficial to order the result SQL."

Similar presentations


Ads by Google