Bird
Raised Fist0
PostgreSQLquery~20 mins

INNER JOIN execution in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
INNER JOIN Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of INNER JOIN with matching keys
Given two tables Employees and Departments, what is the output of the following query?
SELECT Employees.name, Departments.department_name FROM Employees INNER JOIN Departments ON Employees.department_id = Departments.id;

Assume:
Employees: (id:1, name:'Alice', department_id:10), (id:2, name:'Bob', department_id:20), (id:3, name:'Charlie', department_id:30)
Departments: (id:10, department_name:'HR'), (id:20, department_name:'Finance')
PostgreSQL
SELECT Employees.name, Departments.department_name FROM Employees INNER JOIN Departments ON Employees.department_id = Departments.id;
A[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": "Finance"}]
B[{"name": "Alice", "department_name": "HR"}, {"name": "Bob", "department_name": "Finance"}, {"name": "Charlie", "department_name": null}]
C[{"name": "Charlie", "department_name": "HR"}]
D[]
Attempts:
2 left
💡 Hint
INNER JOIN returns rows where the join condition matches in both tables.
🧠 Conceptual
intermediate
1:30remaining
Understanding INNER JOIN behavior
Which statement best describes the behavior of an INNER JOIN between two tables?
AIt returns all rows from the second table and matching rows from the first table, including unmatched rows as NULLs.
BIt returns all rows from the first table and matching rows from the second table, including unmatched rows as NULLs.
CIt returns all rows from both tables, combining them regardless of matching values.
DIt returns only rows where there is a matching value in both tables based on the join condition.
Attempts:
2 left
💡 Hint
Think about which rows appear when both tables have matching keys.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in INNER JOIN query
Which option contains a syntax error in the INNER JOIN query?
PostgreSQL
SELECT e.name, d.department_name FROM Employees e INNER JOIN Departments d ON e.department_id = d.id;
ASELECT e.name, d.department_name FROM Employees e INNER JOIN Departments d ON e.department_id = d.id ORDER BY e.name;
BSELECT e.name, d.department_name FROM Employees e INNER JOIN Departments d ON e.department_id = d.id LIMIT 5;
CSELECT e.name, d.department_name FROM Employees e INNER JOIN Departments d WHERE e.department_id = d.id;
DSELECT e.name, d.department_name FROM Employees e INNER JOIN Departments d ON e.department_id = d.id;
Attempts:
2 left
💡 Hint
INNER JOIN requires ON clause for join condition, not WHERE.
❓ optimization
advanced
2:00remaining
Optimizing INNER JOIN performance
Which option is the best way to optimize INNER JOIN performance on large tables?
AAdd indexes on the columns used in the join condition in both tables.
BUse a CROSS JOIN instead of INNER JOIN for faster results.
CUse SELECT * to retrieve all columns to avoid specifying columns.
DAvoid using WHERE clause filters after the INNER JOIN.
Attempts:
2 left
💡 Hint
Indexes help the database find matching rows faster.
🔧 Debug
expert
2:30remaining
Diagnose unexpected INNER JOIN result
A query uses INNER JOIN between tables Orders and Customers on Customers.id = Orders.customer_id. The result is empty, but both tables have data. What is the most likely cause?
AThe database engine does not support INNER JOIN.
BThe join condition uses wrong columns or mismatched data types causing no matches.
CINNER JOIN always returns empty if one table has more rows than the other.
DThe SELECT statement is missing a WHERE clause to filter rows.
Attempts:
2 left
💡 Hint
Check if the join columns actually match and have compatible data types.

Practice

(1/5)
1. What does an INNER JOIN do in PostgreSQL?
easy
A. Combines rows from two tables where matching column values exist in both tables.
B. Returns all rows from the first table, and matching rows from the second table.
C. Returns all rows from both tables, matching or not.
D. Deletes rows from both tables where values match.

Solution

  1. Step 1: Understand INNER JOIN purpose

    INNER JOIN returns rows only when there is a match in both tables based on the join condition.
  2. Step 2: Compare options with INNER JOIN behavior

    Combines rows from two tables where matching column values exist in both tables. correctly describes this behavior. Options B and C describe LEFT JOIN and FULL OUTER JOIN respectively, and D is unrelated.
  3. Final Answer:

    Combines rows from two tables where matching column values exist in both tables. -> Option A
  4. Quick Check:

    INNER JOIN = matched rows only [OK]
Hint: INNER JOIN returns only rows with matches in both tables [OK]
Common Mistakes:
  • Confusing INNER JOIN with LEFT JOIN or FULL JOIN
  • Thinking INNER JOIN returns unmatched rows
  • Assuming INNER JOIN deletes data
2. Which of the following is the correct syntax for an INNER JOIN in PostgreSQL?
easy
A. SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
B. SELECT * FROM table1 INNER JOIN table2 WHERE table1.id = table2.id;
C. SELECT * FROM table1 INNER JOIN table2 USING (id);
D. SELECT * FROM table1 JOIN table2 ON table1.id == table2.id;

Solution

  1. Step 1: Review INNER JOIN syntax

    The correct syntax uses INNER JOIN with ON clause specifying the join condition using a single equals sign (=).
  2. Step 2: Check each option

    SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id; uses correct INNER JOIN syntax with ON and =. SELECT * FROM table1 INNER JOIN table2 WHERE table1.id = table2.id; missing ON or USING clause, syntax error. SELECT * FROM table1 INNER JOIN table2 USING (id); uses correct USING syntax with parentheses. SELECT * FROM table1 JOIN table2 ON table1.id == table2.id; uses double equals (==) which is invalid in SQL.
  3. Final Answer:

    SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id; -> Option A
  4. Quick Check:

    INNER JOIN uses ON with = [OK]
Hint: Use ON with single = for INNER JOIN conditions [OK]
Common Mistakes:
  • Using WHERE instead of ON for join condition
  • Using double equals (==) instead of single =
  • Confusing USING clause applicability
3. Given the tables:
employees(id, name, dept_id)
departments(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.id;
medium
A. All employees, even if they have no matching department.
B. An error because of alias usage.
C. All departments with employee names, even if no employees exist.
D. All employees with their department names, only if dept_id matches a department id.

Solution

  1. Step 1: Understand INNER JOIN effect on employees and departments

    The INNER JOIN returns rows where employees.dept_id matches departments.id, so only employees with a valid department appear.
  2. Step 2: Analyze query output

    The query selects employee names and their department names for matching rows only. Employees without a matching department are excluded.
  3. Final Answer:

    All employees with their department names, only if dept_id matches a department id. -> Option D
  4. Quick Check:

    INNER JOIN filters unmatched rows [OK]
Hint: INNER JOIN shows only matching rows from both tables [OK]
Common Mistakes:
  • Assuming unmatched employees appear
  • Confusing aliases causing errors
  • Thinking departments without employees appear
4. Identify the error in this INNER JOIN query:
SELECT * FROM orders INNER JOIN customers ON orders.customer_id = customers.id WHERE orders.date > '2023-01-01'
medium
A. Missing table alias causing ambiguity.
B. WHERE clause cannot be used with INNER JOIN.
C. No error; query is valid and will run correctly.
D. Incorrect join condition syntax; should use USING instead of ON.

Solution

  1. Step 1: Check JOIN syntax and WHERE clause

    The INNER JOIN uses ON with a valid condition. The WHERE clause filters rows after the join, which is allowed.
  2. Step 2: Verify if any syntax or semantic errors exist

    There is no ambiguity or syntax error. Aliases are optional. USING is an alternative but not required.
  3. Final Answer:

    No error; query is valid and will run correctly. -> Option C
  4. Quick Check:

    INNER JOIN with ON and WHERE is valid [OK]
Hint: WHERE filters after JOIN; no syntax error here [OK]
Common Mistakes:
  • Thinking WHERE cannot be used with JOIN
  • Confusing USING and ON as mandatory
  • Assuming aliases are always required
5. You have two tables:
products(id, name)
sales(product_id, quantity)
Write a query to find product names with total quantity sold, but only for products that have sales.
Which query correctly uses INNER JOIN to achieve this?
hard
A. SELECT p.name, SUM(s.quantity) FROM sales s LEFT JOIN products p ON s.product_id = p.id GROUP BY p.name;
B. SELECT p.name, SUM(s.quantity) FROM products p INNER JOIN sales s ON p.id = s.product_id GROUP BY p.name;
C. SELECT p.name, SUM(s.quantity) FROM products p LEFT JOIN sales s ON p.id = s.product_id GROUP BY p.name;
D. SELECT p.name, SUM(s.quantity) FROM products p INNER JOIN sales s ON p.id = s.product_id WHERE p.name > 0 GROUP BY p.name;

Solution

  1. Step 1: Understand requirement for products with sales only

    INNER JOIN returns only products that have matching sales records, excluding products without sales.
  2. Step 2: Analyze each query

    SELECT p.name, SUM(s.quantity) FROM sales s LEFT JOIN products p ON s.product_id = p.id GROUP BY p.name; uses LEFT JOIN, includes unmatched sales (NULL p.name). SELECT p.name, SUM(s.quantity) FROM products p LEFT JOIN sales s ON p.id = s.product_id GROUP BY p.name; uses LEFT JOIN, includes products without sales. SELECT p.name, SUM(s.quantity) FROM products p INNER JOIN sales s ON p.id = s.product_id GROUP BY p.name; uses INNER JOIN correctly. SELECT p.name, SUM(s.quantity) FROM products p INNER JOIN sales s ON p.id = s.product_id WHERE p.name > 0 GROUP BY p.name; WHERE p.name > 0 causes type error.
  3. Step 3: Choose best answer matching INNER JOIN usage

    SELECT p.name, SUM(s.quantity) FROM products p INNER JOIN sales s ON p.id = s.product_id GROUP BY p.name; is correct.
  4. Final Answer:

    SELECT p.name, SUM(s.quantity) FROM products p INNER JOIN sales s ON p.id = s.product_id GROUP BY p.name; -> Option B
  5. Quick Check:

    INNER JOIN + GROUP BY sums sales per product [OK]
Hint: Use INNER JOIN and GROUP BY to sum only matching sales [OK]
Common Mistakes:
  • Using LEFT JOIN includes products without sales
  • Forgetting GROUP BY causes errors
  • Confusing table order in JOIN
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard