Skip to content
Sign inGet all access
Chapter 1·18 minFree

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:

terminal
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-oracle
Output$ sqlite3 --version3.54.0$ 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-oracle

sqlite3 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:

terminal
sqlite3 shop.db
docker exec -it pg psql -U postgres -d shop
docker exec -it my mysql -uroot -ppw shop
Output$ sqlite3 shop.dbSQLite version 3.54.0sqlite>$ docker exec -it pg psql -U postgres -d shoppsql (18.7)shop=#$ docker exec -it my mysql -uroot -ppw shopmysql>

sqlite3 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:

terminal
.mode table
.headers on
Outputsqlite> SELECT id, name FROM product WHERE id <= 2;1|Keyboard2|Mousesqlite> .mode tablesqlite> .headers onsqlite> SELECT id, name FROM product WHERE id <= 2;+----+----------+| id | name |+----+----------+| 1 | Keyboard || 2 | Mouse |+----+----------+

Run 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
terminal
.tables      # sqlite3
\dt          # psql
SHOW TABLES; # mysql
Outputsqlite> .tablesproductshop=# \dt List of tables Schema | Name | Type | Owner--------+---------+-------+---------- public | product | table | postgresmysql> SHOW TABLES;+----------------+| Tables_in_shop |+----------------+| product |+----------------+

You can also run one query without opening a session at all, which is how every snippet in this course was verified:

terminal
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;"
Output$ sqlite3 shop.db "SELECT COUNT(*) FROM product;"5$ psql -U postgres -d shop -c "SELECT COUNT(*) FROM product;" count------- 5(1 row)$ mysql -uroot -ppw -t shop -e "SELECT COUNT(*) FROM product;"+----------+| COUNT(*) |+----------+| 5 |+----------+

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;
Output+----+----------+-------+| id | name | price |+----+----------+-------+| 1 | Keyboard | 49.99 || 2 | Mouse | 19.95 |+----+----------+-------+

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;
Output+----+---------+--------+| id | name | price |+----+---------+--------+| 3 | Monitor | 189.95 || 4 | Headset | 79.25 |+----+---------+--------+

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;
Output+----------+--------+| name | price |+----------+--------+| Keyboard | 49.99 || Mouse | 19.95 || Monitor | 189.95 || Headset | 79.25 || Webcam | 64.75 |+----------+--------+

SELECT * gives every column, which is fine while exploring:

SELECT * FROM product WHERE id = 3;
Output+----+---------+----------+--------+-------+| id | name | category | price | stock |+----+---------+----------+--------+-------+| 3 | Monitor | display | 189.95 | 7 |+----+---------+----------+--------+-------+

The table used from here

Every chapter from here queries this table, already filled:

SELECT * FROM product;
Output+----+----------+----------+--------+-------+| id | name | category | price | stock |+----+----------+----------+--------+-------+| 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 |+----+----------+----------+--------+-------+
shop.sql
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:

terminal
SELECT naem FROM product;
OutputsqliteParse error near line 1: no such column: naempostgresERROR: column "naem" does not existHINT: Perhaps you meant to reference the column "product.name".mysqlERROR 1054 (42S22): Unknown column 'naem' in 'field list'

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;
Output+---------+-------+| name | price |+---------+-------+| Headset | 79.25 |+---------+-------+

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;
Output+----+----------+| id | name |+----+----------+| 1 | Keyboard || 2 | Mouse |+----+----------+

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.

Tables