From the course: Querying Microsoft SQL Server 2019

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Combine results with UNION

Combine results with UNION

- When you have similar data in two different tables, you can add the rows from one select statement to the rows returned by a second. To do this, you'll use the union operator. The basic workflow might look something like this. I'm going to write two select statements that look at the product category and the product subcategory tables. When I run both of these select statements together, it shows me that we have four main categories in this section, as well as 37 subcategories of products that AdventureWorks sells. If I wanted to see these results in a single result set, I can union these two queries together. To do that you simply add the union keyword between the two queries. I'm going to click here onto line number four and press enter a couple of times to move that down. Then, on line number five, I'll type in the keyword, "union". Because I want this to run as a single union query now, I'm going to…

Contents