Objective
Create a Python program that relates two tables with LEFT JOIN and computes each customer's total, even if they have no orders. Use an in-memory database with the sqlite3 module (sqlite3.connect(":memory:")).
Instructions
- Create
customers(idinteger primary key,nametext) andorders(customer_idandamountintegers). - Read
ncustomer names (one per line): theiridwill be 1, 2, 3... in insertion order (generated automatically). - Read
mand thenmlines withindex amount, whereindexis the position of the customer (1 is the first). Insert each order. - With
LEFT JOINandGROUP BYget, per customer, the sum of amounts (COALESCEso that it is 0 when there are no orders) and the number of orders. Sort by name and showname: total (orders: k).
3
ana
bea
carlos
4
1 10
1 20
3 5
1 1
ana: 31 (orders: 3)
bea: 0 (orders: 0)
carlos: 5 (orders: 1)
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise