Sub topic 15
Where Database Systems Differ
This chapter assumes you know basic SELECT, WHERE, ORDER BY, JOIN, and GROUP BY queries. You should also understand that NULL means an absent or unknown value, not zero or an empty string.
SQL defines many rules for NULL, but database systems add their own behavior. A query can therefore be logically correct yet fail to run, or produce different results after moving from one database to another.
Syntax for NULL-Safe Comparisons
IS NULL and IS NOT NULL are widely portable:
SELECT name
FROM employees
WHERE commission IS NULL;The larger difference is the syntax for comparing two values while treating two NULLs as equal.
| Database system | NULL-safe equality or inequality |
|---|---|
| PostgreSQL | `IS DISTINCT FROM` and `IS NOT DISTINCT FROM` |
| MySQL | `<=>` for NULL-safe equality |
| SQLite | `IS` and `IS NOT`, also `IS DISTINCT FROM` in recent versions |
| SQL Server | No single equivalent operator; write the logic explicitly |
| Oracle | Supports `IS [NOT] DISTINCT FROM` in recent versions |
For portable code, spell out the intended logic or isolate database-specific SQL in your application. Do not replace IS NULL with = NULL.
-- Portable NULL-safe equality
WHERE (a = b) OR (a IS NULL AND b IS NULL)Some systems also differ in how they implement Boolean values. PostgreSQL has a Boolean type. MySQL's BOOLEAN is an alias for an integer type. SQL Server commonly uses BIT, while SQLite stores values dynamically. A nullable Boolean column can therefore need different literals and casts.
Empty Strings and Concatenation
Oracle treats an empty string ('') as NULL in character columns. PostgreSQL, MySQL, SQL Server, and SQLite distinguish an empty string from NULL.
This difference affects both filters and constraints:
-- Not equivalent on Oracle and most other systems
WHERE comment = ''
WHERE comment IS NULLString concatenation also varies. PostgreSQL, Oracle, and SQLite commonly use ||. SQL Server uses +, and MySQL commonly uses CONCAT().
CONCAT() often converts NULL arguments to empty strings, while the || operator generally propagates NULL. Test this behavior before moving expression-based queries between systems.
Ordering and Constraints
The default position of NULL during sorting differs. PostgreSQL and Oracle support NULLS FIRST and NULLS LAST. SQLite supports these modifiers in modern versions. MySQL and SQL Server usually require an expression when you need explicit placement.
-- Portable pattern: put missing commissions last
SELECT name, commission
FROM employees
ORDER BY CASE WHEN commission IS NULL THEN 1 ELSE 0 END,
commission;Unique constraints also vary. Most systems allow multiple NULL values because NULL values are not considered equal for uniqueness. SQL Server traditionally permits only one NULL in a unique constraint. A filtered or partial unique index may provide the desired rule, but its syntax is database-specific.
CHECK constraints usually reject a row only when their condition is FALSE. A condition that evaluates to UNKNOWN because of NULL can pass. Add NOT NULL or an explicit IS NOT NULL test when missing values must be forbidden.
Migration Checklist
Before moving a schema or query, test these cases on both systems:
NULLcompared withNULLand with an ordinary value.- Empty strings in text columns.
- Concatenation involving
NULL. - Ascending and descending sort order.
- Multiple
NULLvalues in unique columns. CHECKconditions involving nullable columns.- Boolean literals and nullable Boolean expressions.
NOT INsubqueries that might returnNULL.
A compatibility layer can hide syntax differences, but it cannot decide whether a missing value means unknown, not applicable, or intentionally empty. That meaning belongs in the schema and query design.
Exercise
Your application must sort nullable commission values last on PostgreSQL, MySQL, and SQL Server. Which approach is safest?
Show answer
Use a portable CASE expression in ORDER BY, such as ORDER BY CASE WHEN commission IS NULL THEN 1 ELSE 0 END, commission, instead of relying on NULLS LAST.