- Use
ORDER BYto sort SQL query results into a predictable order. - Choose
ASCorDESCcorrectly for text and number fields. - Use two-field sorting and range conditions with
>=and<=.
- I can write
ORDER BY field ASCandORDER BY field DESC. - I can explain which field is the primary sort and which field is the secondary sort.
- I can write a range condition using
>= low AND <= high. - [Extension] I can use
BETWEEN low AND highas a shortcut for the same range (not required for N5 exam).
Use the same films.sql database from DDD1. Open it in DataGrip before you start. If the database is missing, download the script and run it again.
Answer before the lesson begins. These check prior SQL knowledge from DDD5 and DDD6 — it's fine if you're unsure.
1. Which clause chooses the table a query reads from?
2. Which condition finds films with a rating of 8.0 or higher?
3. What does AND mean in a WHERE clause?
Key vocabulary
FROM and after any WHERE clause.ORDER BY. SQL sorts by this field before considering any others.BETWEEN 2000 AND 2009 means the same as >= 2000 AND <= 2009. Both boundary values are included. Not required for the N5 exam — use >= / <= instead.Sorting and ranges in SQL
Why sorting matters
A database table stores records, but the order you see on screen is not something you should rely on. It might look like records are in primary-key order, or in the order they were inserted, but SQL does not guarantee that unless you ask for it. When a question says "alphabetical order", "highest first", "oldest to newest", or "sort by", you need an ORDER BY clause.
ORDER BY is written at the end of a SELECT query. If the query has a WHERE clause, sorting happens after filtering: the database first finds the matching records, then arranges those matching records into the requested order.
FROM film
ORDER BY rating DESC;
This query displays every film, sorted by the rating field from highest to lowest. The keyword DESC means descending. For a number field, descending means largest value first. For a year field, descending means newest year first. For a text field, descending means reverse alphabetical order, from Z to A.
ASC and DESC
ASC means ascending. For numbers it means smallest to largest, for years it usually means oldest to newest, and for text it means A to Z. SQL uses ascending order by default if you do not write ASC or DESC, but in an exam answer it is safer to write the direction explicitly. It shows the marker that you understood the requested order.
FROM film
ORDER BY title ASC;
That query sorts film titles alphabetically. Be careful not to confuse sorting with filtering. ORDER BY does not remove any records. If the table has 52 films and there is no WHERE clause, the result still has 52 films — just arranged in a different order.
Two-field sorting
Sometimes one sort field is not enough. If several records share the same value, SQL needs a tiebreaker. You can sort by more than one field by separating the fields with a comma. The first field is the primary sort; the second field is the secondary sort.
FROM film
ORDER BY genre ASC, rating DESC;
This sorts the films by genre first, so all Action films appear together, all Adventure films appear together, and so on alphabetically. Within each genre group, rating DESC puts the highest-rated films first. The secondary sort does not sort the whole result by rating; it only breaks ties inside each genre group. This is one of the most common SQL misunderstandings at National 5.
Range conditions with >= and <=
To filter records within an inclusive range, use >= (greater than or equal to) and <= (less than or equal to) joined with AND. For example, to find films from the 2000s including both boundary years:
FROM film
WHERE year >= 2000 AND year <= 2009
ORDER BY year ASC;
Both 2000 and 2009 are included because of the = signs. This is the recommended form for N5 — it uses operators you already know from filtering lessons and is guaranteed to appear in the mark scheme.
BETWEEN is a shorthand for the same range condition. The query above could also be written as:
BETWEEN does not appear in the N5 appendix or in any past paper. If you use it in the exam, your answer may still be accepted — but to be safe, use >= AND <= instead. If you see BETWEEN in real-world SQL (e.g. in DataGrip), you now know what it means.
Combining filtering, ranges, and sorting
Real queries often use more than one idea. You might filter first, use a range, then sort the remaining records. The order of the SQL clauses stays consistent: SELECT, FROM, WHERE, then ORDER BY.
FROM film
WHERE genre = "Sci-Fi"
AND rating BETWEEN 8.0 AND 9.0
ORDER BY rating DESC, title ASC;
Read this in English as: show selected fields from the film table, keep only Sci-Fi films with ratings from 8.0 to 9.0 inclusive, then sort by rating highest first and use title alphabetically if two films have the same rating.
Worked examples
Task: Display the title, year, and rating for every film, sorted from highest rating to lowest.
title, year, and rating.FROM film. There is no WHERE clause because every film should appear.ORDER BY rating DESC, because highest to lowest is descending order.FROM film
ORDER BY rating DESC;
Verified result: the first three rows are The Dark Knight (9.0), Schindler's List (8.9), and Pulp Fiction (8.9). The two 8.9 films may appear in database order because no secondary sort was requested.
Task: Display each film's title, genre, and year. Sort by genre A to Z. Within each genre, sort newest films first.
genre ASC, because genre grouping is mentioned first.year DESC, because "newest first" means the largest year first inside each genre.ORDER BY, separated by a comma.FROM film
ORDER BY genre ASC, year DESC;
Verified result: the Action group starts first. Within Action, the first three films are The Dark Knight (2008), Batman Begins (2005), and Kill Bill: Vol. 1 (2003).
Task: Find films released between 2000 and 2009, including both 2000 and 2009. Show title and year, oldest first.
year field — use >= and <= so both boundary values are included.WHERE year >= 2000 AND year <= 2009.ORDER BY year ASC for oldest to newest.FROM film
WHERE year >= 2000 AND year <= 2009
ORDER BY year ASC;
Verified result: 16 records are returned. The first records are Memento and Gladiator from 2000, proving that the lower boundary is included. [Extension: this could also be written as WHERE year BETWEEN 2000 AND 2009 — identical result, but use >= / <= in the N5 exam.]
Task: Find Sci-Fi films with a rating between 8.0 and 9.0 inclusive. Sort by rating highest first, then title A to Z.
genre = "Sci-Fi". Text values need quotation marks.AND rating >= 8.0 AND rating <= 9.0. Both boundary values are included.FROM film
WHERE genre = "Sci-Fi"
AND rating >= 8.0 AND rating <= 9.0
ORDER BY rating DESC, title ASC;
Verified result: 7 records are returned, starting with Inception (8.8), then Interstellar (8.6), then Alien (8.4).
A teacher wants a list of Fantasy films from the films database. They want only films released between 2000 and 2010 inclusive. The list should show the highest-rated film first. If two films have the same rating, the titles should be alphabetical.
Answer the following:
- Which table and fields are needed?
- Which
WHEREconditions are needed? - Write the complete SQL query.
- Use the
filmtable. Displaytitle,year, andrating. - Use
genre = "Fantasy"andyear >= 2000 AND year <= 2010. - SELECT title, year, rating
FROM film
WHERE genre = "Fantasy"
AND year >= 2000 AND year <= 2010
ORDER BY rating DESC, title ASC;
Verified result: 6 records are returned. The first three are The Lord of the Rings: The Return of the King, The Lord of the Rings: The Fellowship of the Ring, and The Lord of the Rings: The Two Towers.
ORDER BY before WHERE. The correct order is SELECT, FROM, WHERE, then ORDER BY. Sorting comes after filtering.= in range conditions. year > 2000 excludes 2000 itself — only films from 2001 onwards appear. Use year >= 2000 to include the boundary value. The same applies to <= on the upper end.ORDER BY genre ASC, rating DESC sorts by genre first. It does not sort the whole result by rating first."Fantasy" need quotes. Number values such as 2000, 150, and 8.0 do not.In SQL questions, underline order words in the scenario before writing your answer. "alphabetical" usually means ASC, "highest first" means DESC, "newest first" means year DESC, and "between X and Y inclusive" means >= X AND <= Y — use this form in the exam. Put ORDER BY at the end of the query after WHERE.
Questions 1–5 are auto-checked. Questions 6–7 are self-marked SQL writing questions. Questions 8–9 are practical DataGrip tasks using the films database.
1. Which clause sorts query results? TYPE 1
2. What does ORDER BY year DESC do? TYPE 1
3. Which condition finds films released from 1990 to 1999 inclusive? TYPE 1
4. In ORDER BY genre ASC, rating DESC, what is the secondary sort? TYPE 1
5. Which full query displays all film titles alphabetically? TYPE 1
6. Write a SQL query to display title, runtime_mins, and rating for films longer than 150 minutes, sorted from longest to shortest. TYPE 2
FROM film
WHERE runtime_mins > 150
ORDER BY runtime_mins DESC;
Verified result: 15 records are returned. The first three runtimes are 201, 195, and 195 minutes.
7. Write a SQL query to display title, year, and rating for films released between 2015 and 2019 inclusive. Sort newest films first. TYPE 2
FROM film
WHERE year >= 2015 AND year <= 2019
ORDER BY year DESC;
Verified result: 13 records are returned. The first three are 2019 films: Little Women, Parasite, and Us.
8. Practical: In DataGrip, run a query that displays title, genre, and rating for all films sorted by genre A to Z, then rating highest to lowest within each genre. Paste your SQL and record the first three Action films shown. TYPE 3
FROM film
ORDER BY genre ASC, rating DESC;
Verified result: the first three Action films are The Dark Knight (9.0), Gladiator (8.5), and Terminator 2: Judgment Day (8.5). The two 8.5 Action films keep their database order because no title tiebreaker was requested.
9. Practical: In DataGrip, write and run a query to find Fantasy films with a rating of at least 7.5 and no more than 9.0. Display title, year, and rating. Sort by rating highest first, then title A to Z. Paste your SQL and record how many records are returned. TYPE 3
FROM film
WHERE genre = "Fantasy"
AND rating >= 7.5 AND rating <= 9.0
ORDER BY rating DESC, title ASC;
Verified result: 7 records are returned.
Suggested timing: 60 minutes. Warm up + vocabulary 10 min; notes and live DataGrip demo 20 min; worked examples 10 min; now you try 5 min; task set/practical work 15 min.
Practical setup: Pupils need Laragon running, DataGrip open, and the films database selected. Use a query console connected to the films database.
Key misconception to address: The secondary sort is only a tiebreaker inside matching primary-sort groups. Demonstrate ORDER BY genre ASC, rating DESC and then swap the fields to show how different the result becomes.
Live demo suggestion: Start with SELECT title, rating FROM film;, then add ORDER BY rating DESC. Next add , title ASC and point out how ties are handled. Finish with a WHERE year >= 2000 AND year <= 2009 query and explicitly check that both 2000 and 2009 appear in the results.
Note on BETWEEN: BETWEEN is marked as Extension throughout this lesson. It does not appear in the N5 spec appendix or in any past paper. Teach >= AND <= as the core form. If pupils ask about BETWEEN, explain it is a shorthand that means the same thing but that using it in the exam carries a small risk if mark schemes aren't flexible. Extension question: Ask pupils to write the BETWEEN-equivalent form using >= and <=, then ask what would change if the task said "after 2000 but before 2009" (answer: > 2000 AND < 2009, no = signs).
SQA command words covered: "write", "identify", "describe", "explain". Emphasise that "write a query" requires the full SQL statement in the correct clause order.