Unions

The UNION operation lets you combine the results of two or more different SQL statements into one answer set. You can combine many different tables or SQL statements using the UNION (or UNION ALL) operator; the only restriction is that every table or SQL statement must have the same type, number, and order of columns. The term used to describe this is that they must be UNION-compatible.

Suppose you wanted to combine the minimum and maximum score for each DB2 Certification program exam on different output rows, and add a string constant that indicates which values are the maximum and minimum.

 SELECT number,'Minimum:', MIN(score) FROM test_taken GROUP BY number UNION SELECT number,'Maximum:', MAX(score) FROM test_taken GROUP BY number ORDER ...

Get DB2® Universal Database™ for OS/390™ v7.1 Application Certification Guide 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.