SQL Joins - Building Your Data Models with Lego Bricks
Fathima Zajel
Senior Officer Business Intelligence | ?? Data Science Expert | ?? Author: AI Data Pill | ?? Power BI/Python/SQL/ML Enthusiast | ?? 100K+ LinkedIn Fam Goal | ?? Certified IELTS Instructor (Band 8) | IIM-I??
SQL Joins are like the building blocks of SQL, allowing you to combine data from two or more tables. Just like building with Lego bricks, choosing the right type of SQL join is crucial to creating a solid and effective data model. In this newsletter, I will use the analogy of Lego bricks to help explain the different types of SQL Joins and their respective uses, advantages, and disadvantages.
Imagine you have two boxes of Legos, each representing a table in your database. The goal is to combine the pieces from both boxes to build a bigger and more complete structure. This is where SQL joins come into play.
1.INNER JOIN:
Think of an inner join as a game of legos. You have two sets of lego pieces, and you only use the pieces that fit together to build a complete structure. In an inner join, only the rows with matching values in both tables are returned. Just like how only lego pieces that fit together are used to complete the structure.
Advantages:
Disadvantages:
2. LEFT JOIN:
Now imagine one of the boxes has more pieces than the other. With a left join, we use all the pieces from the left box (the first table in your SQL query) and match them with the pieces in the right box. If there are no matching pieces, the result will still show the pieces from the left box with NULL values for the missing pieces from the right box.
Think of it as building with blocks where you have a set of blocks with unique shapes, and another set of blocks with common shapes. In a left join, you use all the unique shaped blocks and try to fit the common shaped blocks. The blocks that don't fit are left as they are, giving you a partially built structure.
Advantages:
Disadvantages:
3. RIGHT JOIN:
A right join works similarly to a left join, except that all the pieces from the right box (the second table in your SQL query) are used and matched with the pieces from the left box.
Think of it as building with blocks where you have a set of blocks with common shapes, and another set of blocks with unique shapes. In a right join, you use all the unique shaped blocks and try to fit the common shaped blocks. The blocks that don't fit are left as they are, giving you a partially built structure.
Advantages:
Disadvantages:
4. FULL OUTER JOIN:
This is like combining two Lego sets and using all the pieces from both boxes, regardless of whether they fit together or not. The result will show all the pieces from both boxes, and where there are no matching pieces, the result will show NULL values.
Think of it as building with blocks where you have two sets of blocks with unique shapes, and you want to use all the blocks to build a complete structure. The blocks that don't fit are left as they are, giving you a partially built structure.
Advantages:
Disadvantages:
5. CROSS JOIN:
Imagine you have a box of red Legos and a box of blue Legos, and you want to see every possible combination of a red Lego and a blue Lego. This is what a cross join does - it returns every possible combination of rows from both tables, just like how a cross join would return every possible combination of a red Lego and a blue Lego.
领英推荐
Advantages:
Disadvantages:
6. ANTI LEFT JOIN:
An anti left join is similar to a left join, except that it returns only the rows from the left table that do not have matching values in the right table.
Think of it as building with blocks where you have a set of blocks with unique shapes and another set of blocks with common shapes. In an anti left join, you use only the blocks from the first set that don't have matching shapes in the second set, giving you a partially built structure.
Advantages:
Disadvantages:
7. ANTI RIGHT JOIN:
An anti right join works similarly to an anti left join, except that it returns only the rows from the right table that do not have matching values in the left table.
Think of it as building with blocks where you have a set of blocks with common shapes and another set of blocks with unique shapes. In an anti right join, you use only the blocks from the second set that don't have matching shapes in the first set, giving you a partially built structure.
Advantages:
Disadvantages:
8. ANTI OUTER JOIN:
An anti outer join is a combination of both anti left join and anti right join, where it returns only the rows that do not have matching values in both tables.
Think of it as building with blocks where you have two sets of blocks with unique shapes. In an anti outer join, you use only the blocks from both sets that don't have matching shapes, giving you a partially built structure.
Advantages:
Disadvantages:
In conclusion, each type of SQL join has its own advantages and disadvantages, and the best one for you will depend on your specific needs. When deciding on the best type of join for your data, it's important to consider factors such as the size of your datasets, the type of data you're working with, and the level of detail you need to see. With a little experimentation, you'll be able to determine which type of join works best for you and your specific situation.
As you can see, each type of join is like a different type of Lego block, and each one has its own unique properties and uses. Whether you're working with a small dataset or a large one, or whether you need to see all the data or just some of it, there's a SQL join that can help you get the results you're looking for. So grab your virtual Lego blocks and start experimenting today!
Hope you enjoyed today's 5min read..Also go check out on the article for all the popular python libraries to equip yourself better for upcoming interviews!
Stay tuned for more !
Analytics | Risk and Policy | Building EvolveWise | Data-Driven Business Operations Consultant
2 年It was really interesting and insightful reading your articles! ??