Intermediate SQL Tutorial | Unions | Union Operator

Alex The Analyst · Beginner ·📊 Data Analytics & Business Intelligence ·6y ago

Key Takeaways

This video tutorial covers the Union Operator in SQL, including how to use it to combine the results of two or more SELECT statements, as part of an intermediate SQL course that also covers topics like Joins, Case Statements, and Data Types.

Full Transcript

I mean it's going on everybody my name is Alex Freiburg and in today's video we're gonna be looking at unions now in the very last video we walked through joins and if I was appropriate to look at unions next because unions and joins are somewhat similar are 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 in each column and not separate it 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 or 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 have 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 gonna 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 Darryl 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 were 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 sound reader which is an integer it has the same amount of columns so 3 & 3 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 thirty forty-five thousand and forty-five dollars 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

Original Description

Take my Full MySQL Course Here: https://bit.ly/3tqOipr In today's Intermediate SQL lesson we walk through Unions. All Topics Covered: Joins, Unions, Case Statements, Updating/Deleting Data, Partition By, Data Types, Aliasing, Views, Having Clause, GetDate(), Primary vs Foreign Key ____________________________________________ SUBSCRIBE! Do you want to become a Data Analyst? That's what this channel is all about! My goal is to help you learn everything you need in order to start your career or even switch your career into Data Analytics. Be sure to subscribe to not miss out on any content! ____________________________________________ RESOURCES: Coursera Courses: Google Data Analyst Certification: https://coursera.pxf.io/5bBd62 Data Analysis with Python - https://coursera.pxf.io/BXY3Wy IBM Data Analysis Specialization - https://coursera.pxf.io/AoYOdR Tableau Data Visualization - https://coursera.pxf.io/MXYqaN Udemy Courses: Python for Data Analysis and Visualization- https://bit.ly/3hhX4LX Statistics for Data Science - https://bit.ly/37jqDbq SQL for Data Analysts (SSMS) - https://bit.ly/3fkqEij Tableau A-Z - http://bit.ly/385lYvN *Please note I may earn a small commission for any purchase through these links - Thanks for supporting the channel!* ____________________________________________ Websites: GitHub: https://github.com/AlexTheAnalyst ____________________________________________ *All opinions or statements in this video are my own and do not reflect the opinion of the company I work for or have ever worked for*
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Alex The Analyst · Alex The Analyst · 18 of 60

1 Top 3 Data Analyst Skills in 2020
Top 3 Data Analyst Skills in 2020
Alex The Analyst
2 Truth About Big Companies | Told by a Fortune 500 Data Analyst
Truth About Big Companies | Told by a Fortune 500 Data Analyst
Alex The Analyst
3 Data Analyst Salary | 100k with No Experience
Data Analyst Salary | 100k with No Experience
Alex The Analyst
4 Working at a Big Company Vs Small Company | Told by a Fortune 500 Data Analyst
Working at a Big Company Vs Small Company | Told by a Fortune 500 Data Analyst
Alex The Analyst
5 Data Analyst Resume | Reviewing My Resume! | Fortune 500 Data Analyst
Data Analyst Resume | Reviewing My Resume! | Fortune 500 Data Analyst
Alex The Analyst
6 Data Analyst Resume | Complete Guide To Creating A Data Analyst Resume | Tips + Templates + Examples
Data Analyst Resume | Complete Guide To Creating A Data Analyst Resume | Tips + Templates + Examples
Alex The Analyst
7 Switching Careers to Become a Data Analyst | How I Made the Switch
Switching Careers to Become a Data Analyst | How I Made the Switch
Alex The Analyst
8 Working With a Recruiter to Land Your First Job as a Data Analyst | LinkedIn Recruiters
Working With a Recruiter to Land Your First Job as a Data Analyst | LinkedIn Recruiters
Alex The Analyst
9 Data Analyst Salary in 2020
Data Analyst Salary in 2020
Alex The Analyst
10 Data Analyst Resume | Reviewing YOUR Data Analyst Resumes!
Data Analyst Resume | Reviewing YOUR Data Analyst Resumes!
Alex The Analyst
11 Data Analyst Fact Check |  84k Average Starting Salary?? | The Career Force 2020 Data Analyst Salary
Data Analyst Fact Check | 84k Average Starting Salary?? | The Career Force 2020 Data Analyst Salary
Alex The Analyst
12 SQL Basics Tutorial For Beginners | Installing SQL Server Management Studio and Create Tables | 1/4
SQL Basics Tutorial For Beginners | Installing SQL Server Management Studio and Create Tables | 1/4
Alex The Analyst
13 SQL Basics Tutorial For Beginners | Select + From Statements | 2/4
SQL Basics Tutorial For Beginners | Select + From Statements | 2/4
Alex The Analyst
14 SQL Basics Tutorial For Beginners | Where Statement | 3/4
SQL Basics Tutorial For Beginners | Where Statement | 3/4
Alex The Analyst
15 SQL Basics Tutorial For Beginners | Group By + Order By Statements | 4/4
SQL Basics Tutorial For Beginners | Group By + Order By Statements | 4/4
Alex The Analyst
16 Day in the Life of a Data Analyst | Fortune 500 Edition
Day in the Life of a Data Analyst | Fortune 500 Edition
Alex The Analyst
17 Intermediate SQL Tutorial | Inner/Outer Joins | Use Cases
Intermediate SQL Tutorial | Inner/Outer Joins | Use Cases
Alex The Analyst
Intermediate SQL Tutorial | Unions | Union Operator
Intermediate SQL Tutorial | Unions | Union Operator
Alex The Analyst
19 Intermediate SQL Tutorial | Case Statement | Use Cases
Intermediate SQL Tutorial | Case Statement | Use Cases
Alex The Analyst
20 Intermediate SQL Tutorial | Having Clause
Intermediate SQL Tutorial | Having Clause
Alex The Analyst
21 Intermediate SQL Tutorial | Updating/Deleting Data
Intermediate SQL Tutorial | Updating/Deleting Data
Alex The Analyst
22 Day in the Life of a Data Analyst | Fortune 500 Edition (During Quarantine)
Day in the Life of a Data Analyst | Fortune 500 Edition (During Quarantine)
Alex The Analyst
23 Data Analyst Interview Questions | Phone + In-Person Interview Questions
Data Analyst Interview Questions | Phone + In-Person Interview Questions
Alex The Analyst
24 SQL Interview Questions and Answers for Beginners | Data Analyst Interview Questions
SQL Interview Questions and Answers for Beginners | Data Analyst Interview Questions
Alex The Analyst
25 Data Analyst Interview Questions | What To Say vs What NOT To Say
Data Analyst Interview Questions | What To Say vs What NOT To Say
Alex The Analyst
26 Data Analyst Interviews | Salary Negotiation
Data Analyst Interviews | Salary Negotiation
Alex The Analyst
27 Data Analyst Q&A LIVE
Data Analyst Q&A LIVE
Alex The Analyst
28 Intermediate SQL Tutorial | Aliasing
Intermediate SQL Tutorial | Aliasing
Alex The Analyst
29 Data Scientist vs Data Analyst | Which Is Right For You?
Data Scientist vs Data Analyst | Which Is Right For You?
Alex The Analyst
30 Best Online Courses for Data Analysts
Best Online Courses for Data Analysts
Alex The Analyst
31 Best Free Online Courses for Data Analysts
Best Free Online Courses for Data Analysts
Alex The Analyst
32 Data Analyst vs Business Analyst | Which Is Right For You?
Data Analyst vs Business Analyst | Which Is Right For You?
Alex The Analyst
33 Scraping Data Off Twitter Using Python | Twitterscraper + NLP + Data Visualization
Scraping Data Off Twitter Using Python | Twitterscraper + NLP + Data Visualization
Alex The Analyst
34 Data Analyst Question and Answer | Answering Your YouTube Questions
Data Analyst Question and Answer | Answering Your YouTube Questions
Alex The Analyst
35 What Does a Data Analyst Actually Do?
What Does a Data Analyst Actually Do?
Alex The Analyst
36 Data Analyst Bootcamps | Are They Worth It?
Data Analyst Bootcamps | Are They Worth It?
Alex The Analyst
37 Top 5 Reasons Not to Become a Data Analyst
Top 5 Reasons Not to Become a Data Analyst
Alex The Analyst
38 Data Analyst Career Path | How to Become a Data Analyst + What to Do Next
Data Analyst Career Path | How to Become a Data Analyst + What to Do Next
Alex The Analyst
39 Live Data Analyst Q&A #3
Live Data Analyst Q&A #3
Alex The Analyst
40 Top 5 Reasons Not to Lie on Your Resume
Top 5 Reasons Not to Lie on Your Resume
Alex The Analyst
41 The Hiring Process from an Interviewer's Perspective | Alex The Analyst Show | Episode 1
The Hiring Process from an Interviewer's Perspective | Alex The Analyst Show | Episode 1
Alex The Analyst
42 Top 5 Reasons Data Analytics is a Good Career Choice
Top 5 Reasons Data Analytics is a Good Career Choice
Alex The Analyst
43 How I Changed Careers to Become a Data Analyst | Alex The Analyst Show | Episode 2
How I Changed Careers to Become a Data Analyst | Alex The Analyst Show | Episode 2
Alex The Analyst
44 Top 5 Reasons You'll Be a Good Data Analyst
Top 5 Reasons You'll Be a Good Data Analyst
Alex The Analyst
45 Self Taught vs Boot Camp vs Degree | Alex The Analyst Show | Episode 3
Self Taught vs Boot Camp vs Degree | Alex The Analyst Show | Episode 3
Alex The Analyst
46 Covid and the Data Analyst Job Market | Alex The Analyst Show | Episode 4
Covid and the Data Analyst Job Market | Alex The Analyst Show | Episode 4
Alex The Analyst
47 Data Analyst Expectations vs Reality
Data Analyst Expectations vs Reality
Alex The Analyst
48 Imposter Syndrome in Tech | Alex The Analyst Show | Episode 5
Imposter Syndrome in Tech | Alex The Analyst Show | Episode 5
Alex The Analyst
49 Top 10 Coursera Courses for Data Analysts
Top 10 Coursera Courses for Data Analysts
Alex The Analyst
50 Working at a Startup vs Fortune 500 Company | Alex The Analyst Show | Episode 6
Working at a Startup vs Fortune 500 Company | Alex The Analyst Show | Episode 6
Alex The Analyst
51 Data Analyst Certifications | Are They Worth It? | Alex The Analyst Show | Episode 7
Data Analyst Certifications | Are They Worth It? | Alex The Analyst Show | Episode 7
Alex The Analyst
52 Top 10 Udemy Courses for Data Analysts
Top 10 Udemy Courses for Data Analysts
Alex The Analyst
53 Asking My Wife Your Questions About Me | Alex The Analyst Show | Episode 8
Asking My Wife Your Questions About Me | Alex The Analyst Show | Episode 8
Alex The Analyst
54 Data Analyst Q&A LIVE #4
Data Analyst Q&A LIVE #4
Alex The Analyst
55 Data Analyst Skills Path | What Skills You NEED to Know
Data Analyst Skills Path | What Skills You NEED to Know
Alex The Analyst
56 What is Analytics Consulting? With John Ariansen | Alex The Analyst Show | Episode 9
What is Analytics Consulting? With John Ariansen | Alex The Analyst Show | Episode 9
Alex The Analyst
57 Solving LeetCode SQL Interview Questions | Part 1/3
Solving LeetCode SQL Interview Questions | Part 1/3
Alex The Analyst
58 What is No Code Analytics? | Alex The Analyst Show | Episode 10
What is No Code Analytics? | Alex The Analyst Show | Episode 10
Alex The Analyst
59 Top 3 Tips on Using LinkedIn to Land a Job
Top 3 Tips on Using LinkedIn to Land a Job
Alex The Analyst
60 Completely Unrealistic Jobs on LinkedIn | Alex The Analyst Show | Episode 11
Completely Unrealistic Jobs on LinkedIn | Alex The Analyst Show | Episode 11
Alex The Analyst

This video teaches how to use the Union Operator in SQL to combine data from multiple tables, and is part of a larger course covering intermediate SQL topics.

Key Takeaways
  1. Use the Union Operator to combine the results of two or more SELECT statements
  2. Specify the columns to be included in the combined result set
  3. Use the ALL keyword to include duplicate rows in the combined result set
  4. Use the DISTINCT keyword to exclude duplicate rows from the combined result set
  5. Test the Union Operator with sample data
💡 The Union Operator can be used to combine data from multiple tables, but the number and data types of the columns must match

Related AI Lessons

Up next
Spreadsheet Guy Meets the CFO: "Define How Much"
Digital Transformation with Eric Kimberling
Watch →