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
INNER JOIN with ON condition
📖 Scenario: You are managing a small bookstore database. You have two tables: Books and Authors. Each book has an author_id that links to the Authors table.You want to find the titles of books along with their authors' names.
🎯 Goal: Build an SQL query that uses INNER JOIN with an ON condition to combine the Books and Authors tables, showing book titles and author names.
📋 What You'll Learn
Create a Books table with columns book_id, title, and author_id.
Create an Authors table with columns author_id and author_name.
Write an INNER JOIN query joining Books and Authors on author_id.
Select the title from Books and author_name from Authors.
💡 Why This Matters
🌍 Real World
Joining tables is common in databases to combine related information, like linking books to their authors.
💼 Career
Understanding INNER JOIN with ON condition is essential for database querying in roles like data analyst, backend developer, and database administrator.
Progress0 / 4 steps
1
Create the Books table with sample data
Write SQL statements to create a table called Books with columns book_id (integer), title (text), and author_id (integer). Insert these exact rows into Books: (1, 'The Great Gatsby', 101), (2, '1984', 102), (3, 'To Kill a Mockingbird', 103).
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Create the Authors table with sample data
Write SQL statements to create a table called Authors with columns author_id (integer) and author_name (text). Insert these exact rows into Authors: (101, 'F. Scott Fitzgerald'), (102, 'George Orwell'), (103, 'Harper Lee').
SQL
Hint
Use CREATE TABLE and INSERT INTO similar to Step 1.
3
Write the INNER JOIN query with ON condition
Write an SQL SELECT query that uses INNER JOIN to join Books and Authors on the condition that Books.author_id = Authors.author_id. Select the columns Books.title and Authors.author_name.
SQL
Hint
Use INNER JOIN Authors ON Books.author_id = Authors.author_id to join the tables.
4
Complete the query with ordering
Add an ORDER BY clause to the previous query to sort the results by Books.title in ascending order.
SQL
Hint
Use ORDER BY Books.title ASC to sort the results alphabetically by title.
Practice
(1/5)
1. What does an INNER JOIN do in SQL when used with an ON condition?
easy
A. It returns all rows from the first table regardless of matches.
B. It returns all rows from both tables, matching or not.
C. It returns all rows from the second table regardless of matches.
D. It returns only rows where the join condition matches in both tables.
Solution
Step 1: Understand INNER JOIN behavior
INNER JOIN returns rows only when the join condition matches rows in both tables.
Step 2: Compare with other join types
Unlike LEFT or RIGHT JOIN, INNER JOIN excludes rows without matches.
Final Answer:
It returns only rows where the join condition matches in both tables. -> Option D
Quick Check:
INNER JOIN = matching rows only [OK]
Hint: INNER JOIN keeps only matching rows from both tables [OK]
Common Mistakes:
Thinking INNER JOIN returns unmatched rows
Confusing INNER JOIN with LEFT JOIN
Ignoring the ON condition effect
2. Which of the following is the correct syntax for an INNER JOIN with an ON condition between tables Employees and Departments on DepartmentID?
easy
A. SELECT * FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
B. SELECT * FROM Employees INNER JOIN Departments ON Employees.ID = Departments.ID;
C. SELECT * FROM Employees JOIN Departments USING DepartmentID;
D. SELECT * FROM Employees INNER JOIN Departments WHERE Employees.DepartmentID = Departments.DepartmentID;
Solution
Step 1: Identify correct INNER JOIN syntax
The INNER JOIN requires the ON keyword followed by the join condition.
Step 2: Check the join condition correctness
The join should be on Employees.DepartmentID = Departments.DepartmentID, not just ID or WHERE clause.
Final Answer:
SELECT * FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID; -> Option A
Quick Check:
INNER JOIN uses ON with condition [OK]
Hint: Use ON keyword with join condition for INNER JOIN [OK]
Common Mistakes:
Using WHERE instead of ON for join condition
Joining on wrong columns
Omitting ON keyword
3. Given these tables:
Employees ID | Name | DeptID 1 | Alice | 10 2 | Bob | 20 3 | Carol | 30
Customers CustomerID | Name 101 | John 102 | Jane 104 | Mike
Write an INNER JOIN query to find total order amount per customer name, including only customers with orders. Which query is correct?
hard
A. SELECT c.Name, SUM(o.Amount) FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY c.CustomerID;
B. SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY c.Name;
C. SELECT c.Name, SUM(o.Amount) FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY c.Name;
D. SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY o.CustomerID;
Solution
Step 1: Understand requirement - total per customer with orders only
INNER JOIN keeps only customers with matching orders; GROUP BY customer name to sum amounts.
Step 2: Check query correctness
SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY c.Name; joins Orders to Customers on CustomerID and groups by c.Name, matching requirement exactly.
Step 3: Compare other options
SELECT c.Name, SUM(o.Amount) FROM Customers c INNER JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY c.CustomerID; groups by CustomerID but c.Name not grouped (SQL error). SELECT c.Name, SUM(o.Amount) FROM Customers c LEFT JOIN Orders o ON c.CustomerID = o.CustomerID GROUP BY c.Name; uses LEFT JOIN (includes customers without orders). SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY o.CustomerID; groups by o.CustomerID (not customer name).
Final Answer:
SELECT c.Name, SUM(o.Amount) FROM Orders o INNER JOIN Customers c ON o.CustomerID = c.CustomerID GROUP BY c.Name; -> Option B
Quick Check:
INNER JOIN with GROUP BY customer name sums orders [OK]
Hint: Join Orders to Customers, group by customer name for totals [OK]