Sub topic 15
Where Database Systems Differ
This chapter assumes you know the rules from the earlier chapters: three-valued logic, IS NULL, COALESCE, how aggregates skip NULLs, and the NOT IN trap. The core rules are the same in every mainstream database. The differences are in syntax, defaults, and a few surprising corners. They matter most when you port a query or share SQL across systems.
The examples reuse the employees table from earlier in the tutorial. The systems discussed are PostgreSQL, MySQL, SQL Server, Oracle, and SQLite. Version numbers are given where support changed. Check your own version, because vendors keep adding standard syntax.
What Stays the Same
These behaviors are consistent everywhere, so you can rely on them:
commission = NULLevaluates to UNKNOWN, andWHEREkeeps only TRUE rows.COUNT(column),SUM, andAVGskip NULLs.COUNT(*)counts rows.GROUP BYandDISTINCTput all NULLs into a single group.NOT INwith a NULL in the list never returns TRUE.- A
CHECKconstraint passes when its condition is UNKNOWN, soCHECK (commission >= 0)accepts NULL. COALESCEandNULLIFare standard SQL and work in all five systems.
Keep these in mind when porting. The differences below are the exceptions.
Null-Safe Comparison
IS DISTINCT FROM is standard SQL, but support arrived at different times. Each system has its own idiom for "equal, treating two NULLs as equal".
| System | Null-safe equality | Null-safe inequality |
|---|---|---|
| PostgreSQL | `a IS NOT DISTINCT FROM b` | `a IS DISTINCT FROM b` |
| SQL Server 2022+ | `a IS NOT DISTINCT FROM b` | `a IS DISTINCT FROM b` |
| MySQL | `a <=> b` | `NOT (a <=> b)` |
| SQLite | `a IS b` (also `IS NOT DISTINCT FROM` since 3.39) | `a IS NOT b` |
| Oracle 23ai | `a IS NOT DISTINCT FROM b` | `a IS DISTINCT FROM b` |
Older Oracle and SQL Server versions have no operator. A portable fallback is an explicit expression:
-- Works on any system: "commission is different from 100, NULL counts as different".
SELECT name
FROM employees
WHERE commission <> 100 OR commission IS NULL;This returns Ada, Cy, and Di, the same as IS DISTINCT FROM 100. For comparing two columns, you need more tests:
-- Portable "a IS NOT DISTINCT FROM b" for two nullable columns.
SELECT e.name
FROM employees e
JOIN employees m ON m.id = e.manager_id
WHERE e.commission = m.commission
OR (e.commission IS NULL AND m.commission IS NULL);In SQLite, IS works on any values, not only NULL. This surprises people who read x IS 5 as a syntax error from another database.
Default Sort Position of NULLs
SQL does not say whether NULLs sort before or after other values, only that they must group together at one end. Systems chose differently.
| System | NULLs in `ASC` order | NULLs in `DESC` order | `NULLS FIRST/LAST` |
|---|---|---|---|
| PostgreSQL | Last | First | Supported |
| Oracle | Last | First | Supported |
| MySQL | First | Last | Not supported |
| SQL Server | First | Last | Not supported |
| SQLite | First | Last | Supported since 3.30 |
PostgreSQL and Oracle treat NULL as larger than any value. MySQL, SQL Server, and SQLite treat it as smaller. A query with ORDER BY commission DESC therefore puts Ada and Cy at the top in PostgreSQL and at the bottom in MySQL.
The portable fix for MySQL and SQL Server is to sort on a NULL flag first:
-- MySQL: NULLs last, then highest commission first.
SELECT name, commission
FROM employees
ORDER BY commission IS NULL, commission DESC;
-- SQL Server: no boolean expressions in ORDER BY, so use CASE.
SELECT name, commission
FROM employees
ORDER BY CASE WHEN commission IS NULL THEN 1 ELSE 0 END,
commission DESC;The CASE form also works in PostgreSQL, Oracle, and SQLite. If a query's output order matters and the code may move between systems, spell the order out and do not depend on the default.
Oracle and the Empty String
Oracle is the largest outlier. In Oracle, a zero-length string is NULL:
-- Oracle: returns 1 row (the empty string became NULL).
SELECT 'is null' AS result FROM dual WHERE '' IS NULL;In PostgreSQL, MySQL, SQL Server, and SQLite, '' is an ordinary non-NULL value, and '' IS NULL is FALSE. The consequences in Oracle:
- A
NOT NULLcolumn rejects''on insert. WHERE name = ''never matches, because it compares to NULL.LENGTH('')returns NULL, not 0.- Concatenating NULL with a string (
'a' || NULL) returns'a', because Oracle treats NULL as an empty string in||.
Code that distinguishes "no value" from "empty value" cannot be ported to Oracle without redesign.
The same example shows another Oracle quirk. Before Oracle 23ai, every SELECT needs a FROM clause, so the earlier SELECT 100 / NULLIF(0, 0) becomes SELECT 100 / NULLIF(0, 0) FROM dual.
Concatenation and NULL
Standard SQL says || with a NULL operand yields NULL. Databases disagree on how to apply that to the functions around it.
| Expression | PostgreSQL | MySQL | SQL Server | Oracle | Column | Column | Column | Column | Column | Column |
|---|---|---|---|---|---|---|---|---|---|---|
| `'a' \ | \ | NULL` | NULL | `\ | \ | ` is OR by default | `\ | \ | ` not supported | `'a'` |
| `'a' + NULL` | error | numeric addition | NULL | error | ||||||
| `CONCAT('a', NULL)` | `'a'` | NULL | `'a'` | `'a'` | ||||||
| `CONCAT_WS('-', 'a', NULL)` | `'a'` | `'a'` | `'a'` | not available |
Read the table by row: CONCAT ignores NULLs in PostgreSQL, SQL Server, and Oracle, but returns NULL in MySQL. In SQL Server, the + operator returns NULL by default because the setting CONCAT_NULL_YIELDS_NULL is ON. Turning it OFF is deprecated. If you need predictable results, wrap each nullable piece in COALESCE(piece, '').
Names for the Two-Argument Replacement
COALESCE is the portable choice. Most vendors also ship a shorter, non-standard function:
| System | Function | Notes |
|---|---|---|
| Oracle | `NVL(a, b)` | Two arguments only |
| SQL Server | `ISNULL(a, b)` | Result takes the type of `a`, which can truncate `b` |
| MySQL | `IFNULL(a, b)` | Two arguments only |
| SQLite | `IFNULL(a, b)` | Two arguments only |
The SQL Server type rule is a real gotcha. ISNULL(@short_varchar5, 'longer text') truncates the replacement to five characters. COALESCE picks a type from all its arguments and avoids this. The trade-off is that COALESCE may evaluate its arguments differently in edge cases, such as subqueries, so test it if performance matters.
UNIQUE Constraints and Multiple NULLs
Standard SQL says two NULLs are not equal, so a UNIQUE column may hold many NULLs. PostgreSQL, MySQL (InnoDB), Oracle, and SQLite follow this. SQL Server does not: its unique constraint and unique index treat NULLs as equal and allow only one.
CREATE TABLE badges (
id INTEGER PRIMARY KEY,
code VARCHAR(10) UNIQUE
);
INSERT INTO badges (id, code) VALUES (1, NULL);
INSERT INTO badges (id, code) VALUES (2, NULL); -- succeeds in PostgreSQL, MySQL, Oracle, SQLite; fails in SQL ServerYou can change the behavior in either direction:
-- PostgreSQL 15+: allow only one NULL.
CREATE TABLE badges_one_null (
id INTEGER PRIMARY KEY,
code VARCHAR(10),
UNIQUE NULLS NOT DISTINCT (code)
);
-- SQL Server: allow many NULLs with a filtered unique index.
CREATE UNIQUE INDEX ux_badges_code
ON badges (code)
WHERE code IS NOT NULL;Check this behavior before you write a "one optional value per row" rule, such as an optional email address that must be unique.
Other Differences to Know
- Division by zero. PostgreSQL, SQL Server, and Oracle raise an error. MySQL returns NULL for a
SELECT(with a warning) unless strict mode applies to the statement. UseNULLIF(divisor, 0)to get the same result everywhere. - Legacy comparison switches. SQL Server has
SET ANSI_NULLS OFF, which makes= NULLbehave likeIS NULL. It is deprecated, and code written for it breaks on other systems. - Type of a bare NULL.
SELECT NULLhas no type. PostgreSQL may demand a cast in some contexts, such asNULL::integer. The standard formCAST(NULL AS INTEGER)works everywhere. - Null-aware indexing. Oracle's B-tree indexes do not store rows where all indexed columns are NULL, so
WHERE col IS NULLmay not use a single-column index. Other systems index NULLs.
A Porting Checklist
- Replace
IS DISTINCT FROM,<=>, andISwith an explicitOR ... IS NULLexpression if you target several systems. - Add
NULLS LASTor aCASEsort key to everyORDER BYwhose NULL position matters. - Wrap nullable pieces in
COALESCEbefore concatenating. - Decide how
UNIQUEcolumns should treat NULLs, and test it on each system. - Audit any logic that depends on
''and NULL being different values, especially when Oracle is involved.
Exercise
A table tickets(id, closed_at) has these values for closed_at: 2026-01-05, NULL, 2026-03-01, NULL. You run SELECT id FROM tickets ORDER BY closed_at DESC first on PostgreSQL, then on MySQL. Where do the NULL rows appear in each result, and how would you write one query that gives the PostgreSQL ordering on both systems?
Show answer
PostgreSQL treats NULL as larger than any value, so in DESC order the NULL rows come first. MySQL treats NULL as smaller, so in DESC order they come last. To get NULLs first on both, sort on a NULL flag descending and then on the column: ORDER BY CASE WHEN closed_at IS NULL THEN 1 ELSE 0 END DESC, closed_at DESC. That works on both systems, and PostgreSQL can also use ORDER BY closed_at DESC NULLS FIRST.
Exercise
A users table has email VARCHAR(100) UNIQUE, and several users have no email. Which of PostgreSQL and SQL Server accepts a second user with a NULL email, and what would you change on the other one to allow it?
Show answer
PostgreSQL accepts it, because it treats NULLs as distinct in a unique constraint. SQL Server rejects it, because it treats NULLs as equal. In SQL Server, replace the constraint with a filtered unique index: CREATE UNIQUE INDEX ux_users_email ON users (email) WHERE email IS NOT NULL;.
Exercise
On Oracle, INSERT INTO t (name) VALUES ('') fails for a NOT NULL column name, but the same statement succeeds on PostgreSQL. Why?
Show answer
Oracle treats a zero-length string as NULL, so the insert supplies NULL and violates the NOT NULL constraint. PostgreSQL treats '' as an ordinary non-NULL value, so the insert succeeds.