4. Average Order Value
Beginner Mode
Objective
Given a table named Orders that records customer purchases, write an SQL query to determine the average expenditure per customer. Your result should display each customer_id alongside their avg_order_value, rounded to two decimal places. Ensure the output is sorted in ascending order based on customer_id.
Additional information
Table Schema:
The
orderstable:Column Type Description order_id INTEGER Unique identifier for each order customer_id INTEGER Identifier for the customer who made the order total_amount DECIMAL Total amount spent on the order order_date DATE Date when the order was placed Constraints:
- Each
customer_idmay have multiple associated orders. - There is at least one order present in the table.
- Each
Output Requirements:
- Columns:
customer_id,avg_order_value avg_order_valuemust be rounded to two decimal places.- Results must be ordered by
customer_idin ascending order.
- Columns:
Examples
Example 1:
Output:
Input:
| orders | |||
|---|---|---|---|
| customer_id | order_date | order_id | total_amount |
| 101 | 2023-01-01 | 1 | 150.5 |
| 101 | 2023-01-15 | 2 | 200.75 |
| 102 | 2023-01-02 | 3 | 75.25 |
| 102 | 2023-01-20 | 4 | 125 |
| 103 | 2023-01-10 | 5 | 300 |
| avg_order_value | customer_id |
|---|---|
| 175.63 | 101 |
| 100.13 | 102 |
| 300 | 103 |
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 →
Accenture
TCS
X
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
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