cswork / libraryOpen practice workspace

Coding questions · Capital One

Find Lowest Prices for Highly Rated Categories

This question evaluates competency in data manipulation and aggregation using SQL or Python, focusing on grouping, conditional aggregation, and handling related product and purchase records. It is commonly asked for Data Manipulation (SQL/Python) roles to assess practical application of filtering and aggregation logic for per-category summaries rather than purely conceptual understanding.

You are given two tables containing product catalog information and purchase records.

Tables

products

ColumnTypeDescription
idINTPrimary key for the product.
categoryVARCHARProduct category, such as apple, orange, or banana.
product_nameVARCHARName of the product.

purchases

ColumnTypeDescription
idINTPrimary key for the purchase record.
product_idINTForeign key referencing products.id.
priceDECIMAL(10,2)Purchase price of the product.
starsDECIMAL(2,1)Customer rating for the purchase.

Task

For each product category in products, return the lowest purchase price in that category only if the category has at least one purchase with stars > 4.

If a category does not have any purchase with stars > 4, return 0 as the price for that category.

Assumptions

  • Include every category that appears in products.
  • A category may contain multiple products and each product may have multiple purchase records.
  • If a category has at least one purchase with stars > 4, compute the minimum price across purchases in that category.
  • If a category has no purchases, return 0.
  • There is no time filter for this question, so timezone handling is not applicable.

Required Output

Return one row per category.

ColumnTypeDescription
categoryVARCHARProduct category.
priceDECIMAL(10,2) or INTLowest price for qualifying categories; otherwise 0.

Example output format:

categoryprice
apple0

Find Lowest Prices for Highly Rated Categories

Table products

columntypeconstraints
idINTPRIMARY KEY
categoryVARCHAR
product_nameVARCHAR

Sample data:

categoryidproduct_name
apple1Apple A
apple2Apple B
orange3Orange O1
orange4Orange O2
banana5Banana B1

Table purchases

columntypeconstraints
idINTPRIMARY KEY
product_idINTFOREIGN KEY REFERENCES products(id)
priceDECIMAL(10,2)
starsDECIMAL(2,1)

Sample data:

idpriceproduct_idstars
1013.513
1022.7524
103534.5
104433.5
105645

Expected output

One row per distinct category from products. If the category has at least one purchase with stars > 4, price is the minimum of all purchase prices in that category; otherwise price is 0. Categories with no purchases also return 0.

categoryprice
apple0
banana0
orange4

Hints

  1. Start from the list of categories in products (SELECT DISTINCT).
  2. LEFT JOIN purchases to include categories with no purchases.
  3. Use aggregation per category: MIN(price) for the overall lowest price, and a flag like MAX(CASE WHEN stars > 4 THEN 1 ELSE 0 END) to detect high-rated purchases.
  4. Return 0 when the high-rated flag is not present (or when there are no purchases).