Combining results from multiple queries

Question

You, for some reason, want a combined list of all surnames and all facility names. Yes, this is a contrived example :-). Produce that list!

Expected Results

surname
Bader
Badminton Court
Baker
Boothe
Butters
Coplin
Crumpet
Dare
Farrell
GUEST
Genting
Hunt
Jones
Joplette
Mackenzie
Massage Room 1
Massage Room 2
Owen
Pinker
Pool Table
Purview
Rownam
Rumney
Sarwin
Smith
Snooker Table
Squash Court
Stibbons
Table Tennis
Tennis Court 1
Tennis Court 2
Tracy
Tupperware
Worthington-Smyth

Your Answer

Your Results

Loading database...

Answers and Discussion

select surname 
	from cd.members
union
select name
	from cd.facilities;

The UNION operator does what you might expect: combines the results of two SQL queries into a single table. The caveat is that both results from the two queries must have the same number of columns and compatible data types.

UNION removes duplicate rows, while UNION ALL does not. Use UNION ALL by default, unless you care about duplicate results.