SQL and its dialects
one standard, many accents. what's portable across postgres, mysql and sqlite, and how this book marks what isn't.
- address
- everything.start.sql_and_its_dialects
- part
- part 1, start here
- chapter
- chapter 7 of 7
- reading time
- 7 min
SQL is an ISO standard (ISO/IEC 9075, latest revision SQL:2023), and every database speaks it with an accent: extra features, missing ones, and a few places where the same query gives different answers.
this book writes postgres 18. when something is postgres-only, or new in a version, or comes from a particular revision of the standard, a small chip says so.
the picture
you know how british and american english are one language, right up until someone asks for chips? SQL is like that. the grammar is shared, the everyday words are shared, and then || means "join these strings" in one place and "or" in another.
that's not a reason to fear it. most of what you write runs everywhere. you just want to know where the accents are, so they don't surprise you in production.
SELECT 3 / 5;
0
0
0.60 (the manual's own example)the manual
postgres and sqlite do integer division on integers, so 3 / 5 is 0. mysql's / always gives a decimal; it has DIV for the integer kind.
one standard
the standard is written by an ISO committee and revised every few years. each revision adds features; almost nothing is ever taken away.
1999recursive queries (WITH RECURSIVE), triggers, and regular expression matching. the year format changes too: SQL:1999, not SQL-99.
nobody implements all of it. the standard splits features into Core (mandatory) and a long optional tail. postgres's own documentation says it conforms to at least 170 of the 177 mandatory Core features of SQL:2023, and that, as of writing, no database claims full Core conformance. that's close enough for this book to teach "the standard" and "postgres" side by side, and point out where they part.
where they agree
a lot. this question uses nothing that isn't SQL-92, so it means the same thing in postgres, mysql, sqlite and nearly anything else:
SELECT country, count(*) AS customers
FROM customers
WHERE city IS NOT NULL
GROUP BY country
HAVING count(*) > 150
ORDER BY customers DESC, country;| countrychar | customersint8 |
|---|---|
| US | 593 |
| DE | 273 |
| GB | 245 |
| IN | 229 |
SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, joins, CASE, CAST, COALESCE, subqueries: most of one table and joins and subqueries live in this shared core.
where they don't
here's the postgres column of the figure, run. every statement is exactly the one in the matrix:
SELECT 'a' || 'b';
SELECT 3 / 5;
SELECT 1 / 0;
SELECT 'Ada' = 'ada';
SELECT 'Ada' LIKE 'ada';
SELECT 'Ada' ILIKE 'ada';
SELECT '42'::int;
SELECT 1 FETCH FIRST 1 ROWS ONLY;| ?column?text |
|---|
| ab |
| ?column?int4 |
|---|
| 0 |
| ?column?bool |
|---|
| f |
| ?column?bool |
|---|
| f |
| ?column?bool |
|---|
| t |
| int4int4 |
|---|
| 42 |
| ?column?int4 |
|---|
| 1 |
CREATE TABLE t (n integer);
INSERT INTO t VALUES ('forty-two');
INSERT INTO t VALUES (7) RETURNING n;
SELECT a.n, b.n FROM t AS a FULL OUTER JOIN t AS b ON a.n = b.n;| nint4 |
|---|
| 7 |
| nint4 | nint4 |
|---|---|
| 7 | 7 |
the differences fall into three kinds, and only one of them is dangerous:
- syntax one side doesn't have (
ILIKE,::,RETURNINGin mysql,FETCH FIRSTin sqlite). harmless: you get an error the first time you try, and you fix it. - types and strictness. postgres refuses
'forty-two'in an integer column; sqlite stores it. mostly harmless, until a report sums a column with words in it. - the same statement, a different answer.
3 / 5is0in postgres and0.60in mysql.'Ada' = 'ada'is false in postgres and true under mysql's default collation.1 / 0is an error in postgres and NULL in the other two. these are the dangerous ones, because nothing fails: a query you moved between databases quietly means something else.
SELECT name FROM customers ORDER BY id. which ending runs unchanged on postgres, sqlite and mysql?even LIMIT has an accent. mysql and sqlite also accept LIMIT 10, 3 (skip 10, then take 3). postgres refuses it, and says what to write instead:
SELECT id, name FROM customers ORDER BY id LIMIT 10, 3;- SELECT id, name FROM customers ORDER BY id
- OFFSET 10 ROWS FETCH FIRST 3 ROWS ONLY;
- SELECT id, name FROM customers ORDER BY id
- LIMIT 3 OFFSET 10;
- SELECT id, name FROM customers ORDER BY id
- LIMIT 10, 3;
SELECT id, name FROM customers ORDER BY id
OFFSET 10 ROWS FETCH FIRST 3 ROWS ONLY;| idint4 | nametext |
|---|---|
| 11 | Ana Lima |
| 12 | Ravi Kumar |
| 13 | Mei Kowalski |
how this book marks it
postgres speaks the standard with a few extensions of its own, and the book uses them when they're the best tool. when it does, it says so, inline, with a chip:
| chip | means | for example |
|---|---|---|
| postgres only | postgres's own, not in the standard | DISTINCT ON, ILIKE, :: casts |
| SQL:2008 | in the standard since that revision | FETCH FIRST SQL:2008, window functions SQL:2003, WITH RECURSIVE SQL:1999 |
| pg 18+ | needs at least that postgres version | RETURNING with OLD and NEW |
| postgres 19 | in the next postgres, not released yet | can't run on this page's 18.6 |
no chip means it's plain, shared SQL. and "not in the standard" isn't the same as "postgres only": LIMIT is neither standard nor postgres's alone, and RETURNING is a postgres extension that sqlite modelled its own on (since 3.35). when that matters, the text says it.
keylines
- SQL is an ISO standard. the latest revision is SQL:2023, and nobody implements all of it.
- the core (SELECT, joins, GROUP BY, subqueries) is the same everywhere.
- the dangerous differences don't error: integer division, case sensitivity, division by zero.
- portable and standard aren't the same: LIMIT is neither standard nor rare.
- chips in this book mark postgres-only features, standard revisions and version requirements.