SQL: Sorting and Filtering
Master SQL: Sorting and Filtering for GCSE Computer Science with this free worksheet and full mark scheme — Foundation and Higher exam-style questions with worked answers for AQA GCSE Computer Science (8525). Results can be filtered with conditions and ordered using ORDER BY.
Free downloads
These worksheets and mark schemes are original, written for Virtus Academy and checked against the current AQA specification. Every worksheet comes with a full mark scheme.
Topic overview
WHERE filters which records a query returns, and ORDER BY controls the order in which they appear.
WHERE follows the FROM clause and states a condition. SELECT Name FROM Students WHERE Age > 15 returns only students older than 15. Conditions can be combined with AND and OR, and text values must be enclosed in quotation marks.
ORDER BY comes last and names the field to sort by. ASC sorts ascending and DESC descending; ascending is the default if neither is stated. The clause order is fixed: SELECT, FROM, WHERE, ORDER BY. Writing them in a different order is a syntax error, and this is the detail most often lost.
Revision notes
Filtering with WHERE
WHERE follows FROM and states a condition that records must satisfy.
SELECT Name FROM Students WHERE Age > 15 returns only those older than 15. Text values need quotation marks: WHERE Town = 'Manchester'.
Combining conditions
AND requires both conditions to be true; OR requires at least one.
WHERE Age > 15 AND Town = 'Leeds' returns only students meeting both criteria. Brackets clarify the order when AND and OR appear together.
Sorting with ORDER BY
ORDER BY comes last and names the field to sort by.
ASC sorts ascending, DESC descending. Ascending is the default. The clause order is fixed: SELECT, FROM, WHERE, ORDER BY — any other order is a syntax error.
Key points
- WHERE filters which records are returned.
- WHERE follows the FROM clause.
- Text values need quotation marks.
- AND requires both conditions to be true.
- ORDER BY sorts the results.
- The clause order is SELECT, FROM, WHERE, ORDER BY.
Worked examples
Example 1
Write an SQL query returning the Name of members from Leeds, sorted by Name. [3 marks]
Working
Example 2
Write a WHERE clause selecting students aged over 15 who live in York. [2 marks]
Working
Example 3
State the correct order of the four SQL clauses. [2 marks]
Working
Common mistakes
Putting ORDER BY before WHERE.
The order is fixed: SELECT, FROM, WHERE, ORDER BY.
Forgetting quotation marks around text.
Text values need them; numbers do not.
Using AND when either condition would do.
AND requires both; OR requires at least one.
Omitting the field name after ORDER BY.
It must state which field to sort by.
Exam tips
- Follow the fixed clause order every time.
- Quote text values, not numbers.
- Check whether AND or OR is required.
- Name the sort field after ORDER BY.
Key terms
- WHERE
- The clause filtering which records are returned.
- ORDER BY
- The clause sorting the results.
- ASC
- Sorting in ascending order, the default.
- DESC
- Sorting in descending order.
Related topics
Written and reviewed against the current AQA specification. Spotted an error? Let us know.