By Cristian G. Guasch • Updated: 03/10/24 • 3 min read
If you’re delving into the world of SQL, one concept that’s crucial to grasp is the idea of joins. I’m here to guide you through this important aspect of SQL, explaining not only what they are but how to use them effectively with visualization using Venn diagrams.
Plus SQL Cheat Sheets and more bonuses, all for FREE!
Joins in SQL allow us to combine rows from two or more tables based on a related column between them. This means we can pull together data from different sources, painting a fuller picture with our results.
Imagine we have two tables: A
and B
.
Table A Table B
id name id name
-- ---- -- ----
1 Hacker 1 Peaches
2 Dog 2 Hacker
3 Coder 3 MacGyver
4 Pizza 4 Coder
Now, I will help you join these tables by the name field in different ways. I hope this will help you conceptualize the matches with Venn diagrams.
There are several types of joins in SQL:
- INNER JOIN: This returns records that have matching values in both tables.
- LEFT (OUTER) JOIN: It gives all records from the left table and matched records from the right one.
- RIGHT (OUTER) JOIN: The opposite of Left Join; it provides every record from the right table and matched ones from the left side.
- FULL (OUTER) JOIN: It’ll offer up all records when there is a match in either left or right table.
INNER JOIN
SQL example:
SELECT * FROM TableA A
INNER JOIN TableB B ON
A.key = B.key
Table results:
id name id name
-- ---- -- ----
1 Hacker 2 Hacker
3 Coder 4 Coder
FULL OUTER JOIN
SQL example:
SELECT * FROM TableA A
FULL OUTER JOIN TableB B ON
A.key = B.key
Table results:
id name id name
-- ---- -- ----
1 Hacker 2 Hacker
2 Dog null null
3 Coder 4 Coder
4 Pizza null null
null null 1 Peaches
null null 3 MacGyver
SQL example:
SELECT * FROM TableA A
FULL OUTER JOIN TableB B ON
A.key = B.key WHERE A.key IS NULL
OR B.key IS NULL
Table results:
id name id name
-- ---- -- ----
2 Dog null null
4 Pizza null null
null null 1 Peaches
null null 3 MacGyver
Plus SQL Cheat Sheets and more bonuses, all for FREE!
LEFT JOIN
SQL example:
SELECT * FROM TableA A
LEFT JOIN TableB B ON
A.key = B.key
Table results:
id name id name
-- ---- -- ----
1 Hacker 2 Hacker
2 Dog null null
3 Coder 4 Coder
4 Pizza null null
SQL example:
SELECT * FROM TableA A
LEFT JOIN TableB B ON
A.key = B.key WHERE B.key IS NULL
Table results:
id name id name
-- ---- -- ----
2 Dog null null
4 Pizza null null
Plus SQL Cheat Sheets and more bonuses, all for FREE!
Related articles
- SQL Temp Table: How to Create a Temporary Table in SQL with Examples
- How to Use AVG in SQL
- How to Use Dates in SQL
- How to CREATE VIEW in SQL
- How to Use AUTO INCREMENT in SQL
- How to Use the SQL Default Constraints
- How to Use the SQL Check Constraint
- How to Use DENSE_RANK() in SQL
- How to Use PRIMARY KEY in SQL
- How to Use Unique Alter Table in SQL
- How to Use ROW_NUMBER & OVER() in SQL
- How to Use Unique Constraint in SQL
- How to Concatenate Two Columns in SQL?
- How to Include Zero in a COUNT() Aggregate
- What Are DDL, DML, DQL, and DCL in SQL?
- What is an SQL Inline Query?
- What Is the Benefit of Foreign Keys in SQL?
- How to Use Constraints Operator in SQL
- What a Moving Average Is and How to Use it in SQL
- How to Analyze a Time Series in SQL
- How to Use TRUNCATE TABLE in SQL
- TRUNCATE TABLE vs. DELETE vs. DROP TABLE
- How to Number Rows in SQL
- How to Use 2 CTEs in a Single SQL Query
- How to Use Lag and Lead Functions in SQL
- How to Calculate the Length of a Series with SQL
- How to Use Aliases in SQL Queries for Clearer Code
- How to Use the BETWEEN Operator in SQL
- How to Use the IN Operator in SQL
- What are & How to Use Wildcards in SQL
- How to Use TOP in SQL with Examples
- How to Use WHERE in SQL with Examples
- How to Use AND OR Operators Correctly in SQL
- How to Use HAVING Clause in SQL
- How to Use the Alter Command in SQL: Renaming Tables and Columns
- How to Use INSTR in SQL? Find Substrings Easily with Examples
- How to Use the PARTITION BY Clause in SQL with Examples
- How to Use ROUND Function in SQL Explained with Examples
- How to Use CAST Function in SQL?
- Why Use WHERE 1=1 in SQL Queries? Exploring Its Impact on Database Efficiency
- How to Create a Table in SQL? Your Step-by-Step Guide for Beginners
- How to Use GROUP BY in SQL? Master the Art of Query Optimization
- How to Use UPDATE in SQL: A Comprehensive Guide for Beginners
- How to Use Select in SQL: A Beginner’s Guide to Database Queries
- How to Use Select Distinct in SQL: A Simple Guide for Efficient Database Queries
- How to Use Union in SQL: A Simple Guide for Efficient Database Management
- How to Use Self Join in SQL: A Comprehensive Guide for Beginners
- How to Use Full Join in SQL: A Comprehensive Guide for Beginners
- How to Use Right Join in SQL: A Comprehensive Guide for Database Enthusiasts
- How to Use Left Join in SQL: A Guide for Database Query Optimization