About this problem
The business question
At month-end close, finance compares what was sold with what was actually paid. Some customers pay in instalments, so one order can have several payments, and some orders have not been paid at all. Last quarter's report showed more revenue than was ever invoiced, and nobody could say why.
Expected result: One row per order month with orders, booked_revenue, collected and outstanding, counting every order once whether it has one, several, or no payments.
What you will practice
Tables
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
order_total NUMERIC(10, 2) NOT NULL
);
CREATE TABLE payments (
payment_id INT PRIMARY KEY,
order_id INT NOT NULL REFERENCES orders(order_id),
paid_at DATE NOT NULL,
amount NUMERIC(10, 2) NOT NULL,
method VARCHAR(20) NOT NULL
);More Joins practice
20 problems in this topic, with key concepts and common mistakes.