All posts
Data & AnalyticsPart 4 · SQL for Humans

Sorting & Ranking — Making Sense of the Answers

Data without order is just noise. Learn to sort, rank, and surface what matters most.

River Team
September 22, 20266 min read

By now you can pull data and filter it. But there's one more step between "a pile of rows" and "an answer you can act on": order. A list of 5,000 customers in random order is noise. The same list sorted by how much they spend is a strategy.

That's what this post is about — ORDER BY and LIMIT, the two tools that turn raw results into rankings.

Data without order is just noise

Think about a leaderboard. If a game showed you everyone's scores shuffled randomly, it would be pointless. The ranking is the information. Business questions work the same way:

  • Who are our top 10 customers by revenue?
  • What are our most recent orders?
  • Who are our highest-paid employees?

Every one of those is the same question in different clothes: give me these rows, but put them in a meaningful order.

ORDER BY: sorting your results

ORDER BY does exactly what it says — it orders your rows by a column. The skeleton grows by one more line:

SELECT <columns>
FROM <table>
WHERE <condition>
ORDER BY <column> <direction>;

The direction is either ASC (ascending — smallest first) or DESC (descending — largest first). If you don't specify, SQL defaults to ascending.

Let's return to the employees table from Soko Fresh:

employee_idnamedepartmentsalarycity
1Wanjiku KamauSales180000Nairobi
2Otieno OchiengSales95000Kisumu
3Achieng NyamburaEngineering240000Nairobi
4Peter MwangiEngineering190000Nakuru
5Njeri WaweruFinance140000Nairobi
6David KipropSales90000Eldoret
7Mercy ChebetEngineering160000Nairobi
8Samuel OmondiCustomer Success65000Mombasa

Highest-paid employees first

The classic ranking question: who earns the most?

SELECT name, salary FROM employees
ORDER BY salary DESC;

Plain English: "Show me name and salary, from employees, ordered by salary descending."

namesalary
Achieng Nyambura240000
Peter Mwangi190000
Wanjiku Kamau180000
Mercy Chebet160000
Njeri Waweru140000
Otieno Ochieng95000
David Kiprop90000
Samuel Omondi65000

Now the list tells you something at a glance. The top earner is Achieng, the lowest is Samuel. That's the difference ordering makes.

If you'd left off the direction — just ORDER BY salary — you'd get the same list but smallest first. Ascending is the default.

That default is worth committing to memory, because most "top N" questions want the opposite. You'll find yourself typing DESC constantly, and it's a one-letter difference (DESC vs ASC) that flips your entire ranking. Before you present numbers in a meeting, double-check your sort direction — it's the kind of subtle slip that produces a perfectly ordered list that is nonetheless backwards.

Sorting text works too

ORDER BY isn't just for numbers. Sort names alphabetically:

SELECT name FROM employees
ORDER BY name ASC;

Plain English: "Show me names, ordered alphabetically ascending."

Achieng comes first, Wanjiku last. Text sorts in alphabetical order, which is genuinely useful for directories, product lists, or any time you want to scan a list quickly.

Sorting by more than one column

Real rankings often have ties. Suppose two employees earn the same salary — how do you want them ordered? You can sort by a second column to break the tie.

SELECT name, department, salary FROM employees
ORDER BY department ASC, salary DESC;

Plain English: "Show me name, department, and salary, ordered by department ascending, and within each department by salary descending."

Now the result is grouped by department (alphabetically), and inside each department, the highest earner appears first. This is the "sort within a sort" pattern — list the columns in priority order, and SQL applies them left to right.

LIMIT: keeping only the top results

Most of the time, you don't want the whole sorted list — you want the top few. A "top 3" question is answered with LIMIT:

SELECT name, salary FROM employees
ORDER BY salary DESC
LIMIT 3;

Plain English: "Show me name and salary, ordered by salary descending, but only the first 3 rows."

namesalary
Achieng Nyambura240000
Peter Mwangi190000
Wanjiku Kamau180000

LIMIT always comes last — after ORDER BY. It simply truncates your result to the number of rows you specify. Combined with ORDER BY, it answers every "top N" question in the business.

Real rankings for Soko Fresh

Let's imagine the business questions that come up in a typical week, and the query for each.

1. "Who are our three most recent hires?"

This one needs a hire_date column, which we haven't shown yet — but you can see the pattern:

SELECT name, hire_date FROM employees
ORDER BY hire_date DESC
LIMIT 3;

The newest hire first, cut to three rows.

2. "Show me the five lowest-paid employees, so we can plan raises."

SELECT name, salary FROM employees
ORDER BY salary ASC
LIMIT 5;

Smallest salary first, keep five. That's the entire raise-review shortlist, generated in one line.

3. "List every department's employees, highest salary first within each."

SELECT department, name, salary FROM employees
ORDER BY department ASC, salary DESC;

Grouped by department, ranked by pay inside each group.

4. "Top 3 earners in Engineering specifically."

Here we combine everything from this series so far — WHERE to filter, ORDER BY to rank, LIMIT to cut:

SELECT name, salary FROM employees
WHERE department = 'Engineering'
ORDER BY salary DESC
LIMIT 3;

Filter first, sort second, trim third. That's the full toolkit working together.

Putting the whole pattern together

Take a moment to appreciate what you now know. Every query you've written fits this one master skeleton:

SELECT <what>
FROM <where it lives>
WHERE <which rows qualify>
ORDER BY <what order>
LIMIT <how many>;

Read it in order and it's almost a sentence: select these columns, from this table, where these conditions hold, ordered like this, limited to this many rows.

That's not a trivial achievement. You can now:

  • choose which columns come back (SELECT),
  • decide which rows qualify (WHERE),
  • put them in order (ORDER BY),
  • and keep only the top ones (LIMIT).

Those four tools answer an enormous share of the questions a business actually asks. But there's a whole other category we haven't touched — questions like "how many customers do we have?" and "what's our average order value?" Those aren't about listing rows at all; they're about summarizing them. That's the next post, and it's where SQL starts to feel like magic.

Try it yourself

On sqlbolt.com, work through the "filtering and sorting" lesson. Then for employees, write queries to answer:

  1. List all employees from highest to lowest salary.
  2. List all employees alphabetically by name.
  3. Show the top 4 highest-paid employees.
  4. Show the 2 lowest-paid employees.
  5. List employees ordered by city, then by salary descending.
sqltutorialbeginnersorder-by

Follow “SQL for Humans”

Get notified when we publish the next post in this series. No spam.

Unsubscribe anytime. We only email about this series.