NO. 01 · 7 exercises

1. First Queries

What a table is, and how to ask it for things.

The lesson

First Queries

Every conversation with a database starts the same way: you ask a question, and it answers with a table. It doesn't matter how complicated the question gets — the answer is always rows and columns. Once you see that, everything else in SQL is just learning better ways to ask.

This first chapter is about the simplest questions of all: show me what's in here.

The lay of the land

A database is a set of tables. Think of each table as a spreadsheet with strict rules: every column has a name and holds one kind of thing, and every row is one record.

Our record store has ten tables. The ones you'll meet first:

TableWhat's inside
artistsBands and musicians
albumsAlbums, each belonging to one artist
tracksIndividual songs, each on one album

Take artists. It has four columns — id, name, country, formed_year — and twelve rows, one per artist. The id is just a number that uniquely identifies each row; almost every table has one, and it becomes very important later.

Asking for everything: SELECT *

The most basic SQL query has two parts: what columns you want (SELECT), and from which table (FROM).

SELECT * FROM artists;

The * is shorthand for "all the columns". This reads exactly like English: select everything from artists. The database answers with the whole table — twelve rows, four columns.

Two small conventions worth noticing:

  • SQL keywords are usually written in uppercase. The database doesn't care — select * from artists; works too — but uppercase makes queries easier to scan, and it's the convention everywhere.
  • The semicolon ends the statement. It's good hygiene to always include it.

Choosing columns

Asking for everything is great for exploring, but real questions are usually pickier. Replace the * with a column name:

SELECT name FROM artists;

Now the answer is one column wide: just the twelve names. Want two columns? Separate them with commas:

SELECT name, country FROM artists;

The order you list columns is the order they come back in. SELECT country, name is perfectly valid — it just returns the columns flipped.

Renaming things: AS

The headers of your result don't have to match the table. You can rename any column with AS:

SELECT name AS artist, country AS homeland
FROM artists;

Why bother? Sometimes the table's name is cryptic, sometimes you're exporting data for a tool that expects specific headers, and sometimes — as you'll see below — the column is computed and needs a name at all. The renamed column is called an alias.

Computing new columns

SELECT isn't limited to copying columns — it can compute new ones on the fly. The tracks table stores durations in seconds, which nobody thinks in. Fix it in the query:

SELECT title, duration_sec / 60.0 AS minutes
FROM tracks;

Every row's duration_sec gets divided by sixty, and the result shows up as a new column called minutes. The table itself is untouched — you're only changing the answer, never the data.

Watch out for integer division. In SQL, dividing two integers throws away the decimals: 5 / 2 is 2, not 2.5. Writing 60.0 (with the decimal point) forces a real division. This bites everyone once.

No repeats: DISTINCT

Ask for the countries in artists and you'll get twelve rows — with the USA, Spain and the UK appearing twice each. Usually you want the different values, each once:

SELECT DISTINCT country FROM artists;

DISTINCT sits right after SELECT and collapses duplicate rows in the answer. Nine rows come back instead of twelve. It applies to the whole row, so SELECT DISTINCT country, name would only collapse rows where both columns repeat — with unique names, that's every row again.

What you just learned

  • SELECT * FROM table shows everything; naming columns picks just those.
  • AS renames any column, and SELECT can compute new columns from old ones.
  • DISTINCT removes duplicate rows from the answer.
  • Nothing in this chapter changed the database. SELECT only reads.

Dialect note. This course runs on SQLite, the same engine that ships inside every phone and browser. Everything in this chapter works identically in PostgreSQL, MySQL and SQL Server. When something does differ between databases, we'll flag it in a note like this one.

Time to write some queries. The exercises below assume you've read this far — each one takes under a minute if you have.