Sub topic 2
Making Two Queries Compatible
What compatibility means
Assume you can write basic SELECT queries with WHERE, DISTINCT, and ORDER BY, and that you understand tables, rows, columns, and NULL.
A set operator combines the result of one query with the result of another query. Before using UNION, INTERSECT, or EXCEPT, make the two queries compatible. Compatible queries have:
- The same number of selected columns.
- Corresponding columns in the same logical order.
- Data types that the database can combine or convert safely.
The queries do not need to read from the same table. They only need to produce compatible result shapes.
SELECT customer_id, email
FROM customers
UNION
SELECT subscriber_id, email
FROM newsletter_subscribers;Both queries return two columns. The first column contains identifiers, and the second contains email addresses. The column names in the combined result usually come from the first SELECT, so the result columns are named customer_id and email, even though the second query uses subscriber_id.
Matching the number and order of columns
This query is invalid because the two result sets have different numbers of columns:
SELECT customer_id, email
FROM customers
UNION
SELECT subscriber_id
FROM newsletter_subscribers;The database cannot place a two-column row and a one-column row into the same result.
Column meaning also matters. SQL may accept compatible types in the same position even when the meanings are unrelated:
SELECT customer_id, email
FROM customers
UNION
SELECT subscriber_id, phone_number
FROM newsletter_subscribers;This may run, but it combines email addresses and phone numbers in one output column. Arrange columns according to their meaning, not only according to whether the database accepts their types.
Use aliases in the first query when the combined result needs clear names:
SELECT customer_id AS person_id,
email AS contact_value
FROM customers
UNION
SELECT subscriber_id,
email
FROM newsletter_subscribers;Adding a missing value
Sometimes one source has a value that the other source lacks. Add a typed NULL or a literal so both queries return the same shape.
SELECT customer_id AS person_id,
email,
'customer' AS source
FROM customers
UNION ALL
SELECT subscriber_id,
email,
'subscriber' AS source
FROM newsletter_subscribers;If the second source has no email column, use a NULL with an explicit type:
SELECT customer_id AS person_id,
email
FROM customers
UNION ALL
SELECT subscriber_id,
CAST(NULL AS VARCHAR(255)) AS email
FROM phone_contacts;The exact cast syntax and type names vary by database. PostgreSQL also supports NULL::varchar(255), while standard CAST syntax is more portable.
Converting incompatible types
Corresponding columns should represent the same kind of value. If one query returns an integer and another returns text, explicitly convert them instead of relying on implicit conversion.
SELECT CAST(order_id AS VARCHAR(30)) AS reference
FROM orders
UNION
SELECT ticket_code AS reference
FROM support_tickets;The conversion makes both result columns text. It also documents the intended result type and avoids errors caused by different type-precedence rules.
A compatibility checklist
Before combining queries, check each position:
| Check | Question |
|---|---|
| Count | Do both queries return the same number of columns? |
| Order | Do corresponding positions have the same meaning? |
| Type | Are the data types compatible or explicitly cast? |
| Names | Are the output names clear in the first query? |
| Missing values | Should a source use `NULL` or a literal placeholder? |
The following diagram shows how two query shapes become one compatible shape:
Exercise
The first query returns id and created_at. The second returns ticket_id, title, and created_at. What must change before using a set operator?
Show answer
Both queries must return the same number of columns in the same logical order. Remove or add a column, and cast corresponding types if necessary.