AnalystPath

Count Bottles and Cans Across Pallets

SQLMediumMid level~15 min

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
pallet_idcrate_idbottle_qtycan_qty
110052
234
310101
crate
crate_idbottle_qtycan_qty
1001020
10170

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

Related SQL questions