SQL ORDER BY

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

SQL ORDER BY

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which query lists students from the youngest age to the oldest age?
A SELECT * FROM students ORDER BY age ASC;
B SELECT * FROM students ORDER BY age DESC;
2. True or false: if you write ORDER BY score with no ASC or DESC, the rows are sorted from highest to lowest.
A False
B True
3. Which query shows the top scores first in a games table?
A SELECT * FROM games ORDER BY score DESC;
B SELECT * FROM games ORDER BY score;
4. A table is sorted with ORDER BY name. Which order do the names appear in?
A Zara, Chen, Bella, Alex
B Alex, Bella, Chen, Zara
5. Which query has the correct syntax for sorting the results of a filtered search?
A SELECT * FROM pets ORDER BY name WHERE age > 2;
B SELECT * FROM pets WHERE age > 2 ORDER BY name;

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1SELECT name, score FROM Students ORDER BY score DESC; ALists student names from A to Z
2SELECT name FROM Students ORDER BY name; BSELECT * FROM Students WHERE year = 10 ORDER BY age DESC;
3Sort the results by price from the lowest to the highest, using ASC to say so clearly CSELECT * FROM Games ORDER BY price ASC;
4Show only Year 10 students, then sort those rows by age from oldest to youngest DDESC goes after the field name, not before it. Fix: ORDER BY score DESC;
5SELECT * FROM Students ORDER BY WHERE year = 10; EWHERE must come before ORDER BY, and ORDER BY needs a field name. Fix: WHERE year = 10 ORDER BY name;
6SELECT name FROM Students ORDER BY DESC score; FLists students from the highest score to the lowest score

Definitions

ORDER BY

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
SELECT name, score FROM students ________ score;
2.
SELECT name, score FROM students ORDER BY score ____;
3.
SELECT name FROM students ORDER BY ____;
4.
SELECT name, score FROM students _____ year = 10 ORDER BY score DESC;
5.
SELECT name, score FROM students WHERE year = 9 ORDER BY score ___;

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A SELECT name, score
B FROM students
C WHERE year = 10
D ORDER BY DESC score;
Question 2
A SELECT name FROM pets
B ORDER name ASC;
C SELECT name FROM pets
D ORDER BY name DESC;
Question 3
A SELECT title, price FROM books
B WHERE price < 20;
C ORDER BY price DESC;
Question 4
A SELECT name, points
B FROM teams
C ORDER BY points DOWN;
Question 5
A SELECT name, city
B FROM customers
C WHERE city = 'Perth'
D SORT BY name;

Answer Key

SQL ORDER BY
Section A: Quick Questions
Q1
A — SELECT * FROM students ORDER BY age ASC;
Q2
A — False
Q3
A — SELECT * FROM games ORDER BY score DESC;
Q4
B — Alex, Bella, Chen, Zara
Q5
B — SELECT * FROM pets WHERE age > 2 ORDER BY name;
Section B: Match the Pairs
Q1
SELECT name, score FROM Students ORDER BY score DESC; → Lists students from the highest score to the lowest score
Q2
SELECT name FROM Students ORDER BY name; → Lists student names from A to Z
Q3
Sort the results by price from the lowest to the highest, using ASC to say so clearly → SELECT * FROM Games ORDER BY price ASC;
Q4
Show only Year 10 students, then sort those rows by age from oldest to youngest → SELECT * FROM Students WHERE year = 10 ORDER BY age DESC;
Q5
SELECT * FROM Students ORDER BY WHERE year = 10; → WHERE must come before ORDER BY, and ORDER BY needs a field name. Fix: WHERE year = 10 ORDER BY name;
Q6
SELECT name FROM Students ORDER BY DESC score; → DESC goes after the field name, not before it. Fix: ORDER BY score DESC;
Section C: Definitions
Q1
ORDER BY — Sorts the rows a SELECT query returns by a chosen field. It sorts A to Z or lowest to highest by default (ASC), or the reverse if you add DESC after the field name.
Section D: Fill in the Blanks
Q1
SELECT name, score FROM students ORDER BY score;
Q2
SELECT name, score FROM students ORDER BY score DESC;
Q3
SELECT name FROM students ORDER BY name;
Q4
SELECT name, score FROM students WHERE year = 10 ORDER BY score DESC;
Q5
SELECT name, score FROM students WHERE year = 9 ORDER BY score ASC;
Section E: Spot the Error
Q1
D — DESC must come after the field name, not before it. It should be ORDER BY score DESC; so the highest scores come first.
Q2
B — The keyword is ORDER BY, not just ORDER. The line should be ORDER BY name ASC; to sort the names from A to Z.
Q3
B — The semicolon ends the query too early, so the ORDER BY line is cut off from it. Remove the semicolon so it reads WHERE price < 20 and the semicolon goes at the end of the ORDER BY line.
Q4
C — SQL does not have a DOWN keyword. To sort from highest to lowest, use DESC: ORDER BY points DESC;
Q5
D — SQL uses ORDER BY to sort results, not SORT BY. It should be ORDER BY name; which sorts the names from A to Z.