Calculate Average Price and Order Count by Category
Beginner Mode

Scenario

You work for an e-commerce platform and have been given two tables containing product information and order records.

Task

Write a Snowflake SQL query that:

  1. Joins {{ ref("products_df") }} with {{ ref("orders_df") }} on product_id
  2. Calculates the average price per category, rounded to 2 decimal places (as avg_price)
  3. Calculates the total number of orders per category (as total_orders_count)
  4. Groups results by category

Schema

products_df

Column Type Description
product_id Integer Unique product identifier
category String Product category name
price Float Product price

orders_df

Column Type Description
order_id Integer Unique order identifier
product_id Integer ID of the ordered product
quantity Integer Quantity ordered

Example

products_df:

product_id category price
1 Apparel 25.99
2 Apparel 35.99
3 Footwear 50.00
4 Footwear 75.00
5 Apparel 20.00

orders_df:

order_id product_id quantity
101 1 2
102 2 1
103 1 3
104 3 1
105 4 2

Expected Output:

category avg_price total_orders_count
Apparel 29.32 3
Footwear 62.50 2

Note: avg_price is the average of joined product prices per category, rounded to 2 decimal places (product 1 appears twice in Apparel because it has two orders). total_orders_count is the count of order rows, not the sum of quantities.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →