Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

ORDER BY & Pagination
SQL

ORDER BY & Pagination

Master sorting techniques and how to handle large datasets using LIMIT and OFFSET.

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;

Sorting Order

  • ASC: Ascending (default)
  • DESC: Descending

Multi-Column Sorting

You can sort by multiple columns. SQL sorts by the first column; for rows with identical values in the first column, it uses the second column, and so on.

SELECT * FROM employees ORDER BY department_id ASC, salary DESC;

NULL Handling in Sorting

NULL behavior varies by database:

  • PostgreSQL: NULLs are considered larger than any value — they appear last in ASC, first in DESC.
  • MySQL: NULLs appear first in ASC, last in DESC.
  • SQL Server: NULLs are smallest — they appear first in ASC, last in DESC.

Override with NULLS FIRST or NULLS LAST (PostgreSQL, Oracle):

SELECT * FROM employees ORDER BY salary DESC NULLS LAST;

Pagination with LIMIT and OFFSET

When result sets are large, you paginate by specifying how many rows to return and where to start:

-- Page 1: rows 1-10
SELECT * FROM employees ORDER BY employee_id LIMIT 10 OFFSET 0;

-- Page 2: rows 11-20
SELECT * FROM employees ORDER BY employee_id LIMIT 10 OFFSET 10;

Performance note: Large OFFSET values are slow because the database still scans all skipped rows. For deep pagination, use keyset pagination:

-- Keyset pagination (efficient for deep pages)
SELECT * FROM employees WHERE employee_id > 1000 ORDER BY employee_id LIMIT 10;

ORDER BY with Expressions

You can sort by computed values:

SELECT name, salary * 12 AS annual FROM employees ORDER BY annual DESC;

ORDER BY and Indexes

Sorting an unindexed column on a large table triggers a “filesort” operation, which is expensive. Adding an index on the sort column can dramatically speed up ORDER BY queries.

Q: Can you sort by multiple columns?

A: Yes. You can specify multiple columns in the ORDER BY clause. SQL will sort by the first column, and for rows where the first column values are identical, it will sort by the second column, and so on.

Q: How do you handle NULLs while sorting?

A: Default behavior varies by database (e.g., PostgreSQL puts NULLs last in ASC, first in DESC). You can control this using NULLS FIRST or NULLS LAST if the database supports it.

Q: What is the default sorting order?

A: The default order is Ascending (ASC).

Q: Why is OFFSET pagination slow on large datasets?

A: The database must scan and skip all rows before the OFFSET. For page 100, it scans 1000 rows and discards 990. Keyset pagination (using a WHERE clause on the last seen ID) avoids this overhead.

1. Sort employees by salary descending.

SELECT * FROM employees
ORDER BY salary DESC;

2. Sort by department ASC and salary DESC.

SELECT * FROM employees
ORDER BY department_id ASC, salary DESC;

3. Get 3rd highest salary.

SELECT salary FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;

4. Select random 5 rows.

-- For PostgreSQL/MySQL
SELECT * FROM employees
ORDER BY RANDOM() -- or RAND() in MySQL
LIMIT 5;

5. Implement pagination (page 2, 10 rows per page).

SELECT * FROM employees
ORDER BY employee_id
LIMIT 10 OFFSET 10;

6. Keyset pagination (page after ID 100).

SELECT * FROM employees
WHERE employee_id > 100
ORDER BY employee_id
LIMIT 10;

My Private Notes

Notes are auto-saved locally to this device.