37. Combine Customer Orders and Products
Beginner Mode
Scenario
You are working on a CRM platform that tracks customers, their orders, and the products they purchased.
Task
Write a Snowflake SQL query that:
- Joins
{{ ref("orders") }}to{{ ref("customers") }}oncustomer_id - Joins the result to
{{ ref("products") }}onproduct_id - Concatenates
first_nameandlast_name(separated by a space) ascustomer_name - Aliases
emailascustomer_emailandcategoryasproduct_category - Returns only six columns:
order_id,customer_name,customer_email,product_name,product_category,order_date
Schema
customers
| Column | Type | Description |
|---|---|---|
| customer_id | Integer | Unique customer identifier |
| first_name | String | Customer first name |
| last_name | String | Customer last name |
| String | Customer email address |
orders
| Column | Type | Description |
|---|---|---|
| order_id | Integer | Unique order identifier |
| customer_id | Integer | References customers.customer_id |
| product_id | Integer | References products.product_id |
| order_date | Date | Date the order was placed |
products
| Column | Type | Description |
|---|---|---|
| product_id | Integer | Unique product identifier |
| product_name | String | Name of the product |
| category | String | Product category |
Example
customers:
| customer_id | first_name | last_name | |
|---|---|---|---|
| 1 | John | Doe | [email protected] |
| 2 | Jane | Smith | [email protected] |
orders:
| order_id | customer_id | product_id | order_date |
|---|---|---|---|
| 1001 | 1 | 101 | 2023-01-10 |
| 1002 | 2 | 102 | 2023-01-11 |
products:
| product_id | product_name | category |
|---|---|---|
| 101 | Product A | Category1 |
| 102 | Product B | Category2 |
Expected Output:
| order_id | customer_name | customer_email | product_name | product_category | order_date |
|---|---|---|---|---|---|
| 1001 | John Doe | [email protected] | Product A | Category1 | 2023-01-10 |
| 1002 | Jane Smith | [email protected] | Product B | Category2 | 2023-01-11 |
Note: Each order row is enriched with the customer's full name, email, and the product details by joining all three tables on their respective keys.
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 →
Twilio
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
PayPal
Snowflake
AMD
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