O'Reilly logo

Oracle SQL: A Beginner’s Tutorial by Djoni Darmawikarta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 7

Compound Queries

You can combine the results of two or more SELECT statements using the UNION ALL, UNION, INTERSECT, or MINUS operators. The number of output columns from every statement must be the same and the corresponding columns must have identical or compatible data types.

This chapter shows you how to combine query results.

UNION ALL

When you combine two or more queries with the UNION ALL operator, the overall output will be the total rows from all the queries. For example, take a look at the query in Listing 7.1. This query consists of two SELECT statements.

Listing 7.1: Using UNION ALL

SELECT p_code, p_name, 'FIRST QUERY' query

FROM product p WHERE p_name LIKE '%Nut%'

UNION ALL

SELECT p.p_code,

  p_name,

  'SECOND_QUERY' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required