Difficulty: Beginner
Estimated time: 15–20 minutes
SQL concepts: INNER JOIN, joining tables, foreign keys
Goal: Combine data from multiple tables into one result.
The Scenario
So far, all queries have used a single table.
In real databases, information is usually split across multiple related tables.
A product manager now asks:
“Show me each product along with its supplier name.”
That information lives in two tables, so you need to join them.
Database Tables
products
| column_name | type | description |
|---|---|---|
| product_id | INTEGER | Unique product ID |
| name | TEXT | Product name |
| category | TEXT | Product category |
| price | DECIMAL | Product price |
| supplier_id | INTEGER | References suppliers.supplier_id |
suppliers
| column_name | type | description |
|---|---|---|
| supplier_id | INTEGER | Unique supplier ID |
| supplier_name | TEXT | Supplier name |
Sample Data
products
| product_id | name | category | price | supplier_id |
|---|---|---|---|---|
| 101 | Wireless Mouse | Accessories | 24.99 | 1 |
| 102 | Mechanical Keyboard | Accessories | 89.00 | 1 |
| 103 | 27-inch Monitor | Displays | 229.99 | 2 |
| 104 | USB-C Hub | Accessories | 34.50 | 3 |
| 105 | Laptop Stand | Workspace | 39.99 | 2 |
suppliers
| supplier_id | supplier_name |
|---|---|
| 1 | TechSource |
| 2 | DisplayWorks |
| 3 | GadgetCo |
Your Task
Write a SQL query that returns:
- Product
name supplier_name
For all products that have a matching supplier.
Expected Output
| name | supplier_name |
|---|---|
| Wireless Mouse | TechSource |
| Mechanical Keyboard | TechSource |
| 27-inch Monitor | DisplayWorks |
| USB-C Hub | GadgetCo |
| Laptop Stand | DisplayWorks |
Constraints
- Use
INNER JOIN - Join
productsandsuppliersusingsupplier_id - Return only the requested columns
- Do not use
SELECT *
Hint (Optional)
An INNER JOIN returns only rows where the join condition matches in both tables.
SELECT
p.name,
s.supplier_name
FROM products p
INNER JOIN suppliers s
ON p.supplier_id = s.supplier_id;
Explanation
INNER JOINcombines rows from both tables where the IDs match.- Table aliases (
p,s) make queries easier to read. - Only products with a valid supplier are returned.
Common Mistakes
- Forgetting the join condition
FROM products p, suppliers s -- ❌ creates a cartesian product - Joining on the wrong column
- Using
SELECT *in joins
Optional Extension (Mini Bonus)
Try:
- Add product
priceto the result. - Filter results to only
Accessories. - Sort products by supplier name.
Next Challenge
Beginner Challenge #9: Keep All Products with LEFT JOIN
🔗 View reference solution on GitHub
(After you’ve tried the challenge)
Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.