Getting Started
A database, a table, and your first query.
You describe the result you want. The engine decides how to get it.
What SQL is for
One statement replaces the loop you would write in any other language:
for row in rows: SELECT name FROM product
if row.stock == 0: WHERE stock = 0
print(row.name)
There is no loop, no index variable and no order of execution to follow. That is what declarative means, and it is the whole adjustment.
Getting an engine
Three engines, and the first thirteen chapters run the same on all of them:
sqlite3 --version
docker run -d --name pg -e POSTGRES_PASSWORD=pw \
-p 5432:5432 postgres:18-alpine
docker run -d --name my -e MYSQL_ROOT_PASSWORD=pw \
-p 3306:3306 mysql:9-oraclesqlite3 ships with macOS and most Linux distributions, so it needs no install at all. Pick
one engine and stay with it.
Opening a client
Each engine has a command-line client. This is the whole connection:
sqlite3 shop.db
docker exec -it pg psql -U postgres -d shop
docker exec -it my mysql -uroot -ppw shopsqlite3 shop.db creates the file if it does not exist. The other two connect to the
containers started above.
Making the output readable
SQLite prints pipe-separated values by default, which matches no snippet in this course:
.mode table
.headers onRun those two once per session, or put them in ~/.sqliterc to make them permanent. psql
and mysql already print boxed tables.
Finding your way around
Four things you will want constantly, spelled differently by each client:
| Want | sqlite3 | psql | mysql |
|---|---|---|---|
| List tables | .tables |
\dt |
SHOW TABLES; |
| Describe a table | .schema product |
\d product |
DESCRIBE product; |
| Run a file | .read f.sql |
\i f.sql |
SOURCE f.sql; |
| Quit | .quit |
\q |
exit |
.tables # sqlite3
\dt # psql
SHOW TABLES; # mysqlYou can also run one query without opening a session at all, which is how every snippet in this course was verified:
sqlite3 shop.db "SELECT COUNT(*) FROM product;"
psql -U postgres -d shop -c "SELECT COUNT(*) FROM product;"
mysql -uroot -ppw -t shop -e "SELECT COUNT(*) FROM product;"CREATE TABLE
A table is columns with types. Nothing exists until you make it:
CREATE TABLE product (
id INTEGER,
name VARCHAR(40),
price DECIMAL(7,2)
);
INSERT INTO product VALUES (1, 'Keyboard', 49.99);
INSERT INTO product VALUES (2, 'Mouse', 19.95);
SELECT * FROM product;Three types cover most columns: INTEGER, VARCHAR(n) for text, and DECIMAL(7,2) for money
— seven digits in total, two after the point.
INSERT
Listing the columns is longer and better, because it survives a schema change:
CREATE TABLE product (
id INTEGER,
name VARCHAR(40),
price DECIMAL(7,2)
);
INSERT INTO product (id, name, price) VALUES
(3, 'Monitor', 189.95),
(4, 'Headset', 79.25);
SELECT * FROM product;One INSERT can carry many rows, separated by commas. Text goes in single quotes.
SELECT
Name the columns you want, in the order you want them:
SELECT name, price FROM product;SELECT * gives every column, which is fine while exploring:
SELECT * FROM product WHERE id = 3;The table used from here
Every chapter from here queries this table, already filled:
SELECT * FROM product;CREATE TABLE product (
id INTEGER,
name VARCHAR(40),
category VARCHAR(20),
price DECIMAL(7,2),
stock INTEGER
);
INSERT INTO product VALUES
(1, 'Keyboard', 'input', 49.99, 12),
(2, 'Mouse', 'input', 19.95, 40),
(3, 'Monitor', 'display', 189.95, 7),
(4, 'Headset', 'audio', 79.25, 0),
(5, 'Webcam', 'video', 64.75, 15);Run that once in your engine and the rest of the course works.
Reading an error
The same typo, three messages. Only Postgres guesses what you meant:
SELECT naem FROM product;All three name the bad identifier, which is the part worth reading. The code 42S22 is a
standard SQLSTATE; the 1054 is MySQL’s own.
Comments and case
Keywords are case-insensitive. The convention is upper for keywords, lower for your names:
-- out of stock, so it needs reordering
/* a block comment
spans lines */
select name, price from product where stock = 0;That query ran in lower case. Written the conventional way it reads as SELECT name, price FROM product WHERE stock = 0; — same result, easier to scan.
Semicolons
One statement, one ;. It is what tells the CLI you have finished typing:
CREATE TABLE product (id INTEGER, name VARCHAR(40));
INSERT INTO product VALUES (1, 'Keyboard'), (2, 'Mouse');
SELECT id, name FROM product;Three statements, three semicolons, one result. Without the final ; the CLI waits for more
input rather than running anything.
Whitespace is free, so splitting a long query across lines is the normal style.