SQL must know - combined query

"SQL must know must know" reading notes

This lesson describes how to use the UNION operator to combine multiple SELECT statements into a single result set.

1. Combined query

Most SQL queries contain only a single SELECT statement that returns data from one or more tables.

However, SQL also allows executing multiple queries (multiple SELECT statements) and returning the results as a query result set.

These combined queries are often called union or compound queries.

There are two main situations where a combined query needs to be used:

  • Return structured data from different tables in one query;
  • Execute multiple queries against a table, returning data by one query.

Tip: Combining Queries and Multiple WHERE Conditions

For the most part, two queries combining the same table will do the same work as one query with multiple WHERE clause conditions. In other words, any SELECT statement with multiple WHERE clauses can be used as a combined query, as you can see below.

2. Use UNION

For example, if you need reports for all customers in several US states such as Illinois, Indiana, and Michigan, you also want to include all Fun4Alls regardless of state.

We can do this by writing multiple SELECT statements. It can also be done through UNION.

SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state IN ('IL','IN','MI');

cust_name       cust_contact        cust_email
Village Toys
Fun4All
The Toy Store

SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_name = 'Fun4All';

cust_name       cust_contact        cust_email
Fun4All
Fun4All

The first SELECT passes the state abbreviations of Illinois, Indiana, Michigan, etc. to the IN clause and retrieves all rows for those states. The second SELECT finds all Fun4All using a simple equality test.

Combining these two statements can be done as follows:

SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state IN('IL','IN','MI')
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_name = 'Fun4All';

cust_name       cust_contact        cust_email
Fun4All
Fun4All
Village Toys
The Toy Store

This statement consists of the previous two SELECT statements, separated by the UNION keyword. UNION instructs the DBMS to execute the two-day SELECT statement and combine the output into a query result set.

For ease of reference, here is the same query (combined query and multiple WHERE conditions) using multiple WHERE clauses instead of UNION:

SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state IN ('IL','IN','MI')
    OR cust_name = 'Fun4All';

In this simple example, using a UNION might be more complicated than using a WHERE clause. But for more complex filtering conditions, or retrieving data from multiple tables (rather than one), using UNION may make processing easier.

Tip: Limitations of UNION

There is no standard limit on the number of SELECT statements that can be combined using UNION. However, it is best to consult the specific DBMS documentation to see if it has a limit on the maximum number of statements that UNION can combine.

Note: performance issues

Most good DBMSs use an internal query optimizer that combines multiple SELECT statements before processing them.

3. UNION rules

  • UNION must consist of two or more SELECT statements separated by the keyword UNION (so, if four SELECT statements are combined, three UNION keywords will be used).
  • Each query in a UNION must contain the same columns, expressions, or aggregate functions (however, the columns need not be listed in the same order).
  • The column data types must be compatible: the types do not have to be exactly the same, but must be of a type that the DBMS can implicitly convert (for example, a different numeric type or a different date type).

4. Include or Unduplicate Lines

UNION automatically removes duplicate rows from the query result set. This is the default behavior of UNION, but you can change it if you like. In fact, if you want to return all matching rows, use UNION ALL instead of UNION.

Hint: UNION vs WHERE

UNION almost always does the same job as multiple WHERE conditions. UNION ALL is a form of UNION that does what the WHERE clause cannot. If you really need all matching rows for each condition (including duplicate rows), you must use UNION ALL instead of WHERE.

5. Sort the combined query results

The output of the SELECT statement is ordered by the ORDER BY clause. When combining queries with UNION, only one ORDER BY clause can be used, and it must come after the last SELECT statement. For a result set, there is no case where one part is ordered one way and another part is ordered another way, so multiple ORDER BY clauses are not allowed.

SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_state IN('IL','IN','MI')
UNION
SELECT cust_name, cust_contact, cust_email
FROM Customers
WHERE cust_name = 'Fun4All';
ORDER BY cust_name, cust_contact;

cust_name       cust_contact        cust_email
Fun4All
Fun4All
Village Toys
The Toy Store

This UNION uses an ORDER BY clause after the last SELECT statement. Although the ORDER BY clause appears to be just the composition of the last SELECT statement, in fact the DBMS will use it to order all results returned by all SELECT statements.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324481111&siteId=291194637