SQL TRAINING · LESSON 3 OF 4 · 3 MIN READ

Control order with ORDER BY

Choose ascending or descending order deliberately.

Understand the idea

A result set does not have a dependable default order in a real database. ORDER BY specifies a sort. ASC puts smaller scores first, while DESC puts larger scores first. The best direction depends on the question, such as highest scores versus lowest prices.

Work through it

Choose the sort column and direction before limiting rows. Check a small set manually. When values tie, a real query may need an additional tie-breaker for a fully predictable order. DigiSkill’s top-three exercise states that its test scores do not tie and supports one score sort.

SEE IT IN CONTEXT

Worked example

SELECT name, score
FROM students
ORDER BY score DESC;
Scores 61, 95, 80 become 95, 80, 61.

Descending order puts the highest score first. This query orders all rows; the next lesson keeps only the requested number of top rows.

Common mistakes to avoid

  • Using ASC for a highest-first task.
  • Relying on the order records happen to appear in.
  • Ignoring ties when a real result needs a unique order.

NOW HAVE A GO

Find the three highest quiz scores

Apply the idea in the linked activity. Follow its own brief and values; examples in this lesson are illustrative.

Try the practice activity →Download the practice starter
About the linked activity

DigiSkill has two SQL practice activities and a focused browser checker: SELECT name, score FROM students, numeric WHERE comparisons, ORDER BY score ASC/DESC and LIMIT. Lessons build toward those two activities. No live database, JOIN, GROUP BY or writes are included.

Reading marks are self-reported and saved only in this browser.

0 of 4 lessons marked as read

Keep learning: Reference documentation ↗

YOUR LEARNING JOURNEY

Look how far you’ve come.

Your progress is saved in this browser. Clearing browser data removes it.

Manage saved progress

This removes your completed-challenge history from this browser.