SELECT Queries in Five Short Beginner Modules

SELECT is the SQL statement you will write more than any other: it reads data. This outline splits it into five short modules. Each one adds a single new idea to the query from the module before.

Practice table

Create this once and use it for every module:

CREATE TABLE movies (id INTEGER PRIMARY KEY, title TEXT, year INTEGER, rating REAL);
INSERT INTO movies (title, year, rating) VALUES
 ('Moon Garden', 2019, 7.5), ('Paper Boats', 2021, 6.8),
 ('Quiet Hill', 2019, 8.1), ('Red Kite', 2023, 7.2);

Module 1: choose columns

SELECT title, year FROM movies;

You list the columns you want after SELECT and the table after FROM. SELECT * means "all columns". It is handy for exploring, but naming columns makes your intent clear.

Exercise: select only the titles.

Module 2: filter rows with WHERE

SELECT title FROM movies WHERE year = 2019;

WHERE keeps only rows where the condition is true. You can combine conditions with AND and OR, and compare with =, <, >, <=, >= and <> (not equal).

Exercise: find movies rated above 7 released after 2019.

Module 3: sort with ORDER BY

SELECT title, rating FROM movies ORDER BY rating DESC;

DESC means highest first; ASC, the default, means lowest first. Without ORDER BY, the database may return rows in any order, so never rely on the order you happen to see.

Exercise: list movies by year, newest first, and by title within the same year.

Module 4: take only a few with LIMIT

SELECT title FROM movies ORDER BY rating DESC LIMIT 2;

LIMIT returns at most that many rows. Combined with ORDER BY it answers "top N" questions. (SQL Server spells this SELECT TOP 2 ... instead.)

Exercise: find the single oldest movie.

Module 5: count and summarise

SELECT year, COUNT(*) AS how_many, AVG(rating) AS average
FROM movies
GROUP BY year;

COUNT, AVG, MIN, MAX and SUM are aggregate functions: they turn many rows into one value. GROUP BY makes one result row per group.

Exercise: count how many movies were released in each year.

How the pieces fit

PartQuestion it answers
SELECTWhich columns?
FROMWhich table?
WHEREWhich rows?
GROUP BYGrouped how?
ORDER BYIn what order?
LIMITHow many?

Write them in that order. A handy way to remember it: you always say what you want, where it lives, and then narrow it down.

Common beginner slips

Next steps

See the whole process drawn out in watching a SELECT query run, step by step, then move on to combining tables with INNER JOIN and LEFT JOIN.

A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.

This page is a free outline. No downloadable files, paid content, courses or certificates are offered on this site.