Sorting & Ordering

8 Functions

Sorting and ordering are fundamental to data retrieval. In SQL, the order of result rows is never guaranteed unless you explicitly define it. These tools allow you to organize data logically—whether you are displaying the top players in a league or paginating through a long list of quests.

1.

ORDER BY

Description

The primary clause used to sort the result set of a query.

Syntax
SELECT col FROM table ORDER BY col_name;
When to Use

Displaying the Leaderboard sorted by user XP.

Example Query
SELECT username, xp FROM users ORDER BY xp;
💡 Pro Tip

You can sort by multiple columns. For example, ORDER BY xp DESC, username ASC will sort by XP first, and then alphabetically for users who have the exact same XP.

2.

ASC (Ascending)

Description

Sorts the data in ascending order (smallest to largest, A to Z).

Syntax
ORDER BY col_name ASC;
When to Use

Sorting a list of Quests by their difficulty level or creation date (oldest first).

Example Query
SELECT * FROM quests ORDER BY difficulty ASC;
💡 Pro Tip

ASC is the default sorting order in PostgreSQL. You don't technically need to type it, but doing so makes your code more readable for others.

3.

DESC (Descending)

Description

Sorts the data in descending order (largest to smallest, Z to A).

Syntax
ORDER BY col_name DESC;
When to Use

The standard for any Leaderboard—showing the player with the highest XP at the very top.

Example Query
SELECT * FROM leaderboard ORDER BY xp DESC;
💡 Pro Tip

When sorting by dates, DESC shows the most recent (newest) items first.

4.

NULLS FIRST

Description

A modifier that forces rows with NULL values to appear at the beginning of the sorted list.

Syntax
ORDER BY col_name DESC NULLS FIRST;
When to Use

Finding users who have never logged in (where last_login is NULL) by putting them at the top of an audit list.

Example Query
SELECT username, last_login FROM users ORDER BY last_login DESC NULLS FIRST;
💡 Pro Tip

By default, PostgreSQL considers NULL values to be "larger" than any non-null value, so they naturally go to the end in ASC and the beginning in DESC. Use this keyword to override that behavior.

5.

NULLS LAST

Description

A modifier that forces rows with NULL values to appear at the end of the sorted list.

Syntax
ORDER BY col_name ASC NULLS LAST;
When to Use

Sorting players by their "Arena Win Rate" but keeping those who haven't played any matches (NULL) at the bottom.

Example Query
SELECT username, win_rate FROM users ORDER BY win_rate ASC NULLS LAST;
💡 Pro Tip

This is vital for professional UI/UX, as users usually don't want to see "Empty" data at the top of their search results.

6.

LIMIT

Description

Restricts the total number of rows returned by the query.

Syntax
SELECT * FROM table LIMIT 10;
When to Use

Showing only the Top 5 players on the dashboard widget instead of the entire league.

Example Query
SELECT * FROM users ORDER BY xp DESC LIMIT 5;
💡 Pro Tip

Always use LIMIT in combination with ORDER BY. Without an order, LIMIT will just give you a random set of rows.

7.

OFFSET

Description

Skips a specified number of rows before starting to return results.

Syntax
SELECT * FROM table LIMIT 10 OFFSET 20;
When to Use

Implementing Pagination. To see "Page 3" of the Quest list (assuming 10 per page), you skip the first 20 rows.

Example Query
SELECT * FROM quests ORDER BY id LIMIT 10 OFFSET 20;
💡 Pro Tip

The formula for pagination is usually: OFFSET = (page_number - 1) * items_per_page.

8.

FETCH NEXT

Description

The SQL Standard alternative to LIMIT, used to retrieve a specific number of rows.

Syntax
SELECT * FROM table FETCH NEXT 5 ROWS ONLY;
When to Use

Writing queries that need to be compatible with other databases like Oracle or SQL Server.

Example Query
SELECT * FROM users ORDER BY id FETCH NEXT 5 ROWS ONLY;
💡 Pro Tip

While LIMIT is more common in the PostgreSQL and MySQL communities, FETCH NEXT is the "official" way according to the SQL standard.