Bird
0
0

You want to list all products and their category names. The products table has category_id, and the categories table has id and name. Which approach is better for performance and why?

hard📝 optimization Q15 of 15
SQL - Subqueries
You want to list all products and their category names. The products table has category_id, and the categories table has id and name. Which approach is better for performance and why?

Options:
A) Use a JOIN to combine products and categories.
B) Use a subquery in SELECT to get category name for each product.
C) Use a subquery in WHERE to filter products by category name.
D) Use UNION to combine products and categories.
ASubquery in SELECT is better because it runs once per product.
BJOIN is better because it retrieves all data in one step efficiently.
CSubquery in WHERE is better because it filters early.
DUNION is better because it merges tables.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the data retrieval goal

    You want product info with category names, which requires combining data from two tables.
  2. Step 2: Compare approaches

    JOIN combines tables in one efficient operation. Subqueries in SELECT run once per row, causing slower performance. Subquery in WHERE filters but doesn't retrieve category names. UNION merges rows, not related here.
  3. Final Answer:

    JOIN is better because it retrieves all data in one step efficiently. -> Option B
  4. Quick Check:

    JOIN efficiency > subqueries for this task [OK]
Quick Trick: JOIN combines tables efficiently for related data [OK]
Common Mistakes:
MISTAKES
  • Using subquery in SELECT causing slow per-row lookup
  • Confusing UNION with JOIN
  • Using subquery in WHERE without retrieving needed data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes