0% found this document useful (0 votes)
29 views

Union Query: by Neil A. Basabe

This document discusses using UNION queries to combine data from multiple queries into a single report. It provides examples of UNION queries that combine results from different tables and conditions. The key points are: - Individual queries produce separate reports, while UNION queries combine the results into one report. - UNION ALL is used between queries to append the result sets. - The column names and data types must match across the UNIONed queries. - ORDER BY should be placed after the last SELECT statement to sort the combined results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Union Query: by Neil A. Basabe

This document discusses using UNION queries to combine data from multiple queries into a single report. It provides examples of UNION queries that combine results from different tables and conditions. The key points are: - Individual queries produce separate reports, while UNION queries combine the results into one report. - UNION ALL is used between queries to append the result sets. - The column names and data types must match across the UNIONed queries. - ORDER BY should be placed after the last SELECT statement to sort the combined results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

UNION QUERY

By
Neil A. Basabe

1
UNION

2
Multiple Queries – Multiple Reports
--Query 1
SELECT some_columns DATA FROM QUERY 1
FROM some_table(s)
WHERE some_condition(s)

--Query 2
SELECT some_columns
DATA FROM QUERY 2
FROM some_table(s)
WHERE some_condition(s)

Note: Each time a query is executed a single report is


produced.

3
Unioning Queries Together – Single Report
--Query 1
SELECT some_columns DATA FROM QUERY 1
FROM some_table(s)
WHERE some_condition(s)
UNION ALL
--Query 2
SELECT some_columns DATA FROM QUERY 2
FROM some_table(s)
WHERE some_condition(s)
Note: By following the UNION RULES, and by adding a UNION
ALL clause between the queries the data from both queries
appears in a single report.
4
Example 1
SELECT WORKDEPT, LASTNAME CONCAT ','
CONCAT FIRSTNME AS NAME,
'MALE' AS GENDER
FROM EMPLOYEE
WHERE SEX = 'M' AND WORKDEPT IN
('A00','C01')
UNION ALL
SELECT WORKDEPT, LASTNAME CONCAT ',' CONCAT
FIRSTNME AS NAME, 'FEMALE' AS GENDER
FROM EMPLOYEE
WHERE SEX='F' AND WORKDEPT IN
('A00','C01')
ORDER BY WORKDEPT;

5
6
Example 2
SELECT MGRNO, 'DEPT:', DEPTNAME
FROM DEPARTMENT
WHERE MGRNO IS NOT NULL
UNION ALL
SELECT MGRNO,'MGR:',LASTNAME
FROM DEPARTMENT D, EMPLOYEE E
WHERE D.MGRNO = E.EMPNO;

7
8
Example 3
SELECT LASTNAME, EDLEVEL
FROM EMPLOYEE
WHERE JOB='ANALYST'
UNION ALL
SELECT LASTNAME, EDLEVEL
FROM EMPLOYEE
WHERE EDLEVEL = 18;

9
10
Example 4
SELECT EMPNO, SUBSTR(FIRSTNME,1,1) CONCAT
',' CONCAT MIDINIT, LASTNAME, SALARY AS
INCOME, 1 AS SORT
FROM EMPLOYEE
UNION ALL
SELECT EMPNO,'','',BONUS, 2 AS SORT
FROM EMPLOYEE
UNION ALL
SELECT EMPNO, '','',COMM, 3 AS SORT
FROM EMPLOYEE
UNION ALL
SELECT EMPNO,'','SUM:',
SALARY+BONUS+COMM,4 AS SORT
FROM EMPLOYEE
ORDER BY EMPNO, SORT;
11
12
UNION ALL RULES

13
Rule # 1: The number of columns in each
SELECT must the same.
SELECT col-1, col-2, col-3, col-4
FROM table-1
WHERE condition

UNION ALL

SELECT col-a, col-b, col-c, col-d


FROM table-2
WHERE condition
14
Rule # 2: The data type of each column in
each SELECT must be compatible.
SELECT smallint, char, date
FROM table-1
WHERE condition

UNION ALL

SELECT decimal, varchar, date


FROM table-2
WHERE condition
15
Rule # 3: The ORDER BY clause
should be placed in the last SELECT
statement of the UNION query.

16
Rule # 4: The number of UNION
statements in a UNION query
should be equal to the number of
SELECT statements minus one.

17
Laboratory Exercises

18

You might also like