Lesson 3 of 4 15 min read
SELECT, WHERE, ORDER BY and LIMIT
Retrieve exactly the columns and rows you need, sort them and limit the number of results.
In this lesson you will
- Choose columns with SELECT and rows with WHERE
- Sort results with ORDER BY in ascending or descending order
- Return only the first rows with LIMIT and page through results with OFFSET
The shape of a query
Every query in this lesson follows the same pattern:
SELECT columns
FROM table
WHERE condition
ORDER BY column
LIMIT number;Layers from top to bottom: SELECT, FROM, WHERE, ORDER BY, LIMIT
Only SELECT and FROM are required. The examples use the students table from the first lesson.
Choosing columns with SELECT
SELECT name, city
FROM students;This returns the name and city of all six students. SELECT * returns every column. It's handy for exploring, but in application code list the columns you need, so the query stays fast and doesn't break when columns are added.
You can rename a column in the result with AS:
SELECT name AS student, marks AS score
FROM students;Choosing rows with WHERE
WHERE keeps only the rows for which a condition is true:
SELECT name, marks
FROM students
WHERE city = 'Kochi';| name | marks |
|---|---|
| Asha Menon | 88 |
| Meera Nair | 95 |
Comparison operators: =, <> (not equal; != also works in PostgreSQL), >, <, >=, <=.
SELECT name, year
FROM students
WHERE year = 2;| name | year |
|---|---|
| Asha Menon | 2 |
| Arjun Das | 2 |
Sorting with ORDER BY
Without ORDER BY, the database may return rows in any order. Always sort when order matters:
SELECT name, marks
FROM students
ORDER BY marks DESC;ASC (ascending) is the default; DESC sorts from highest to lowest. You can sort by several columns: ORDER BY city ASC, marks DESC sorts by city, then by marks within each city.
Limiting results with LIMIT
LIMIT returns only the first n rows, which is perfect for "top" lists:
SELECT name, marks
FROM students
ORDER BY marks DESC
LIMIT 3;| name | marks |
|---|---|
| Meera Nair | 95 |
| Asha Menon | 88 |
| Priya Shah | 81 |
OFFSET skips rows, which lets you show results page by page:
SELECT name, marks
FROM students
ORDER BY marks DESC
LIMIT 3 OFFSET 3; -- the 4th to 6th highest: Kiran Rao (77), Ravi Kumar (72), Arjun Das (64)The order SQL evaluates a query
You write SELECT first, but the database logically processes the clauses in this order:
- FROM
- WHERE
- SELECT
- ORDER BY
- LIMIT
That's why LIMIT combined with ORDER BY gives "the top three": the rows are sorted before they are cut.
Hands-on exercise
Using the students table, write queries for: (1) the names and cities of all third-year students; (2) every student sorted alphabetically by name; (3) the two students with the lowest marks; (4) the name of the student with the highest marks in Bengaluru. Work out the expected result of each query by hand from the table before running it.
Knowledge check
3 questions • 70% to pass • Practice only, no certificate
Key concepts
- SELECT chooses columns, FROM chooses the table and WHERE filters rows.
- Without ORDER BY, row order is not guaranteed; ASC is the default and DESC reverses it.
- LIMIT keeps the first n rows; combine it with ORDER BY for top-n queries and OFFSET for paging.
- The database logically processes FROM, WHERE, SELECT, ORDER BY, then LIMIT.
