
You don’t need to specify the column aliases on the second table. Looking at the results you can see which records came from which table. The same column is labelled as ‘Employee’ from the employee table. In this query, I added in a value of ‘Customer’ to display for all records from the Customer table. SELECT ‘Customer’ AS record_type, first_name, last_name

As long as the number and type of columns match, the query will work. How can we do that? We can add a static value to each query to indicate which table it came from, or what type of record it is.

Often we don’t need to know, but sometimes we do. This shows us all customer and employee records.īut looking at this, we can’t tell which record comes from each table. If we wanted to select from both the employee and customer tables, using UNION, our query would look like this: SELECT first_name, last_name Let’s see an example based on the sample data above. It removes any duplicate results and shows you the combination of both.Įxpressed as a Venn diagram, where each circle represents a query result, it looks like this: The UNION keyword or set operator will allow you to combine the results of two queries. Our sample data for this article uses a customer and employee table.Įach of our query examples in this article will use this data. You can’t add ORDER BY inside each SELECT query before the set operator. The data types also need to be compatible, so if you select a number and two character types in the first query, you need to do the same in the second query.Īlso, if you want to order your results, the ORDER BY must go at the end of the last query. So, if you select three columns in the first query, you need to select three columns in the second query. When selecting your columns, the number of columns needs to match between queries, and the data type of each column needs to be compatible. There are a few things to keep in mind though. It uses two (or more) SELECT queries, with a set operator in the middle. Set operators are used like this: SELECT your_select_query Let’s take a look at each of these, using some sample data. There are a few different set operators that can be used, depending on your needs, and which database vendor you’re using.

But instead of joining these two tables, you’ll need to list the results from both tables in a single result, or in different rows. Sometimes when working with SQL, you’ll have a need to query data from two more tables. Let’s get into learning about set operators!Ī set operator in SQL is a keyword that lets you combine the results of two queries into a single query. EXCEPT: Another Way of Finding Missing Results.MINUS: Finding Results Rhat Are Missing.UNION ALL: Combining Results in a Different Way.Click on each of the headings to be taken to that place in this article. Here’s what you’ll learn in this article. In this article, you’ll learn all about set operators such as these, why they are used, and see some examples. Have you seen keywords like UNION or MINUS and want to know what they do and why they are used? Are you wondering what set operators are?
