Skip to content
Guest
Learn SQL · Course Syllabus Show lessons
Querying Data · 7 min read · Beginner

Sorting, LIMIT & DISTINCT

Sort query results with ORDER BY ASC/DESC, cap rows with LIMIT for top-N queries, and remove duplicates with DISTINCT.

What you'll learn

  • Sort rows in ascending (ASC) or descending (DESC) order
  • Return top-N results cleanly with ORDER BY ... LIMIT
  • Remove duplicate values from output using SELECT DISTINCT

Database tables have no guaranteed row order unless you explicitly add ORDER BY. Pair it with LIMIT to build leaderboards, recent activity feeds, and pagination.

Fill-in-the-Blank Exercise

Top 3 Highest Scores

Return the top 3 learners ordered from highest score to lowest.

SELECT name, score
FROM learners
 BY score 
 3;

Try It Yourself

SQLLeaderboard Query (ORDER BY + LIMIT)
Query Result (In-Browser SQLite)

Click Run SQL to execute this query against a live in-browser SQLite database.

Check Your Understanding

Score: 0 / 1

1. Which keyword removes duplicate values from a SELECT result?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind