SQL Syntax
An SQL program is made of statements. Each statement is a complete command — read some rows, insert a row, create a table — and normally ends with a semicolon.
Anatomy of a statement
SELECT name, price -- clause: which columns
FROM products -- clause: which table
WHERE stock = 0 -- clause: which rows
ORDER BY name; -- clause: in which orderA statement is built from clauses (SELECT …, FROM …, WHERE …). Inside the clauses you use keywords (SELECT, FROM), identifiers (table and column names), literals (0, 'Berlin') and operators (=, <, +).
SELECT name, price
FROM products
WHERE stock = 0
ORDER BY name;name | price
----------------+------
Avocados | 4.5
Sparkling Water | 4.6
(2 rows)
Rules of thumb
| Rule | Details |
|---|---|
| Keywords are case-insensitive | SELECT, select and Select are the same. Writing keywords in UPPER CASE is a common convention. |
| Whitespace is free | Line breaks and indentation are ignored; use them to make queries readable. |
| Semicolons separate statements | Required between statements; optional after the last one in many tools. |
| Text literals use single quotes | 'Berlin', and a quote inside text is doubled: 'Nonna''s Kitchen'. |
| Identifiers may be quoted | "order" (standard/SQLite/PostgreSQL), `order` (MySQL), [order] (SQL Server) — needed only for reserved words or spaces. |
| Comments | -- to end of line and /* block */ |
These two queries are identical to the database:
select NAME, CITY from CUSTOMERS where COUNTRY = 'USA' order by name;
SELECT name,
city
FROM customers
WHERE country = 'USA'
ORDER BY name;name | city
----------------+---------
Night Owl Books | New York
Tech Nest | Austin
(2 rows)
name | city
----------------+---------
Night Owl Books | New York
Tech Nest | Austin
(2 rows)
Note: Keywords and column names are case-insensitive, but text values are not in comparisons:
country = 'usa'would find nothing. SQLite compares text exactly unless you ask otherwise (see the LIKE lesson).
Quotes inside text
Use two single quotes to write one quote character inside a string:
SELECT customer_id, name, city
FROM customers
WHERE name = 'Nonna''s Kitchen';customer_id | name | city
------------+-----------------+-------
3 | Nonna's Kitchen | London
(1 row)
Double quotes mean "identifier" in standard SQL, not text. SQLite tolerates "Berlin" as text when no column of that name exists, but other databases reject it — always use single quotes for strings.
Main kinds of statements
| Group | Statements | Purpose |
|---|---|---|
| Query | SELECT |
Read data |
| DML (Data Manipulation) | INSERT, UPDATE, DELETE |
Change rows |
| DDL (Data Definition) | CREATE, ALTER, DROP |
Change structure |
| Transaction control | BEGIN, COMMIT, ROLLBACK |
Group changes safely |
Several statements in one run
A script may mix kinds. The playground shows one result block per statement that produces output; an error stops the script at that point:
UPDATE products SET stock = stock + 10 WHERE product_id = 4;
SELECT name, stock FROM products WHERE product_id = 4;
SELECT nme FROM products;
SELECT 'never reached';1 row affected.
name | stock
----------------+------
Sparkling Water | 10
(1 row)
Error: no such column: nme
The misspelled column nme raises an error, and the last statement never runs.
Test yourself
Which of these is a valid text literal in standard SQL?
- "Berlin"
- 'Berlin'
Berlin- [Berlin]
How do you write the text It's as an SQL string?
- 'It\'s'
- 'It''s'
- "It's"
- 'It's'
Are SQL keywords case-sensitive?
- Yes, always upper case
- Yes, always lower case
- No
- Only in SQLite