runtime
136 / 139

task Join fan-out & cardinality · medium

Predict: payments after joining line items

Predict line_rows and paid_total for the two sample orders when items and payments are joined together.

what to return

Columns
order_idline_rowspaid_total in this order
Rows
in the order asked
Watch for
Use only the sample rows shown. NULL is not zero or empty text. Keep duplicate rows.
Skill
Reason about the exact result of a fixed SQL query. Refresher: Multi-grain joins without fan-out →

data you can query

order_sample2 columns · 2 rows
order_idinvoice_no
211492125
275493071
order_id
211
invoice_no
492125
order_id
275
invoice_no
493071

all 2 rows

item_sample4 columns · 5 rows
order_idstock_codequantityunit_price
211845982880.21
2112140764.25
211845682880.21
27521479183.75
27522114963.45
order_id
211
stock_code
84598
quantity
288
unit_price
0.21
order_id
211
stock_code
21407
quantity
6
unit_price
4.25
order_id
211
stock_code
84568
quantity
288
unit_price
0.21
order_id
275
stock_code
21479
quantity
18
unit_price
3.75
order_id
275
stock_code
22114
quantity
96
unit_price
3.45

all 5 rows

payment_sample3 columns · 4 rows
payment_idorder_idamount
13621143.94
233211102.52
212275398.7
217275398.7
payment_id
136
order_id
211
amount
43.94
payment_id
233
order_id
211
amount
102.52
payment_id
212
order_id
275
amount
398.7
payment_id
217
order_id
275
amount
398.7

all 4 rows

predict.sql
SELECT
  o.order_id,
  COUNT(*) AS line_rows,
  ROUND(SUM(p.amount), 2) AS paid_total
FROM order_sample o
JOIN item_sample i ON i.order_id = o.order_id
JOIN payment_sample p ON p.order_id = o.order_id
GROUP BY o.order_id
ORDER BY o.order_id
prediction.json

Enter the rows you think the query returns, column order order_id, line_rows, paid_total. JSON array of rows — numbers bare, text in quotes, null for NULL. Example: [ [ 1, null, null ] ]. Keep ORDER BY order.

 
Output

Hints and the answer are always available. Using them counts the question as solved with help.

Predict: payments after joining line items — SQL practice · Runtime