Single Queries Versus Compound Queries

The single query is one SELECT statement, while the compound query includes two or more SELECT statements.

Compound queries are formed by using some type of operator that is used to join the two queries. The UNION operator in the following examples is used to join two queries.

A single SQL statement could be written as follows:

SELECT EMP_ID, SALARY, PAY_RATE
FROM EMPLOYEE_PAY_TBL
WHERE SALARY IS NOT NULL OR
PAY_RATE IS NOT NULL;

This is the same statement using the UNION operator:

SELECT EMP_ID, SALARY
FROM EMPLOYEE_PAY_TBL
WHERE SALARY IS NOT NULL
UNION
SELECT EMP_ID, PAY_RATE
FROM EMPLOYEE_PAY_TBL
WHERE PAY_RATE IS NOT NULL;

The previous statements return pay information for all employees who are ...

Get Sams Teach Yourself SQL in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.