📖 Scenario: You are working with two tables in a company's database: employees and departments. You want to learn how to combine data from these tables to see which employee works in which department.
🎯 Goal: Build SQL queries step-by-step to join the employees and departments tables using different types of joins.
📋 What You'll Learn
Create the employees table with exact columns and data
Create the departments table with exact columns and data
Write a SQL query using INNER JOIN to combine employees with their departments
Write a SQL query using LEFT JOIN to include all employees even if they have no department
💡 Why This Matters
🌍 Real World
Joining tables is a common task in databases to combine information like employee details with their department names.
💼 Career
Understanding SQL joins is essential for data analysts, database administrators, and developers to query and report data effectively.
Progress0 / 4 steps
1
Create the employees table with data
Write SQL statements to create a table called employees with columns id (integer), name (text), and department_id (integer). Insert these exact rows: (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', NULL).
DBMS Theory
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Create the departments table with data
Write SQL statements to create a table called departments with columns id (integer) and name (text). Insert these exact rows: (10, 'HR'), (20, 'Engineering'), (30, 'Marketing').
DBMS Theory
Hint
Use the same pattern as the employees table to create departments.
3
Write an INNER JOIN query to combine employees with their departments
Write a SQL query that selects employees.name and departments.name as department using an INNER JOIN on employees.department_id = departments.id.
DBMS Theory
Hint
Use INNER JOIN to combine rows where the department matches.
4
Write a LEFT JOIN query to include all employees even without a department
Write a SQL query that selects employees.name and departments.name as department using a LEFT JOIN on employees.department_id = departments.id to include employees without a department.
DBMS Theory
Hint
Use LEFT JOIN to include all employees, even those without a matching department.
Practice
(1/5)
1. What does an INNER JOIN do in SQL?
easy
A. Returns all rows from the right table, and matched rows from the left
B. Returns all rows from the left table, and matched rows from the right
C. Returns only rows with matching values in both tables
D. Returns all rows from both tables, matching where possible
Solution
Step 1: Understand INNER JOIN behavior
An INNER JOIN returns rows where there is a match in both tables based on the join condition.
Step 2: Compare with other join types
LEFT JOIN returns all from left, RIGHT JOIN all from right, FULL JOIN all from both. INNER JOIN only matched rows.
Final Answer:
Returns only rows with matching values in both tables -> Option C
Quick Check:
INNER JOIN = matched rows only [OK]
Hint: INNER JOIN keeps only matching rows from both tables [OK]
Common Mistakes:
Confusing INNER JOIN with LEFT JOIN
Thinking INNER JOIN returns unmatched rows
Mixing up LEFT and RIGHT JOIN definitions
2. Which of the following is the correct syntax for a LEFT JOIN in SQL?
easy
A. SELECT * FROM table1 LEFT JOIN table2 USING (id);
B. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id;
C. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id;
D. SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
Solution
Step 1: Recall LEFT JOIN syntax
The correct syntax is: SELECT columns FROM table1 LEFT JOIN table2 ON condition;
Step 2: Check each option
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; matches correct syntax. SELECT * FROM table1 JOIN LEFT table2 ON table1.id = table2.id; has wrong order. SELECT * FROM table1 LEFT OUTER JOIN table2 WHERE table1.id = table2.id; uses WHERE instead of ON. SELECT * FROM table1 LEFT JOIN table2 USING (id); uses correct syntax with parentheses.
Final Answer:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id; -> Option D
Quick Check:
LEFT JOIN syntax = FROM table1 LEFT JOIN table2 ON condition [OK]
Hint: LEFT JOIN uses ON, not WHERE, for join condition [OK]
Common Mistakes:
Using WHERE instead of ON for join condition
Incorrect order of JOIN keywords
Confusing USING with ON without matching columns
3. Given two tables: Employees(emp_id, name, dept_id) Departments(dept_id, dept_name) What will this query return?
SELECT e.name, d.dept_name FROM Employees e INNER JOIN Departments d ON e.dept_id = d.dept_id;
medium
A. All employees with their department names, only if dept_id matches
B. All employees, even if they have no department
C. All departments with employees, including departments with no employees
D. All employees and departments, matched or not
Solution
Step 1: Analyze INNER JOIN condition
The INNER JOIN returns rows where Employees.dept_id matches Departments.dept_id.
Step 2: Understand result rows
Only employees with a matching department will appear, showing employee name and department name.
Final Answer:
All employees with their department names, only if dept_id matches -> Option A
Quick Check:
INNER JOIN filters to matching dept_id rows [OK]
Hint: INNER JOIN shows only matching rows from both tables [OK]
Common Mistakes:
Assuming employees without departments appear
Confusing INNER JOIN with LEFT JOIN
Thinking unmatched departments appear
4. Identify the error in this SQL join query:
SELECT * FROM Orders o LEFT JOIN Customers c ON o.customer_id = c.id WHERE c.country = 'USA';
medium
A. The WHERE clause filters out unmatched rows, defeating LEFT JOIN purpose
B. The ON clause should use = instead of LIKE
C. LEFT JOIN should be RIGHT JOIN here
D. Missing GROUP BY clause for aggregation
Solution
Step 1: Understand LEFT JOIN with WHERE filter
LEFT JOIN keeps all rows from Orders, matching Customers or NULL if no match.
Step 2: Effect of WHERE on joined table
WHERE c.country = 'USA' removes rows where c is NULL (no match), so unmatched rows are excluded, negating LEFT JOIN effect.
Final Answer:
The WHERE clause filters out unmatched rows, defeating LEFT JOIN purpose -> Option A
Quick Check:
WHERE on joined table filters NULLs, breaks LEFT JOIN [OK]
Hint: Use ON for join filters, WHERE filters after join [OK]
Common Mistakes:
Filtering joined table in WHERE instead of ON
Confusing LEFT JOIN with RIGHT JOIN
Adding unnecessary GROUP BY
5. You want to list all products and their suppliers, including products without suppliers and suppliers without products. Which join should you use?
hard
A. LEFT JOIN
B. FULL OUTER JOIN
C. RIGHT JOIN
D. INNER JOIN
Solution
Step 1: Understand join requirements
We want all products and all suppliers, even if no matching row exists in the other table.
Step 2: Match join type to requirement
INNER JOIN excludes unmatched rows. LEFT or RIGHT JOIN include unmatched rows from one side only. FULL OUTER JOIN includes unmatched rows from both tables.
Final Answer:
FULL OUTER JOIN -> Option B
Quick Check:
FULL OUTER JOIN returns all rows from both tables [OK]
Hint: Use FULL OUTER JOIN to include unmatched rows from both tables [OK]
Common Mistakes:
Choosing INNER JOIN which excludes unmatched rows
Using LEFT or RIGHT JOIN which include only one side unmatched
Confusing FULL OUTER JOIN syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.