← Back to all snippets
SQL

Combine Multiple Query Results with UNION ALL

Merge the results of two or more SELECT statements into a single result set using the UNION ALL operator, preserving all duplicate rows.

SELECT customer_id, first_name, last_name, 'Active' AS status
FROM customers
WHERE is_active = TRUE

UNION ALL

SELECT customer_id, first_name, last_name, 'Inactive' AS status
FROM customers
WHERE is_active = FALSE;
How it works: The `UNION ALL` operator combines the result sets of two or more `SELECT` statements into a single result set. Unlike `UNION` (which removes duplicate rows), `UNION ALL` includes all rows from all queries, making it efficient when you don't need distinct results. This is useful for consolidating data from different but structurally similar queries.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs