HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
SQL Queries: SELECT, WHERE and ORDER BY
  1. 1SELECT and FROM: choosing the fields
  2. 2WHERE: choosing the records
  3. 3ORDER BY: sorting the output
  1. Computer Science
  2. /SQL Queries: SELECT, WHERE and ORDER BY
  3. /ORDER BY: sorting the output

Course 9.2·Lesson 3 of 3

ORDER BY: sorting the output

ORDER BY sorts the output of a query by one field, in ascending (ASC) or descending (DESC) order. You sort numbers, text and dates, put ORDER BY together with WHERE, and work out the exact order of the output.

about 25 min1 simulation1 practice question
Lesson 3 of 3Show all lessonsHide
SQL Queries: SELECT, WHERE and ORDER BY
  1. 1SELECT and FROM: choosing the fields
  2. 2WHERE: choosing the records
  3. 3ORDER BY: sorting the output

Try the code and the simulations beside the lesson.

Putting the output in order

A list of books is easier to use when it is sorted: the longest book first, or the titles from A to Z. ORDER BY sorts the output by a field.

  • ASC means ascending: smallest first, A to Z, earliest date first.
  • DESC means descending: largest first, Z to A, latest date first.
sql
SELECT Title, Pages
FROM Books
ORDER BY Pages DESC;

The output:

table
Title        Pages
Deep Blue    410
Night Train  320
Iron Bridge  288
Lost Key     256
Green Hills  180
Snow Fox     144
Two lists side by side. On the left the titles and pages in table order: Night Train 320, Green Hills 180, Lost Key 256, Deep Blue 410, Iron Bridge 288, Snow Fox 144. An arrow labelled ORDER BY Pages DESC points to the right list, sorted largest first: Deep Blue 410, Night Train 320, Iron Bridge 288, Lost Key 256, Green Hills 180, Snow Fox 144.

The table itself does not change. Only the output is sorted.

Sorting text

Text is sorted in alphabetical order, letter by letter. Deep Blue comes before Green Hills because D comes before G.

sql
SELECT Title
FROM Books
ORDER BY Title ASC;

The output:

table
Title
Deep Blue
Green Hills
Iron Bridge
Lost Key
Night Train
Snow Fox

If you leave out ASC and DESC, the output is sorted in ascending order. In an examination answer, write ASC or DESC every time, so your meaning is clear.

Dates sort from the earliest to the latest with ASC. In SQLite a date stored as text, year first, such as 2024-03-14, sorts in the right order.

WHERE and ORDER BY together

A script can choose records and sort them. The clauses always come in this order:

  1. SELECT the fields
  2. FROM the table
  3. WHERE the condition
  4. ORDER BY the field and ASC or DESC
sql
SELECT Title, Pages
FROM Books
WHERE Genre = 'Nature'
ORDER BY Pages ASC;

The output:

table
Title        Pages
Snow Fox     144
Green Hills  180
Deep Blue    410

Work it out in two steps. First keep the nature books: Green Hills, Deep Blue and Snow Fox. Then sort those three by pages, smallest first.

A script can also sort by a field that it does not output. SELECT Title FROM Books ORDER BY Pages DESC; lists only the titles, but the longest book is still first.

Practise with new numbers

Try it in Python

The program runs a script with WHERE and ORDER BY on the Books table and prints each row of the output with its place.

Read it first. Then run it. Is the shortest book first?

1. Change ASC to DESC. Predict the new first row, then run it.

2. Change the script so it sorts by Title instead. Which title is first now?

Common mistakes

  • Mixing up ASC and DESC. ASC starts with the smallest value. DESC starts with the largest.
  • ORDER BY before WHERE. The order is SELECT, FROM, WHERE, ORDER BY.
  • Sorting all the records. With WHERE, only the records that pass are sorted and output.
  • Thinking ORDER BY changes the table. It sorts only the output.

Remember these

  • ORDER BY field ASC sorts smallest first, A to Z. DESC sorts largest first, Z to A.
  • The clauses go in the order SELECT, FROM, WHERE, ORDER BY.
  • Work out a script in steps: keep the records that pass, sort them, then show the selected fields.

Check yourself

Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.

  1. Which title is first in the output of SELECT Title FROM Books ORDER BY Pages ASC;?
  1. Which title is first with ORDER BY Title DESC?
  1. Put these clauses in the right order: WHERE, ORDER BY, FROM, SELECT.
  1. What is the output of SELECT BookID FROM Books WHERE Author = 'Silva' ORDER BY Pages DESC;?
  1. Write a script that outputs the Title of every mystery book, from A to Z.

References

  • Cambridge International, IGCSE Computer Science 0478 syllabus (exams in 2026, 2027 and 2028), section 9 Databases, 9.4 (ORDER BY ascending and descending).

Enroll free in SQL Queries: SELECT, WHERE and ORDER BY

Every lesson is open to read. Enrolling also lets you:

  • Mark lessons done and carry on where you stopped.
  • Take the tests of this lesson and its course.
  • Count the course towards the certificate for its chapter.
No payment. You can leave at any time.

Problems (3)

A fair lists its rides and the length of each ride in minutes in a table Rides…Easy · High School (9–10) · by StemPy Lab
A shop keeps this table, Products. Write down the output of this script…Medium · High School (9–10) · by StemPy Lab
A community hall stores its events in a table Events…Hard · High School (9–10) · by StemPy Lab
That was the last lessonBack to the course: take the test
← SQL Queries: SELECT, WHERE and ORDER BY
← WHERE: choosing the recordsNext →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place and take the tests.

Python Labs (1)

Sorting with ORDER BYPython Lab · by StemPy Lab

Question Sets (1)

SELECT, WHERE and ORDER BY: Concept Check10 questions · by StemPy Lab

Notes (1)

SELECT, WHERE and ORDER BY: Quick RevisionNote · by StemPy Lab