Average Order Value by City You work as a data analyst for ShopNest , a growing e-commerce company. The marketing team wants to understand purchasing behaviour across different cities to help allocate regional advertising budgets more effectively. You have access to two tables: Table: customers Column Type Description customer_id INT Unique customer identifier name VARCHAR Full name of the customer city VARCHAR City where the customer is located Table: orders Column Type Description order_id INT Unique order identifier customer_id INT References the customer who placed the order order_date DATE Date the order was placed amount NUMERIC(10,2) Total monetary value of the order Your Task: Write a query that returns the average order value per city , rounded to 2 decimal places. Only include cities that have at least 2 orders . Sort the results by average order value in descending order . Expected Output Columns: city , total_orders , avg_order_value
Table Setup (SQL Schema)
-- DDL: Create tables
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(100),
city VARCHAR(100)
);
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT REFERENCES customers(customer_id),
order_date DATE,
amount NUMERIC(10, 2)
);
-- DML: Insert sample data
INSERT INTO customers (customer_id, name, city) VALUES
(1, 'Alice Tan', 'Singapore'),
(2, 'Bob Lim', 'Singapore'),
(3, 'Carol Ng', 'Bangkok'),
(4, 'David Chan', 'Bangkok'),
(5, 'Eva Suresh', 'Kuala Lumpur'),
(6, 'Frank Wu', 'Kuala Lumpur'),
(7, 'Grace Park', 'Jakarta'),
(8, 'Henry Malik', 'Jakarta'),
(9, 'Isla Reyes', 'Manila'),
(10, 'James Ong', 'Singapore');
INSERT INTO orders (order_id, customer_id, order_date, amount) VALUES
(1, 1, '2024-01-05', 120.00),
(2, 2, '2024-01-08', 340.50),
(3, 10, '2024-01-12', 220.00),
(4, 3, '2024-01-15', 95.00),
(5, 4, '2024-01-18', 450.75),
(6, 5, '2024-01-20', 310.00),
(7, 6, '2024-01-22', 180.00),
(8, 7, '2024-01-25', 530.00),
(9, 8, '2024-01-27', 290.00),
(10, 9, '2024-01-28', 75.00),
(11, 1, '2024-02-03', 400.00),
(12, 3, '2024-02-07', 210.00),
(13, 5, '2024-02-10', 525.00),
(14, 7, '2024-02-14', 115.00),
(15, 2, '2024-02-17', 60.00);