Bird
0
0

Consider tables:

medium📝 query result Q5 of 15
SQL - LEFT and RIGHT JOIN
Consider tables:
Products:
ProductID | Name
1 | Pen
2 | Pencil

Sales:
SaleID | ProductID
101 | 1
102 | 3

What is the output of:
SELECT Products.Name, Sales.SaleID FROM Products RIGHT JOIN Sales ON Products.ProductID = Sales.ProductID;?
APen | 101<br>Pencil | 102
BPen | 101<br>Pencil | NULL
CPen | 101<br>NULL | 102
DNULL | 101<br>NULL | 102
Step-by-Step Solution
Solution:
  1. Step 1: Identify right table rows

    Sales has SaleID 101 (ProductID 1) and 102 (ProductID 3).
  2. Step 2: Match Products to Sales

    ProductID 1 matches Pen; ProductID 3 has no matching product, so Name is NULL.
  3. Final Answer:

    Pen | 101
    NULL | 102
    -> Option C
  4. Quick Check:

    RIGHT JOIN keeps all sales, unmatched products NULL = Pen | 101
    NULL | 102 [OK]
Quick Trick: RIGHT JOIN keeps all right rows, unmatched left columns NULL [OK]
Common Mistakes:
MISTAKES
  • Assuming unmatched right rows are excluded
  • Mixing up ProductID and SaleID
  • Expecting all products to appear

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes