Find Average Order Amount for Each Customer
SQL coding challenge · Difficulty: easy · Topic: Aggregation · +50 XP
Problem
The pricing team wants to understand average basket size per customer. Only include customers who have placed at least one order.
Tables
Table: customers
| customer_id | customer_name | status | register_date | | --- | --- | --- | --- | | 1 | Krishna | Active | 2024-05-01 | | 2 | Parvati | Active | 2024-05-10 | | 3 | Atul | Inactive | 2024-04-01 | | 4 | Pinki | Active | 2024-05-20 | | 5 | Sudheer | Active | 2024-04-01 |
Table: orders
| order_id | customer_id | order_date | order_amount | | --- | --- | --- | --- | | 101 | 1 | 2024-05-01 | 500 | | 102 | 1 | 2024-05-05 | 800 | | 103 | 1 | 2024-05-10 | 200 | | 104 | 2 | 2024-05-02 | 1200 | | 105 | 2 | 2024-05-08 | 600 | | 106 | 3 | 2024-05-03 | 300 | | 107 | 4 | 2024-05-04 | 200 | | 108 | 4 | 2024-05-09 | 400 | | 109 | 4 | 2024-05-11 | 700 | | 110 | 4 | 2024-05-12 | 350 | | 111 | 4 | 2024-05-13 | 450 | | 112 | 4 | 2024-05-14 | 600 | | 113 | 1 | 2024-06-05 | 1000 | | 114 | 2 | 2024-06-10 | 900 |
Expected Output
| customer_id | customer_name | avg_amount | | --- | --- | --- | | 1 | Krishna | 625.000000 | | 2 | Parvati | 900.000000 | | 3 | Atul | 300.000000 | | 4 | Pinki | 450.000000 |
- Return:
customer_id,customer_name,avg_amount - Only customers with at least 1 order
- Sort by
avg_order_amountdescending
Success Criteria
- One row per customer.
- The average is over that customer's own orders.
- The rows match the Expected Output exactly, with no extras.
- Row order is not graded; the set of rows is.
What this SQL challenge teaches you
“Find Average Order Amount for Each Customer” is a easy-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with AVG, ROUND, GROUP BY, ORDER BY — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- AVG
- ROUND
- GROUP BY
- ORDER BY
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- An average per customer means one group per customer, and the aggregate is computed inside that group.
- GROUP BY the customer, then AVG the order amount.
- AVG ignores NULLs rather than treating them as zero -- worth knowing when a customer has none.
Where this comes up
Variations of this problem have been reported in interviews at Amazon, Infosys, Flipkart, Google. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Find Duplicate Emails
- HR: Average Salary by Department
- Logistics: Count Shipments by Status
- Count Total Orders Placed by Each Customer
- Find Customers Who Placed More Than 5 Orders (HAVING)
- Find the Most Recent Order Date for Each Customer
Helpful resources
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated easy and covers Aggregation.