Bird
Raised Fist0
SQLquery~20 mins

Why joins are needed in SQL - Challenge Your Understanding

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Join Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we use JOINs in SQL?

Imagine you have two tables: Customers and Orders. Each order belongs to a customer. Why do we need to use a JOIN to get a list of customers with their orders?

ABecause JOINs delete duplicate rows from tables to keep data clean.
BBecause JOINs create new tables permanently to store combined data.
CBecause JOINs sort the data alphabetically in the result set.
DBecause JOINs combine rows from two or more tables based on a related column, allowing us to see connected data in one result.
Attempts:
2 left
💡 Hint

Think about how you can see information from both tables together in one list.

query_result
intermediate
2:00remaining
Output of INNER JOIN between Customers and Orders

Given these tables:

Customers:
1, Alice
2, Bob

Orders:
101, 1
102, 2
103, 1

What is the result of this query?

SELECT Customers.Name, Orders.OrderID FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID;
AAlice, 101<br>Alice, 103<br>Bob, 102
BAlice, 101<br>Bob, 102<br>NULL, 103
CAlice, 101<br>Bob, 102
DAlice, 101<br>Alice, 103
Attempts:
2 left
💡 Hint

INNER JOIN returns rows where the join condition matches in both tables.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this JOIN query

Which option contains a syntax error in the JOIN clause?

SELECT * FROM Customers JOIN Orders ON Customers.ID = Orders.CustomerID;
ASELECT * FROM Customers JOIN Orders ON Customers.ID = Orders.CustomerID ORDER BY;
BSELECT * FROM Customers JOIN Orders ON Customers.ID = Orders.CustomerID;
CSELECT * FROM Customers JOIN Orders Customers.ID = Orders.CustomerID;
DSELECT * FROM Customers JOIN Orders ON Customers.ID = Orders.CustomerID WHERE;
Attempts:
2 left
💡 Hint

Look for missing keywords or misplaced clauses in the JOIN syntax.

optimization
advanced
2:00remaining
Optimizing JOINs for performance

You have two large tables: Employees and Departments. You want to list employees with their department names. Which option is best to improve query speed?

AUse CROSS JOIN to combine all rows and then filter with WHERE.
BUse INNER JOIN on indexed columns and select only needed columns.
CUse LEFT JOIN without any indexes and select all columns.
DUse subqueries instead of JOINs to get department names.
Attempts:
2 left
💡 Hint

Think about how indexes and selecting fewer columns affect speed.

🔧 Debug
expert
2:00remaining
Why does this JOIN query return fewer rows than expected?

Tables:

Students: 1, John
2, Mary
3, Alex

Enrollments: 1, Math
2, Science

Query:

SELECT Students.Name, Enrollments.Course FROM Students INNER JOIN Enrollments ON Students.ID = Enrollments.StudentID;

Why does Alex not appear in the result?

ABecause INNER JOIN only returns rows with matching keys in both tables, and Alex has no enrollment record.
BBecause Alex's name is misspelled in the Students table.
CBecause the query syntax is incorrect and skips some rows.
DBecause the Enrollments table has duplicate courses causing conflicts.
Attempts:
2 left
💡 Hint

Think about how INNER JOIN works when one table has missing matching rows.

Practice

(1/5)
1. Why do we use JOIN in SQL when working with multiple tables?
easy
A. To combine related data from two or more tables into one result
B. To delete rows from a table
C. To create a new table
D. To change the data type of a column

Solution

  1. Step 1: Understand the purpose of JOIN

    JOIN is used to bring together rows from different tables based on a related column.
  2. Step 2: Identify the correct use case

    Deleting rows, creating tables, or changing data types are not done with JOIN.
  3. Final Answer:

    To combine related data from two or more tables into one result -> Option A
  4. Quick Check:

    JOIN combines tables = To combine related data from two or more tables into one result [OK]
Hint: JOIN merges tables on related columns to see connected data [OK]
Common Mistakes:
  • Thinking JOIN deletes or modifies tables
  • Confusing JOIN with CREATE or DELETE commands
  • Assuming JOIN changes data types
2. Which of the following is the correct syntax to join two tables Employees and Departments on the column DepartmentID?
easy
A. SELECT * FROM Employees WHERE DepartmentID = Departments.DepartmentID;
B. SELECT * FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
C. SELECT * FROM Employees JOIN Departments USING (DepartmentID);
D. SELECT * FROM Employees JOIN Departments ON Employees.ID = Departments.ID;

Solution

  1. Step 1: Check the JOIN condition syntax

    The correct JOIN syntax uses ON with matching columns: Employees.DepartmentID = Departments.DepartmentID.
  2. Step 2: Verify the options

    SELECT * FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; uses correct ON syntax with matching columns. SELECT * FROM Employees WHERE DepartmentID = Departments.DepartmentID; uses WHERE incorrectly. SELECT * FROM Employees JOIN Departments USING (DepartmentID); uses USING with correct parentheses. SELECT * FROM Employees JOIN Departments ON Employees.ID = Departments.ID; joins on wrong columns.
  3. Final Answer:

    SELECT * FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; -> Option B
  4. Quick Check:

    JOIN with ON and matching columns = SELECT * FROM Employees JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; [OK]
Hint: Use JOIN ... ON table1.col = table2.col for correct syntax [OK]
Common Mistakes:
  • Using WHERE instead of ON for JOIN condition
  • Joining on wrong columns
  • Misusing USING without parentheses
3. Given two tables:
Students(id, name)
Grades(student_id, grade)
What will this query return?
SELECT Students.name, Grades.grade FROM Students JOIN Grades ON Students.id = Grades.student_id;
medium
A. An error because of missing WHERE clause
B. All students with NULL grades included
C. Only grades without student names
D. A list of student names with their grades where student IDs match

Solution

  1. Step 1: Understand INNER JOIN behavior

    JOIN without specifying LEFT or RIGHT is INNER JOIN, which returns rows with matching keys in both tables.
  2. Step 2: Analyze the query result

    The query returns student names and grades only where Students.id matches Grades.student_id.
  3. Final Answer:

    A list of student names with their grades where student IDs match -> Option D
  4. Quick Check:

    INNER JOIN returns matching rows = A list of student names with their grades where student IDs match [OK]
Hint: INNER JOIN returns only matching rows from both tables [OK]
Common Mistakes:
  • Expecting all students even without grades
  • Thinking JOIN returns unmatched rows
  • Assuming WHERE is needed for JOIN condition
4. You wrote this query:
SELECT * FROM Orders JOIN Customers ON Orders.CustomerID = Customers.ID;

But it returns an error. What is the most likely cause?
medium
A. The column names in ON clause do not match actual table columns
B. JOIN keyword is not supported in SQL
C. Missing WHERE clause after JOIN
D. SELECT * cannot be used with JOIN

Solution

  1. Step 1: Check column names in ON clause

    If column names Orders.CustomerID or Customers.ID do not exist, SQL throws an error.
  2. Step 2: Verify other options

    JOIN is valid SQL keyword, WHERE is optional, and SELECT * works with JOIN.
  3. Final Answer:

    The column names in ON clause do not match actual table columns -> Option A
  4. Quick Check:

    Wrong column names cause JOIN errors = The column names in ON clause do not match actual table columns [OK]
Hint: Check column names in ON clause carefully to avoid errors [OK]
Common Mistakes:
  • Assuming JOIN keyword is invalid
  • Thinking WHERE is mandatory after JOIN
  • Believing SELECT * cannot be used with JOIN
5. You have two tables:
Authors(author_id, name)
Books(book_id, title, author_id)
You want to list all authors and their books, including authors who have no books yet. Which SQL join should you use?
hard
A. INNER JOIN
B. RIGHT JOIN
C. LEFT JOIN
D. CROSS JOIN

Solution

  1. Step 1: Understand the requirement

    We want all authors listed, even if they have no books. This means we keep all rows from Authors.
  2. Step 2: Choose the correct JOIN type

    LEFT JOIN keeps all rows from the left table (Authors) and matches books if available, else NULL.
  3. Step 3: Exclude other JOIN types

    INNER JOIN excludes authors without books, RIGHT JOIN keeps all books, CROSS JOIN creates all combinations.
  4. Final Answer:

    LEFT JOIN -> Option C
  5. Quick Check:

    LEFT JOIN keeps all left table rows = LEFT JOIN [OK]
Hint: Use LEFT JOIN to keep all rows from the first table [OK]
Common Mistakes:
  • Using INNER JOIN and missing authors without books
  • Confusing RIGHT JOIN with LEFT JOIN
  • Using CROSS JOIN which multiplies rows