Original study
PostgreSQL vs SQLite: what differs, measured on 240 real queries
PostgreSQL 18.6 (PGlite 0.5.16) and SQLite 3.54.1 · measured · recomputed on every release
Short answer
Of 240 graded queries in a beginner-to-advanced SQL course, 28 (11.7%) needed different SQL in PostgreSQL and SQLite, almost all of them because of dates. The other 212 ran unchanged on both, and the results matched, apart from formatting such as true versus 1.
240
graded queries, each run on both engines
11.7%
needed different SQL
57 of 85
function edge cases behave the same
15 of 42
beginner mistakes only PostgreSQL reports
How we measured
sqlstreak's course has 240 graded query exercises across 16 units, from a first SELECT to window functions, recursive queries and analytics. Every exercise has an official solution, and every solution is executed on both engines each time the site is built; a build fails if the two engines disagree.
When a solution couldn't be written once for both, the course stores one per engine. Counting those, and comparing the raw rows each engine returns for the shared ones, gives the numbers on this page. They are recomputed from scratch on every release, with the engine versions shown above.
Caveats: this is one course's queries on three small sample databases, not a survey of every feature. It shows what a learner meets on the way from SELECT to window functions.
Which queries needed different SQL
| What differed | Queries | Units |
|---|---|---|
| Dates and times | 24 | Text and dates, Recursive queries, Analytics patterns, Interview pack |
| ROLLUP subtotals | 3 | Advanced windows |
| Text functions | 1 | Text and dates |
PostgreSQL
SELECT name, EXTRACT(YEAR FROM arrived_on) AS arrival_year FROM pets;SQLite
SELECT name, strftime('%Y', arrived_on) AS arrival_year FROM pets;PostgreSQL
SELECT status, COUNT(*) AS orders FROM orders GROUP BY ROLLUP(status);SQLite
SELECT status, COUNT(*) AS orders FROM orders GROUP BY status UNION ALL SELECT NULL, COUNT(*) FROM orders;PostgreSQL
SELECT split_part(name, ' ', 1) AS first_name FROM customers;SQLite
SELECT substr(name, 1, instr(name, ' ') - 1) AS first_name FROM customers;The teaching material shows the same pattern: 16 of the course's 146 lesson examples and 13 of the 102 guide examples are written once per engine. The rest run unchanged on both.
When the SQL was the same, were the results?
For the 212 queries that ran unchanged, the course's grader accepted both engines' results every time. Byte for byte, this is how they compared:
| Raw results | Queries |
|---|---|
| Byte-for-byte identical | 147 |
| Same rows, different order (the query had no ORDER BY, so both orders are correct) | 54 |
| true/false in PostgreSQL, 1/0 in SQLite | 9 |
| The same number to six decimal places, different floating-point digits | 2 |
Function edge cases
The function reference runs tricky inputs through both engines. Of 85 cases that exist in both, 28 return something different:
- strftime — Day of week (0 = Sunday)
- strftime — Week number (they count weeks differently)
- strftime — Seconds since 1970
- strftime — An impossible date
- date — Plus 1 month from 31 January
- date — 30 February
- date — Text that isn't a date
- julianday — A date
- julianday — Hours between two timestamps
- julianday — Text that isn't a date
- string_agg / group_concat — Numbers instead of text
- string_agg / group_concat — DISTINCT with a separator
- string_agg / group_concat — group_concat
- substr — Negative start
- substr — Negative length
- instr / strpos / position — The standard POSITION syntax
- COALESCE / IFNULL — Mixed types
- COALESCE / IFNULL — IFNULL
- NULLIF — Number vs text
- CAST — Number followed by junk
- CAST — Decimal text to integer
- CAST — Decimal number to integer
- CAST — Date text to DATE
- CAST — Text with no number at all
- CAST — 1 to boolean
- ROUND — A tricky decimal
- ROUND — Negative digits
- ROUND — A double precision value with digits
Mistakes only PostgreSQL reports
Of 42 common beginner mistakes, 15 raise an error in PostgreSQL but run in SQLite, usually returning something plausible and wrong. Code tested only on SQLite can carry these bugs for a long time.
- column "x" must appear in the GROUP BY clause or be used in an aggregate function
- column "cat" does not exist (double quotes vs single quotes)
- column "age_months" does not exist (using an alias in WHERE)
- operator does not exist: integer = text
- invalid input syntax for type integer: "3 years"
- division by zero
- more than one row returned by a subquery used as an expression
- argument of WHERE must be type boolean, not type numeric
- UNION types text and integer cannot be matched
- invalid input syntax for type date: "March 1"
- for SELECT DISTINCT, ORDER BY expressions must appear in select list
- column "age" is of type integer but expression is of type text
- value too long for type character varying(3)
- LIMIT #,# syntax is not supported (PostgreSQL)
- relation "n" does not exist (a recursive CTE without RECURSIVE)
The raw data
One row per query, with both solutions where they differ. Free to reuse under CC BY 4.0 with a link to this page.