Sub topic 13
Differences Between Database Systems
Assume you can write basic SELECT queries, use WHERE, DISTINCT, and ORDER BY, and understand how NULL behaves. Set operators are part of SQL, but database systems do not implement every operator in the same way.
Standard operators and common support
The main operators are:
UNION: combines results and removes duplicate rows.UNION ALL: combines results and keeps duplicates.INTERSECT: returns rows found in both results.EXCEPT: returns rows found in the first result but not the second.INTERSECT ALLandEXCEPT ALL: preserve duplicate counts, but support varies.
The two queries still need compatible column counts and data types. A database may also apply its own rules for implicit type conversion.
| Database system | Difference to check |
|---|---|
| PostgreSQL | Supports `UNION`, `INTERSECT`, `EXCEPT`, and their `ALL` forms |
| SQL Server | Supports `UNION`, `INTERSECT`, and `EXCEPT`, but not the `ALL` forms of the latter two |
| Oracle | Uses `MINUS` instead of `EXCEPT`; support for `ALL` forms depends on the Oracle version |
| SQLite | Supports the three distinct operators and `UNION ALL`, but not `INTERSECT ALL` or `EXCEPT ALL` |
| MySQL | Support for `INTERSECT` and `EXCEPT` depends on the MySQL version; check the target version |
Oracle's `MINUS`
Oracle uses MINUS for the operation that most systems call EXCEPT:
SELECT email FROM newsletter_subscribers
MINUS
SELECT email FROM unsubscribed;This returns distinct emails in newsletter_subscribers that do not appear in unsubscribed.
If you move this query to PostgreSQL or SQL Server, write:
SELECT email FROM newsletter_subscribers
EXCEPT
SELECT email FROM unsubscribed;The order remains important. Swapping the two queries changes the meaning.
The `ALL` variants
The ordinary forms remove duplicate rows. The ALL forms preserve duplicate occurrences where the database supports them.
For example, PostgreSQL can run:
SELECT email FROM newsletter_subscribers
EXCEPT ALL
SELECT email FROM unsubscribed;If an email occurs three times in the first result and once in the second, it can remain twice in the result. This is different from ordinary EXCEPT, which returns one copy at most.
For portability, avoid INTERSECT ALL and EXCEPT ALL unless every target database supports them. You can sometimes reproduce their behavior with joins, grouping, and row numbers, but that rewrite is more complicated and may have different NULL behavior.
Syntax and precedence differences
Use parentheses when combining several operators. This makes your intended order clear and protects the query from dialect-specific precedence rules.
(SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers)
EXCEPT
SELECT email FROM unsubscribed;Some systems evaluate INTERSECT before UNION or EXCEPT. Other systems may reject a form that another database accepts. Parentheses also make code reviews easier.
Place one final ORDER BY after the complete set expression:
SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers
ORDER BY email;Do not assume that an ORDER BY inside one component controls the final result. Row ordering is guaranteed only by the final ordering clause.
When portability matters, prefer UNION and UNION ALL, use parentheses, and test the result with duplicate and NULL values. For EXCEPT or MINUS, document which database syntax the application expects.
Exercise
You need to migrate an Oracle query using MINUS to SQL Server. What change is required?
Show answer
Replace MINUS with EXCEPT, keep the two SELECT statements in the same order, and verify that their columns have compatible types.