Skip to main content

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:

SQL
SELECT columns
FROM table
WHERE condition
ORDER BY column
LIMIT number;
The parts of a query, in the order you write them

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

SQL
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:

SQL
SELECT name AS student, marks AS score
FROM students;

Choosing rows with WHERE

WHERE keeps only the rows for which a condition is true:

SQL
SELECT name, marks
FROM students
WHERE city = 'Kochi';
namemarks
Asha Menon88
Meera Nair95

Comparison operators: =, <> (not equal; != also works in PostgreSQL), >, <, >=, <=.

SQL
SELECT name, year
FROM students
WHERE year = 2;
nameyear
Asha Menon2
Arjun Das2

Sorting with ORDER BY

Without ORDER BY, the database may return rows in any order. Always sort when order matters:

SQL
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:

SQL
SELECT name, marks
FROM students
ORDER BY marks DESC
LIMIT 3;
namemarks
Meera Nair95
Asha Menon88
Priya Shah81

OFFSET skips rows, which lets you show results page by page:

SQL
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:

  1. FROM
  2. WHERE
  3. SELECT
  4. ORDER BY
  5. LIMIT
5-step diagram

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

  1. 1.Using the students table, how many rows does this query return?
    CODE
    SELECT name FROM students WHERE marks > 80;
  2. 2.Which operator means "not equal to" in standard SQL?
  3. 3.What does LIMIT 5 OFFSET 10 return?
0 of 3 answered

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.