Sams Teach Yourself SQL in 24 Hours

Sams Teach Yourself SQL in 24 Hours

By Ron Plew and Ryan Stephens

Single Queries Versus Compound Queries

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

Compound queries are formed by using some type of operator 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:

   mysql_icon.gif
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 paid either hourly or salaried.

Share ThisShare This

Informit Network