Meet SQL: Your First Query
Write your first SQL query and learn the four things SQL can do.
You know data lives in tables of rows and columns. Now the fun part: asking for it.
Here is a complete, real SQL query:
SELECT * FROM users;Read it out loud: "Select everything from the users table." That's SQL's superpower — it reads almost like English.
SELECT— "I want to read data"*— "all the columns"FROM users— "from the table called users";— "end of statement" (like a period ending a sentence)
Run it, and the database hands back every row and every column of the users table.
The users table has 3 rows (Alice, Bob, Carol) and 4 columns. What does this query return?
SELECT * FROM users;- AAll 3 rows with all 4 columns
- BJust the first row
- CJust the column names, no data
- DAn error — you must name the columns
A query like that is called a statement — one complete instruction to the database. The words SELECT and FROM are keywords: SQL's fixed vocabulary. users is a name you chose when creating the table.
Two style rules you'll see everywhere (and should copy):
-- Convention: KEYWORDS in caps, names in lowercase
SELECT * FROM users;
-- SQL itself doesn't care — this works too, but it's harder to read:
select * from users;SQL keywords are case-insensitive. The UPPERCASE convention exists purely for humans: your eye instantly separates SQL's grammar from your table and column names.
One more nicety:
- SQL ignores whitespace — so you can format a query however reads best
Complete your first query — read all columns from the products table:
___ * FROM products;SQL can do four things with data — the famous CRUD operations:
-- C — Create: add new rows
INSERT INTO users (name, email) VALUES ('Dan', 'dan@example.com');
-- R — Read: ask questions (you just did this!)
SELECT * FROM users;
-- U — Update: change existing rows
UPDATE users SET age = 29 WHERE id = 1;
-- D — Delete: remove rows
DELETE FROM users WHERE id = 3;Don't memorize the last three yet — each gets its own lesson later. Just know the map: INSERT, SELECT, UPDATE, DELETE.
SELECTgets most of your attention — reading data is ~95% of real-world SQL work
Which SQL operation reads data without changing anything?
- ASELECT
- BINSERT
- CUPDATE
- DDELETE
One last big idea before you start querying for real. SQL is declarative: you describe what you want, not how to find it.
In most programming languages you'd write a loop: "go through each user, check the age, add matches to a list…" In SQL you just state the goal:
SELECT * FROM users WHERE age >= 18;…and the database's query planner figures out the fastest way to get it — which shortcuts to use, what to scan, how to sort. You declare; it optimizes. That's why one line of SQL can replace fifty lines of loop code.
- The
WHERE age >= 18bit — your first glimpse of filtering
What does it mean that SQL is 'declarative'?
- AYou describe the result you want; the database decides how to compute it
- BYou must declare every variable before using it
- CQueries must be written in uppercase
- DYou write step-by-step loops over each row