Sorting query results

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

Sorting query results

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which query shows the highest scores first?
A SELECT * FROM Results ORDER BY Score ASC
B SELECT * FROM Results ORDER BY Score DESC
2. True or false: ORDER BY Score ASC puts the lowest score at the top.
A False
B True
3. Which clause sorts the results of a query?
A ORDER BY
B SORT WHEN
4. What does ORDER BY Name ASC do to a list of student names?
A Sorts them A to Z
B Sorts them Z to A
5. Two students both scored 90. Which query sorts the tied students by Name?
A ORDER BY Score DESC, Name
B ORDER BY Score DESC AND Name

Match the Pairs

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

Left columnRight column
1ORDER BY Score DESC AORDER BY Score DESC, Name
2ORDER BY Score ASC BSorts the results from the lowest score to the highest score
3Show the top scores first CSorts the results from the highest score to the lowest score
4Sort by Score, then sort tied students by Name DThe word BY is missing. It should be ORDER BY Score DESC.
5ORDER Score DESC ESELECT Name, Score FROM Results ORDER BY Score DESC
6ORDER BY Score DESC Name FThere is no comma between the two sort fields. It should be ORDER BY Score DESC, Name.

Definitions

ORDER BY Score DESC

Fill in the Blanks

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

1.
SELECT Name, Score FROM Results ________ Score DESC
2.
SELECT Name, Score FROM Results ORDER BY Score ____
3.
SELECT Name, Time FROM Races ORDER BY Time ___
4.
SELECT Name, Score FROM Results ORDER BY Score DESC, ____
5.
SELECT Name, Score FROM Results ORDER BY _____ DESC

Spot the Error

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

Question 1
A SELECT Name, Score
B FROM Results
C ORDER Score DESC;
Question 2
A SELECT * FROM Results
B ORDER BY Score DOWN
C LIMIT 3;
Question 3
A SELECT Name, Score
B FROM Results
C ORDER BY Score DESC Name;
Question 4
A -- Top scores first
B SELECT Name, Score
C FROM Results
D ORDER BY Score ASC;
Question 5
A SELECT Name, Score
B FROM Results;
C WHERE Score > 50
D ORDER BY Score DESC;

Answer Key

Sorting query results
Section A: Quick Questions
Q1
B — SELECT * FROM Results ORDER BY Score DESC
Q2
B — True
Q3
A — ORDER BY
Q4
A — Sorts them A to Z
Q5
A — ORDER BY Score DESC, Name
Section B: Match the Pairs
Q1
ORDER BY Score DESC → Sorts the results from the highest score to the lowest score
Q2
ORDER BY Score ASC → Sorts the results from the lowest score to the highest score
Q3
Show the top scores first → SELECT Name, Score FROM Results ORDER BY Score DESC
Q4
Sort by Score, then sort tied students by Name → ORDER BY Score DESC, Name
Q5
ORDER Score DESC → The word BY is missing. It should be ORDER BY Score DESC.
Q6
ORDER BY Score DESC Name → There is no comma between the two sort fields. It should be ORDER BY Score DESC, Name.
Section C: Definitions
Q1
ORDER BY Score DESC — Sorts query results by the Score field from highest to lowest. Use ASC for lowest to highest. Add a second field after a comma to sort ties, like ORDER BY Score DESC, Name.
Section D: Fill in the Blanks
Q1
SELECT Name, Score FROM Results ORDER BY Score DESC
Q2
SELECT Name, Score FROM Results ORDER BY Score DESC
Q3
SELECT Name, Time FROM Races ORDER BY Time ASC
Q4
SELECT Name, Score FROM Results ORDER BY Score DESC, Name
Q5
SELECT Name, Score FROM Results ORDER BY Score DESC
Section E: Spot the Error
Q1
C — The word BY is missing. Sorting needs ORDER BY, so the line should be ORDER BY Score DESC;
Q2
B — DOWN is not a sort direction. Use DESC for highest to lowest, so the line should be ORDER BY Score DESC.
Q3
C — Two sort fields must be separated by a comma. It should be ORDER BY Score DESC, Name; so ties are sorted by Name.
Q4
D — ASC sorts lowest to highest, so the top scores would be last. Use DESC to put the top scores first.
Q5
B — The semicolon ends the query too early, so the WHERE and ORDER BY lines would be cut off. Remove it: FROM Results