Interview Questions, Answers and Tutorials

Right Joins: Finding All Your Friends (Even the Shy Ones!)

Right Joins: Finding All Your Friends (Even the Shy Ones!)

Hey everyone! Imagine you have two groups of friends. One group loves playing video games, and the other group loves playing outside. Sometimes, some of your friends like doing both!

Now, let’s say you want to make a list of all your friends who love playing outside. Even if they don’t play video games at all, you still want them on your list. That’s kind of what a “Right Join” does in the world of computers and databases!

Think of it like this:

  • You have a list of all your video game friends (let’s call this Table A).
  • You have another list of all your outside-playing friends (let’s call this Table B).

A Right Join says, “Hey, give me everyone from the ‘outside-playing friends’ list (Table B). If they also happen to be on the ‘video game friends’ list (Table A), then show me their video game info too. But even if they aren’t on the video game list, I still want them on my final list!”

It’s all about making sure you don’t miss anyone from the right side!

Let’s See It in Action (with Pretend Tables!)

Imagine these two simple tables on our computer:

Table: video_gamers

Gamer IDNameGame Played
1AliceMinecraft
2BobRoblox
3CharlieFortnite

Table: outside_players

Player IDNameActivity
2BobSoccer
4DavidBasketball
5EveTag
1AliceRiding Bikes

Now, if we do a Right Join of these two tables based on the “Name” column, here’s what we’d get:

SELECT *
FROM video_gamers AS vg
RIGHT JOIN outside_players AS op ON vg.Name = op.Name;

And the result would look like this:

Gamer IDNameGame PlayedPlayer IDNameActivity
1AliceMinecraft1AliceRiding Bikes
2BobRoblox2BobSoccer
NULLNULLNULL4DavidBasketball
NULLNULLNULL5EveTag

See what happened?

  • Alice and Bob are in both tables, so we see their video game and outside activity.
  • David and Eve are only in the outside_players table. So, we see their outside activity, but the computer puts “NULL” (which means “nothing” or “empty”) for their video game information because it doesn’t exist in the video_gamers table.

The RIGHT JOIN made sure we got everyone from the outside_players table!

Another Example: Pets and Their Owners

Let’s say we have two lists: one of pets and one of their owners.

Table: pets

Pet IDNameTypeOwner ID
101BuddyDog1
102WhiskersCat2
103GoldieFish1

Table: owners

Owner IDOwner NameCity
1JohnNew York
2SarahLondon
3EmilyParis

If we want a list of all owners and their pets (if they have any), we’d use a Left Join (which we’ll talk about another time!). But if we wanted a list of all owners, and their pet’s info if they have one, we’d use a Right Join (assuming our “right” table is the owners table):

SELECT *
FROM pets AS p
RIGHT JOIN owners AS o ON p.OwnerID = o.OwnerID;

The result:

Pet IDNameTypeOwner IDOwner NameCity
101BuddyDog1JohnNew York
102WhiskersCat2SarahLondon
103GoldieFish1JohnNew York
NULLNULLNULL3EmilyParis

Oops! In this case, Emily doesn’t have a pet in our pets table, but the RIGHT JOIN still included her because she’s in the owners table (the right table).

Important Note: The order of the tables in your RIGHT JOIN statement matters! The table on the right side of the RIGHT JOIN keyword is the one whose all rows will be included in the result.

Practice Time!

Imagine you have these tables:

Table: students

Student IDNameGrade
1Aisha5
2Ben6
3Chloe5

Table: clubs

Club IDClub NameStudent ID
10Science Club1
11Art Club2
12Math Club2
13Coding Club4

Questions:

  1. Write a query to list all the clubs and the names of the students who are in them. If a student is in a club, show their name. If a club has a student, show the club name.
  2. Write a query to list all the students and the clubs they are in. If a student is in a club, show the club name. If a student is not in any club, still show their name.
  3. Write a query to list all the clubs. If a club has a student, show the student’s name. If a club has no students, still show the club name.

(Answers below!)

Interview Corner!

Here are some questions you might get asked about Right Joins:

  1. What is the purpose of a Right Join?
    • Answer: A Right Join returns all rows from the right table and the matching rows from the left table. If there’s no match in the left table, it returns NULL values for the columns of the left table.  
  2. How is a Right Join different from a Left Join?
    • Answer: A Left Join returns all rows from the left table and the matching rows from the right table. A Right Join does the opposite; it returns all rows from the right table and the matching rows from the left table.  
  3. Can you achieve the same result as a Right Join using a Left Join? How?
    • Answer: Yes, you can achieve the same result by simply swapping the order of the tables in your FROM clause and using a LEFT JOIN. For example, TABLE_A RIGHT JOIN TABLE_B ON condition is the same as TABLE_B LEFT JOIN TABLE_A ON condition.
  4. When might you use a Right Join?
    • Answer: You might use a Right Join when you need to ensure that all records from a specific table (the right table) are included in your result, regardless of whether there are matching records in another table (the left table). For example, if you want a list of all customers and their order details, even if some customers haven’t placed any orders yet (assuming the customers table is the right table).

Answers to Practice Questions:

  • Solution-1
SELECT c.Club Name, s.Name AS Student Name FROM students AS s JOIN clubs AS c ON s.StudentID = c.StudentID;

(This is an INNER JOIN, which only shows matches. The question was a bit tricky!)

  • Solution-2
SELECT s.Name AS Student Name, c.Club Name FROM students AS s LEFT JOIN clubs AS c ON s.StudentID = c.StudentID;

(This uses a LEFT JOIN to show all students)

  • Solution-3
SELECT c.Club Name, s.Name AS Student Name FROM students AS s RIGHT JOIN clubs AS c ON s.StudentID = c.StudentID;

(This uses a RIGHT JOIN to show all clubs)

So, remember, a Right Join is like making sure you invite all the kids who love playing outside to your party, and if they also like video games, great! If not, they’re still on the guest list!

Keep practicing, and you’ll become a Right Join superstar in no time! Let me know if you have any more questions!