Bird
0
0

Write a query to find each product with the discount it qualifies for based on price using a non-equi join.

hard📝 Application Q8 of 15
SQL - Advanced Joins
You have tables Products(product_id, price) and DiscountRanges(range_id, min_price, max_price, discount). Write a query to find each product with the discount it qualifies for based on price using a non-equi join.
ASELECT p.product_id, d.discount FROM Products p JOIN DiscountRanges d ON p.price < d.min_price;
BSELECT p.product_id, d.discount FROM Products p JOIN DiscountRanges d ON p.price BETWEEN d.min_price AND d.max_price;
CSELECT p.product_id, d.discount FROM Products p JOIN DiscountRanges d ON p.price = d.discount;
DSELECT p.product_id, d.discount FROM Products p JOIN DiscountRanges d ON p.price > d.max_price;
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirement

    We want to match products with discount ranges where product price falls within min and max price.
  2. Step 2: Use BETWEEN for range matching

    BETWEEN d.min_price AND d.max_price checks if price is in the discount range.
  3. Final Answer:

    SELECT p.product_id, d.discount FROM Products p JOIN DiscountRanges d ON p.price BETWEEN d.min_price AND d.max_price; -> Option B
  4. Quick Check:

    Non-equi join with BETWEEN matches price ranges [OK]
Quick Trick: Use BETWEEN to join on price ranges for discounts [OK]
Common Mistakes:
MISTAKES
  • Using '=' instead of BETWEEN for ranges
  • Using only less than or greater than without range
  • Confusing discount column with price

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes