The UNION Operator

UNION is an operator used to combine two or more SELECT statements and generate one result set. These SELECT statements must meet some conditions, such as the following:

  • They must have the same number of columns. You can work around this restriction if you use constants in the SELECT statement with fewer columns, as shown in Listing 5.21.

Code Listing 5.21. Using a UNION Operator with Constants in the SELECT Statements
					 -- A constant has to be used in the second SELECT statement -- because Shippers doesn't have a contactname column USE Northwind SELECT companyname, contactname FROM Suppliers WHERE country = 'USA' UNION SELECT ...

Get Microsoft® SQL Server™ 2000 Programming by Example 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.