 What's going on everybody? My name is Alex Friedberg and in today's video we're going to be looking at unions. Now in the very last video we walked through joins and I thought it was appropriate to look at unions next because unions and joins are somewhat similar or closely related. And that's because in both instances they're combining two tables to create one output. Now what's the difference? The difference is that a join combines both tables based off a common column. And in last video that was the employee ID. So in both tables we had an employee ID and when you're selecting your data you have to choose either to only select one employee ID or you can choose both employee IDs but they're in separate columns. And with a union you're actually able to select all the data from both tables and put it into one output where all the data is in each column and not separated out and you don't have to choose which table you're choosing it from. Now that may not have made a hundred percent sense but let's look at it real quick in stages. So let's go down here and let's actually join this table together and see what we get. Now the two tables that we're looking at is employee demographics and warehouse employee demographics. So over here we have our employee demographics information and then over here actually down here we have our warehouse employee demographics. Now right now I'm doing a full outer join so we're looking at all the data and if we were to pull this in to an Excel spreadsheet we could just copy this and paste it over here and we would be good to go. And that's because we have all the same columns first name last name age gender first name last name age gender. But if we tried to combine this in a query where we had this information right here it wouldn't work. We cannot get it in the same column and that's where a union comes into play. So let's go back up here and let's actually run both of these. Now as you can see they have the exact same columns and that makes it super easy for what we're about to do. All we're going to do is between these two queries which are completely separate right now all we're going to do is write union. So let's run just this now because of the union you can look down here and the information that used to be in the other table which were in separate columns are now added down below in the exact same order. Now Daryl Philbin was actually in both tables and the reason he isn't showing up multiple times is because this union is actually taking out and removing the duplicates kind of like a distinct statement. Now there's actually another thing called union all and if we do union all it is going to show us all of the information regardless if it is a duplicate or not. So let's run that real quick and they are both there but let's order by and let's do employee ID. So now let's run it and as you can see right here these are exact duplicates and so the union got rid of it because they are the exact same but the union all kept it in because it is showing just the data as is. Now let's get rid of this union all because the only reason why it works so well is because those two tables were the exact same. They were employee ID first name last name age gender so they're basically the same tables just with different information so it made it really easy but we have another table employee salary and let's look at these two tables. So these two tables are obviously very different. They hold different information. Now we would still be able to combine them so let's do employee ID first name and let's do age. Now down here on the employee salary table we will do employee ID job title and salary. Now let's use a union really quick and run this one and it is still going to work. Now why does this work? Well first off the reason it's working is because these data types are the exact same or at least similar so text and text age which is an integer salary which is an integer it has the same amount of columns so three and three so we have employee ID first name and age and it's taking that from the first select statement and it's still using a union to take the data from the second select statement so it's still inserting this information. Now this is not what you want to do because right here we have first name and it's salesman salesman and then our age we have 30, 45,000 and 45,000 is obviously not an age so you want to be careful when you're using a union to combine two separate tables and make sure that the data you're selecting is the same. In the very next video we're going to walk you through case statements. Thank you guys so much for joining me I really appreciate it. If you like this type of content be sure to subscribe below and I'll see you in the next video.