Sorting and filtering

Select the first three episodes from every season of “IT Crowd”, except the first season.

Note

We assume that you already created tables in step Creating a table and populated them with data in step Adding data to a table.

  1. SELECT
  2. series_id,
  3. season_id,
  4. episode_id,
  5. CAST(air_date AS Date) AS air_date,
  6. title
  7. FROM episodes
  8. WHERE
  9. series_id = 1 -- List of conditions to build the result
  10. AND season_id > 1 -- Logical AND is used for complex conditions
  11. ORDER BY -- Sorting the results.
  12. series_id, -- ORDER BY sorts the values by one or multiple
  13. season_id, -- columns. Columns are separated by commas.
  14. episode_id
  15. LIMIT 3 -- LIMIT N after ORDER BY means
  16. -- "get top N" or "get bottom N" results,
  17. ; -- depending on sort order.
  18. COMMIT;

Sorting and filtering - 图1