Skip to content
CodeItRaw
SQL: Select and Filter

Lesson 1/3

A database keeps data in tables: each row is a record (a book, an employee) and each column is a property of that record (its title, its price). In SQL you write what you want, not how to find it; the database decides how to go through the rows.

SELECT title, price
FROM books
ORDER BY price DESC
LIMIT 5;
FROM says which table, SELECT which columns. ORDER BY sorts the result: left alone it runs small to large (ASC), with DESC large to small. LIMIT 5 keeps the first five rows. This example lists the five most expensive books.

A query does not run in the order it is written: FROM first (take the table), then SELECT (pick the columns), then ORDER BY (sort), and LIMIT last (cut). So LIMIT is always applied after the sorting, and "the top three" is written ORDER BY ... DESC LIMIT 3.

Tasks

Tasks open in order. Solve them all and the next lesson opens.

  1. 01

    First query

    SQL query

  2. 02

    Top three salaries

    SQL query

If you would rather not wait for the order, every problem is open without locks: Problem list