Sub topic 9
Precedence and Parentheses with Several SQL Set Operators
This chapter assumes you can write SELECT queries with WHERE, understand NULL, and use DISTINCT and ORDER BY. It also assumes you know the basic meanings of UNION, INTERSECT, and EXCEPT.
Set operators combine complete query results. When one query uses several operators, the database must decide which operation happens first. Parentheses let you control that order.
The default evaluation order
A useful working rule is:
INTERSECTusually has higher precedence thanUNIONandEXCEPT.UNIONandEXCEPTare usually evaluated from left to right when they have equal precedence.- Parentheses override the default order.
For example, this expression:
SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers
INTERSECT
SELECT email FROM verified_emails;is commonly interpreted as:
customers UNION (newsletter_subscribers INTERSECT verified_emails)The database first finds newsletter subscribers who are verified. It then adds those results to the customer emails.
The result can differ if you add parentheses around the first two queries:
(SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers)
INTERSECT
SELECT email FROM verified_emails;This version first combines customers and newsletter subscribers. It then keeps only emails that are also verified.
Why parentheses matter
Relying on precedence makes a query harder to read and easier to misunderstand. It also creates portability problems because database systems can differ in their supported syntax and precedence rules.
Use parentheses when:
- More than two set operators appear.
- The intended grouping is important to the result.
- You are mixing
INTERSECT,UNION, andEXCEPT. - Another developer must maintain the query later.
The following table shows two different meanings:
| Expression | First operation | General meaning |
|---|---|---|
| `A UNION (B INTERSECT C)` | `B INTERSECT C` | Add customers to the verified subscribers |
| `(A UNION B) INTERSECT C` | `A UNION B` | Keep only combined results that are verified |
EXCEPT is especially sensitive to grouping because it is directional. A EXCEPT B means rows in A that are absent from B. For example:
(SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers)
EXCEPT
SELECT email FROM unsubscribed;This removes unsubscribed emails after the two audiences have been combined.
Without the parentheses, a reader might incorrectly assume that EXCEPT applies only to newsletter_subscribers.
Ordering the final result
Place one ORDER BY after the complete set expression when you want to sort the combined rows:
(SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers)
EXCEPT
SELECT email FROM unsubscribed
ORDER BY email;The ORDER BY belongs to the final result, not just to the last SELECT. A query-level LIMIT or equivalent row limit usually belongs at the end as well:
(SELECT email FROM customers
UNION
SELECT email FROM newsletter_subscribers)
EXCEPT
SELECT email FROM unsubscribed
ORDER BY email
FETCH FIRST 100 ROWS ONLY;Some systems require additional parentheses around individual query expressions when a local ORDER BY, LIMIT, or row limit appears inside a set operation. Check the documentation for your database before using local ordering.
Dialect differences
SQL systems do not all implement set operations in the same way.
- PostgreSQL and many other systems give
INTERSECThigher precedence thanUNIONandEXCEPT. - Oracle uses
MINUSfor the operation commonly calledEXCEPT. - Some systems require parentheses around compound query expressions.
- Support for
INTERSECT ALLandEXCEPT ALLvaries. - Column names for the final result commonly come from the first
SELECT, but alias rules can differ.
When portability matters, use explicit parentheses and name the output columns in the first query:
(SELECT email AS address FROM customers
UNION
SELECT email FROM newsletter_subscribers)
EXCEPT
SELECT email FROM unsubscribed
ORDER BY address;Test the complete statement on every database system your application supports. A query that is logically clear can still fail because its dialect does not accept the same compound-query syntax.
Exercise
Which expression first combines customers and newsletter subscribers, then removes unsubscribed emails: A UNION (B EXCEPT C) or (A UNION B) EXCEPT C?
Show answer
(A UNION B) EXCEPT C. The parentheses force the UNION to happen before the EXCEPT.