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