Using union and count(*) together in an SQL query
To aggregate data from different datasets or apply different conditions, use UNION ALL
with COUNT(*)
in subqueries, and sum the results in a top-level query. A sample SQL script is as follows:
Note: UNION ALL
is used to include all output records, even duplications. Using UNION
only can result in decreased aggregates due to its implicit deduplication.
Mastering the basics
Your fast-track guide to understanding and using UNION
and UNION ALL
. While UNION
returns the unique records, UNION ALL
preserves all the records, including duplicates. It's really a point to understand when you are after maintaining the entire count from multiple tables or queries.
Across multiple tables
Joining counts from several tables poses a common challenge. You can use the following SQL statement, which properly orders and keeps the GROUP BY
function intact:
Need specific counts?
You may need to tally distinct values across several unions. Dive in with nested subqueries to solve this issue:
Common pitfalls and remedies
Compatibility across databases is essential. Did you know Oracle Database doesn't allow AS
for table or column aliasing with UNION
statements? So, be friendly and welcome all databases with open arms by omitting AS
with UNION
:
Hint: If you're wrangling with multiple columns and functions, always ensure consistency in data types and the count of columns among all UNION
parts.
Optimization essentials
With larger data sets, indexing important columns is ideal to minimize query execution time. Keep your transactions low by considering batch processing for large tables—less locking and fewer overflows.
Simplifying complex scenes
Sometimes combining counts and performing additional computations become essential. This is where derived tables and CASE statements can be powerful:
This brings complicated scenarios under control with elegance and humor.
Was this article helpful?