Single Queries Versus Compound Queries

Team-Fly    

 
Sams Teach Yourself SQL in 24 Hours, Third Edition
By Ronald R. Plew, Ryan K. Stephens
Table of Contents
Hour  15.  Combining Multiple Queries into One


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:

 graphics/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.

graphics/note_icon.gif

If you executed the second query, the output has two column headings: EMP_ID and SALARY. Each individual's pay rate is listed under the SALARY column. When using the UNION operator, column headings are determined by column names or column aliases used in the first SELECT of the UNION.



Team-Fly    
Top
 


Sams Teach Yourself SQL in 24 Hours
Sams Teach Yourself SQL in 24 Hours (5th Edition) (Sams Teach Yourself -- Hours)
ISBN: 0672335417
EAN: 2147483647
Year: 2002
Pages: 275

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net