Statement
medium
Top 3 customers by revenue
Write a SQL query: return each customer's name and the total of their paid
orders for the last 30 days. Sort by revenue descending, then by name. Keep
three rows.
Result columns: `name`, `revenue`.
SQL · PostgreSQL
Schema
CREATE TABLE customers (
id integer PRIMARY KEY,
name text NOT NULL,
email text NOT NULL
);
CREATE TABLE orders (
id integer PRIMARY KEY,
customer_id integer NOT NULL REFERENCES customers(id),
total integer NOT NULL,
status text NOT NULL,
created_at timestamptz NOT NULL
);
Data
INSERT INTO customers (id, name, email) VALUES
(1, 'Анна', 'anna@example.com'),
(2, 'Борис', 'boris@example.com'),
(3, 'Вера', 'vera@example.com'),
(4, 'Глеб', 'gleb@example.com');
INSERT INTO orders (id, customer_id, total, status, created_at) VALUES
(10, 1, 42000, 'paid', now() - interval '5 days'),
(11, 3, 31500, 'paid', now() - interval '10 days'),
(12, 2, 12000, 'paid', now() - interval '3 days'),
(13, 1, 5000, 'cancelled', now() - interval '2 days'),
(14, 2, 99999, 'paid', now() - interval '45 days'),
(15, 4, 7000, 'paid', now() - interval '20 days');
AI mentor
The AI mentor is available after you log in: it sees your attempt history and does not spend resources on anonymous requests.
Log in and ask
Hints
1 of 3 unlocked · attempts: 0
Hint 1
Start with paid orders from the last 30 days. Cancelled and older orders must
not contribute to the total.
Hint 2 — unlocks after 1 attempt
Hint 3 — unlocks after 3 attempts