Count Bottles and Cans Across Pallets
Problem
Table: `pallet`
| Column Name | Type |
|---|---|
| pallet_id | int |
| crate_id | int |
| bottle_qty | int |
| can_qty | int |
`pallet_id` is the primary key. A pallet holds some loose bottles and cans (`bottle_qty`, `can_qty`) and may optionally hold one crate identified by `crate_id` (NULL if the pallet has no crate).
Table: `crate`
| Column Name | Type |
|---|---|
| crate_id | int |
| bottle_qty | int |
| can_qty | int |
`crate_id` is the primary key. A crate holds its own bottles and cans.
Report the total number of bottles and cans across **all** pallets, including the contents of any crate sitting on a pallet. Output two columns aliased `bottle_qty` and `can_qty`.
Tables
Example rows — the live problem includes the full dataset.
| pallet_id | crate_id | bottle_qty | can_qty |
|---|---|---|---|
| 1 | 100 | 5 | 2 |
| 2 | 3 | 4 | |
| 3 | 101 | 0 | 1 |
| crate_id | bottle_qty | can_qty |
|---|---|---|
| 100 | 10 | 20 |
| 101 | 7 | 0 |
Expected output
Your answer should return 1 row with the columns bottle_qty, can_qty.
Starter code (SQL)
SELECT *
FROM pallet;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock