39. Product Sales and Inventory Data
Beginner Mode
Scenario
You work for a food and beverage company and have three tables containing product details, sales records, and inventory stock levels across warehouses.
Task
Write a Snowflake SQL query that:
- Aggregates
{{ ref("sales") }}to computetotal_quantity(sum ofquantity) andtotal_revenue(sum ofrevenue) per product - Aggregates
{{ ref("inventory") }}to computetotal_stock(sum ofstock) per product - Joins both aggregations to
{{ ref("products") }}usingLEFT JOINso all products appear even if they have no sales or inventory records - Replaces any
NULLvalues intotal_quantity,total_revenue, andtotal_stockwith0 - Returns columns:
product_id,name,category,total_quantity,total_revenue,total_stock
Schema
products
| Column | Type | Description |
|---|---|---|
| product_id | Integer | Unique product identifier |
| name | String | Product name |
| category | String | Product category |
sales
| Column | Type | Description |
|---|---|---|
| sale_id | Integer | Unique sale identifier |
| product_id | Integer | Product that was sold |
| quantity | Integer | Number of units sold |
| revenue | Integer | Revenue from the sale |
inventory
| Column | Type | Description |
|---|---|---|
| product_id | Integer | Product identifier |
| stock | Integer | Units in stock |
| warehouse | String | Warehouse location |
Example
products:
| product_id | name | category |
|---|---|---|
| 1 | Green Tea | Beverages |
| 2 | Trail Mix | Snacks |
| 3 | Sparkling Water | Beverages |
| 4 | Granola Bar | Snacks |
| 5 | Mango Smoothie | Beverages |
sales:
| sale_id | product_id | quantity | revenue |
|---|---|---|---|
| 1 | 1 | 12 | 24 |
| 2 | 1 | 8 | 16 |
| 3 | 2 | 5 | 15 |
| 4 | 3 | 20 | 40 |
| 5 | 4 | 3 | 9 |
inventory:
| product_id | stock | warehouse |
|---|---|---|
| 1 | 60 | WarehouseA |
| 2 | 45 | WarehouseA |
| 3 | 30 | WarehouseB |
| 4 | 25 | WarehouseA |
| 5 | 15 | WarehouseB |
Expected Output:
| product_id | name | category | total_quantity | total_revenue | total_stock |
|---|---|---|---|---|---|
| 1 | Green Tea | Beverages | 20 | 40 | 60 |
| 2 | Trail Mix | Snacks | 5 | 15 | 45 |
| 3 | Sparkling Water | Beverages | 20 | 40 | 30 |
| 4 | Granola Bar | Snacks | 3 | 9 | 25 |
| 5 | Mango Smoothie | Beverages | 0 | 0 | 15 |
Note: Product 5 (Mango Smoothie) has no sales records, so
total_quantityandtotal_revenueare0instead ofNULL. All products appear in the output regardless of whether they have sales or inventory records.
Code Environment
Sign in or try as guest to run your code.
Essential
SQL 0/33
Git 0/15
Spark 0/20
Snowflake 0/22
Python 0/24
Need more practice in this area? Explore more questions →
PayPal
TCS
X
Accenture
Adobe
Google
LinkedIn
Samsung
Datadog
Wix
Dropbox
Meta
OpenAI
Hulu
Uber
DoorDash
Anthropic
Amazon
ActivisionBlizzard
Vercel
Crypto.Com
Zscaler
DeutscheBank
Apple
GoDaddy
BMW
Snowflake
AMD
Twilio
Atlassian
JPMorgan
NVIDIA
IBM
Databricks
Coinbase
Cisco
Robinhood
Twitter
Microsoft
Palantir
Netflix
VMware
Cloudflare
Stripe
Capital One
Splunk
Intel
SAP
Tesla
GitHub
JaneStreet
Bloomberg
Salesforce
Elastic
CGI
UBS
GitLab
Ubisoft
Slack
Nintendo
EY
Kayak
Lyft
Airbnb
Walmart
Revolut
Visa
Okta
HashiCorp
Instacart
Mastercard