From the course: Querying Microsoft SQL Server 2019

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Combine results with UNION

Combine results with UNION - SQL Server Tutorial

From the course: Querying Microsoft SQL Server 2019

Start my 1-month free trial

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