Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for sql joins: moving in together
Ashley D
Ashley DSubscriber

Posted on

sql joins: moving in together

In our coding bootcamp, as we breezed past SQL week, one of the topics that was harder to grasp was that of joins. When I looked at the fifth Venn diagram representation on a Google Image search in an attempt to better to visualize the concept then, it was then that I pictured a couple moving in and how that ties into SQL joins. 💖🏠💕

When a couple decides to move in together, the things they bring into their shared space represent how data from two tables (the guy and girl) merge through different types of SQL joins. The "love" they have for certain items (data) determines what ends up in the shared house.

Let's start off with a visual of these two tables between John and Emily (a hypothetical couple), and what they each love.

Main Tables: The Guy and the Girl

Guy Table (John's Interests)

GuyGuyLove
JohnMovies
JohnMusic
JohnSports

Girl Table (Emily's Interests)

GirlGirlLove
EmilyMovies
EmilyMusic
EmilyBooks

Table of Contents

  1. Inner Join: The Love Match
  2. Left Join: The Bossy Guy
  3. Right Join: The Bossy Girl
  4. Full Join: The Cantankerous Couple

Inner Join: The Love Match

When a couple shares mutual love for the same things, only those items make it into the house. If either doesn't love something, it stays out.

Analogy: They both only move in items they both love.

*SQL Code: *

SELECT * FROM guy INNER JOIN girl ON guy.GuyLove = girl.GirlLove;
Enter fullscreen modeExit fullscreen mode

Result Table:

GuyGuyLoveGirlGirlLove
JohnMoviesEmilyMovies
JohnMusicEmilyMusic

Explanation:
An inner join finds the "love match" between the guy and girl, meaning it only brings in records where there’s a common interest between both. In our case, John and Emily both love movies and music, so only those shared loves end up in the house.


Left Join: The Bossy Guy

In this case, the guy is bossy. He brings all of his stuff into the house, even if the girl doesn’t love it.

Analogy: The guy brings all his stuff when they move in together, regardless of whether the girl loves it or not.

*SQL Code: *

SELECT * FROM guy LEFT JOIN girl ON guy.GuyLove = girl.GirlLove;
Enter fullscreen modeExit fullscreen mode

Result Table:

GuyGuyLoveGirlGirlLove
JohnMoviesEmilyMovies
JohnMusicEmilyMusic
JohnSportsNULLNULL

Explanation:
A left join returns all records from the guy’s table (left table aka first table specified inFROM), regardless of whether there’s a match in the girl’s table (right table). So, even though John’s love for sports doesn’t match any of Emily’s interests, it still shows up in the final result.

While John has an interest in "Sports," Emily does not share that interest, so there’s no matching value- hence we see aNULL for her in that record.


Right Join: The Bossy Girl

Here, the roles are reversed. The girl is the bossy one, and she brings all of her stuff into the house regardless of the guy's feelings.

Analogy: The girl brings all her stuff into the house, whether or not the guy loves it.

*SQL Code: *

SELECT * FROM guy RIGHT JOIN girl ON guy.GuyLove = girl.GirlLove;
Enter fullscreen modeExit fullscreen mode

Result Table:

GuyGuyLoveGirlGirlLove
JohnMoviesEmilyMovies
JohnMusicEmilyMusic
NULLNULLEmilyBooks

Explanation:
A right join prioritizes the girl’s table, meaning all of her interests get included, even if they don’t match with the guy’s interests. In this case, John and Emily share a love for movies and music so that record shows up in both.

However, since John does not love shopping, it appears as a new record withNULL values for John's columns, indicating he has no interest in it.


Full Join: The Cantankerous Couple

In this scenario, both the guy and girl are very stubborn and bossy. They each bring everything they love into the house, whether or not the other person loves it. No compromise here!

Analogy: They both bring all their stuff into the house, regardless of whether the other loves it or not.

*SQL Code: *

SELECT * FROM guy FULL OUTER JOIN girl ON guy.GuyLove = girl.GirlLove;
Enter fullscreen modeExit fullscreen mode

Result Table:

GuyGuyLoveGirlGirlLove
JohnMoviesEmilyMovies
JohnMusicEmilyMusic
JohnSportsNULLNULL
NULLNULLEmilyBooks

Explanation:
In this full join example, we see that John brings in his love for sports, while Emily brings her love for books. TheNULL values in theGirl andGirlLove columns indicate that there were no corresponding entries in John’s interests for these loves, and vice versa.

This full join captures all the items from both tables, showing how both John and Emily fill the house with their loves, whether shared or not.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

10+ year client support roles, and currently in technical apprentice program (Bootcamp started Feb 2024)
  • Joined

More fromAshley D

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp