TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2710 1.69K
๐Ÿš€ SQL Roadmap 2026 โ€” Part 4

Sorting, Limiting & Selecting the Right Records

In the previous part, you learned how to filter data using WHERE.

Now we'll learn how to control which records appear first, last, or how many records are returned.

These concepts are simple, but they are extremely important for SQL interviews and real-world analytics.

1๏ธโƒฃ ORDER BY

ORDER BY is used to sort query results.

Syntax

SELECT column1, column2
FROM table_name
ORDER BY column_name;


By default, SQL sorts in ascending order (ASC).

Example:

SELECT
employee_name,
salary
FROM employees
ORDER BY salary;


This displays employees from the lowest salary to the highest.

2๏ธโƒฃ ASC โ€” Ascending Order

You can explicitly specify ASC.

SELECT
employee_name,
salary
FROM employees
ORDER BY salary ASC;


For numbers:

100, 250, 500, 1000

For text:

Amit, Neha, Priya, Rahul

3๏ธโƒฃ DESC โ€” Descending Order

Use DESC when you want the highest values first.

SELECT
employee_name,
salary
FROM employees
ORDER BY salary DESC;


Result:

Amit: 1200000, Priya: 950000, Rahul: 850000, Neha: 650000

This is one of the most commonly used SQL patterns.

4๏ธโƒฃ Real-World Example: Top Salaries

Business requirement:



Find the highest-paid employees.



SELECT
employee_name,
salary
FROM employees
ORDER BY salary DESC;


But this might return thousands of employees.

That's where LIMIT becomes useful.

5๏ธโƒฃ LIMIT

LIMIT restricts the number of rows returned.

SELECT
employee_name,
salary
FROM employees
ORDER BY salary DESC
LIMIT 5;


This returns only the top 5 employees by salary.

Think of it as:

ORDER BY DESC โ†’ Highest first โ†’ LIMIT 5 โ†’ Keep first 5

6๏ธโƒฃ Top 10 Products by Price

SELECT
product_name,
price
FROM products
ORDER BY price DESC
LIMIT 10;


Very common in analytics.

7๏ธโƒฃ LIMIT Without ORDER BY

You technically can write:

SELECT *
FROM customers
LIMIT 10;


But this means:



Give me 10 rows.



It does not mean:



Give me the first 10 rows according to some meaningful business order.



Without ORDER BY, the returned order should generally not be relied upon.

If you want the top 10 customers by revenue:

SELECT
customer_id,
revenue
FROM customer_revenue
ORDER BY revenue DESC
LIMIT 10;


8๏ธโƒฃ OFFSET

OFFSET allows you to skip a number of rows.

Example:

SELECT
employee_name,
salary
FROM employees
ORDER BY salary DESC
LIMIT 5 OFFSET 5;


This skips the first 5 rows and returns the next 5.

Conceptually:

Rows 1โ€“5 โ†’ Skip, Rows 6โ€“10 โ†’ Return

9๏ธโƒฃ Pagination

LIMIT and OFFSET are often used for pagination.

For example:

Page 1

SELECT *
FROM customers
ORDER BY customer_id
LIMIT 10 OFFSET 0;


Page 2

SELECT *
FROM customers
ORDER BY customer_id
LIMIT 10 OFFSET 10;


Page 3

SELECT *
FROM customers
ORDER BY customer_id
LIMIT 10 OFFSET 20;


The general pattern is:

Page 1 โ†’ OFFSET 0, Page 2 โ†’ OFFSET 10, Page 3 โ†’ OFFSET 20

๐Ÿ”Ÿ Sorting by Multiple Columns

You can sort using more than one column.

Example:

SELECT
employee_name,
department,
salary
FROM employees
ORDER BY department ASC, salary DESC;
  • โค 4
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’