skip to content

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.

the statement
SELECT 3 / 5;
postgres 18.6 · ran here
0
sqlite 3.54.1 · recorded
0
mysql 8.4 · the manual says
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.

postgres: this page's goldens · sqlite: the official 3.54.1 wasm build, recorded · mysql: the 8.4 manual, not run
pick a statement. postgres ran it here (the blocks below), sqlite 3.54.1 was run and recorded by the author, and the mysql column is what the 8.4 manual says, with a link, because no mysql ran for this page. red is an error.

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.

drawn by hand · postgresql.org's SQL conformance page, and the standard's revision history
click a revision, or focus the timeline and use the arrow keys. what each one is best known for.

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:

sqldb=shop
SELECT country, count(*) AS customers
FROM customers
WHERE city IS NOT NULL
GROUP BY country
HAVING count(*) > 150
ORDER BY customers DESC, country;
output, from postgres 18.6
SELECT 4
countrycharcustomersint8
US593
DE273
GB245
IN229
(4 rows)

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:

sqlempty db
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;
output, from postgres 18.6
#1 · SELECT 1
?column?text
ab
(1 row)
#2 · SELECT 1
?column?int4
0
(1 row)
#3 · ERROR 22012
ERROR 22012: division by zero
#4 · SELECT 1
?column?bool
f
(1 row)
#5 · SELECT 1
?column?bool
f
(1 row)
#6 · SELECT 1
?column?bool
t
(1 row)
#7 · SELECT 1
int4int4
42
(1 row)
#8 · SELECT 1
?column?int4
1
(1 row)
sqlempty db
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · ERROR 22P02
ERROR 22P02: invalid input syntax for type integer: "forty-two"
2 │ INSERT INTO t VALUES ('forty-two');
at line 2, column 23
#3 · INSERT 0 1
nint4
7
(1 row)
#4 · SELECT 1
nint4nint4
77
(1 row)

the differences fall into three kinds, and only one of them is dangerous:

  • syntax one side doesn't have (ILIKE, ::, RETURNING in mysql, FETCH FIRST in 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 / 5 is 0 in postgres and 0.60 in mysql. 'Ada' = 'ada' is false in postgres and true under mysql's default collation. 1 / 0 is 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.
quick check
you want the first three rows of 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:

sqldb=shop
SELECT id, name FROM customers ORDER BY id LIMIT 10, 3;
output, from postgres 18.6
ERROR 42601: LIMIT #,# syntax is not supported
1 │ SELECT id, name FROM customers ORDER BY id LIMIT 10, 3;
at line 1, column 44
HINT: Use separate LIMIT and OFFSET clauses.
the standard, SQL:2008 · postgres
  1. SELECT id, name FROM customers ORDER BY id
  2. OFFSET 10 ROWS FETCH FIRST 3 ROWS ONLY;
postgres, mysql, sqlite
  1. SELECT id, name FROM customers ORDER BY id
  2. LIMIT 3 OFFSET 10;
mysql, sqlite (not postgres)
  1. SELECT id, name FROM customers ORDER BY id
  2. LIMIT 10, 3;
the same page of results (rows 11 to 13), three spellings. the first is the standard's, the second works in all three databases, the last only in mysql and sqlite.
sqldb=shop
SELECT id, name FROM customers ORDER BY id
OFFSET 10 ROWS FETCH FIRST 3 ROWS ONLY;
output, from postgres 18.6
SELECT 3
idint4nametext
11Ana Lima
12Ravi Kumar
13Mei Kowalski
(3 rows)

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:

chipmeansfor example
postgres onlypostgres's own, not in the standardDISTINCT ON, ILIKE, :: casts
SQL:2008in the standard since that revisionFETCH FIRST SQL:2008, window functions SQL:2003, WITH RECURSIVE SQL:1999
pg 18+needs at least that postgres versionRETURNING with OLD and NEW
postgres 19in the next postgres, not released yetcan'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.