SQL (say it "sequel" or "ess-cue-el" — both are fine) is the language for asking a database questions. You describe what you want; the database figures out how to get it. That's the whole trick — you're writing a request, not a program.
Tap a coloured word to see what it controls. You only need three ideas to begin.
SELECT name, price
FROM products
WHERE category = 'Coffee';
SELECT chooses the columns you want to see. Think: “which facts should appear in my answer?”
A database is a set of tables. Each table has named columns (like
spreadsheet headers) and rows (the records). SQL lets you slice, filter, combine and
summarise those rows. Here is the entire products table — this output came from the
real engine, not a screenshot:
Change products to customers, orders, or employees and
run again to meet the other tables:
You write SQL in this order, but the database runs it in a different order (that's Section 5 — the big "aha"). For now, just learn to read a query as an English sentence:
SELECT name, price -- 3. show me these columns FROM products -- 1. from this table WHERE category = 'Tea' -- 2. but only these rows ORDER BY price; -- 4. sorted this way
Out loud: "From products, keep only the Tea rows, then show name and price, sorted by price."
Notice the numbers — your eye reads SELECT first, but the database looks at
FROM and WHERE first. Prove it to yourself:
| Clause | Job | Plain English |
|---|---|---|
SELECT | choose columns | "show me…" |
FROM | choose the table | "…out of this table…" |
WHERE | filter rows | "…only the rows that match…" |
GROUP BY | bundle rows & summarise | "…rolled up per category…" |
ORDER BY | sort the result | "…sorted like this." |
'Tea', not "Tea" or Tea.price > 4, never price > '4' (well — it often works, but don't).; — required when you run several at once.Misspell a column or table on purpose. The engine tells you exactly what went wrong instead of just failing. Learning to read errors is half of learning SQL:
SELECT * mean?* is the wildcard for "all columns." Great for exploring; in real
code you usually name the columns you actually need.Tea would be read as a
column name; double quotes mean different things across databases — single quotes are safe.SELECT name FROM products WHERE price > 4, which clause chooses the rows?WHERE filters rows. SELECT chooses columns,
FROM chooses the table. Keep those three jobs separate in your head.