HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SQL (SELECT)Tables (CREATE & INSERT)Filtering (WHERE)Sorting & LimitingAggregate FunctionsUpdate & DeleteJoins (JOIN)
REFERENCE
📖 Overview
🔎 Querying (SELECT)
🗃️ Creating a Table
✏️ Inserting & Modifying Data
🧲 Filtering (WHERE)
🔢 Sorting & Limiting
ORDER BYLIMIT
📊 Aggregate Functions
🔗 Joins (JOIN)
SQL›REFERENCE›Sorting & Limiting
🔢

Sorting & Limiting

ORDER BY and LIMIT.

CommandDescription
ORDER BYSorts the result.
LIMITLimits the number of results.

ORDER BY

Try it yourself →

Sorts the result.

Syntax

SELECT ... ORDER BY column [DESC];

Description

Default is ascending (ASC); DESC sorts in descending order.

Example

CREATE TABLE k(name TEXT, score INTEGER);
INSERT INTO k VALUES ('Alex', 95), ('Sam', 88), ('Zoe', 99);
SELECT name FROM k ORDER BY score DESC;

Expected output

Zoe
Alex
Sam

When to use?

Ordered lists (highest score, etc.).

LIMIT

Try it yourself →

Limits the number of results.

Syntax

SELECT ... LIMIT n;

Description

Retrieves the first n rows. Combined with ORDER BY, it becomes a 'top n' query.

Example

CREATE TABLE k(score INTEGER);
INSERT INTO k VALUES (50), (90), (70);
SELECT score FROM k ORDER BY score DESC LIMIT 1;

Expected output

90

When to use?

First/last N records, pagination.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code