SQL Challenges · WEEKLY QUACK CHALLENGE!

THE STOCKTAKE

Difficulty: medium

Quartermaster Mandarin drops a ledger and a stack of count sheets on the desk, ink still wet. "Winter stocktake, Duckbert. The ledger says what every storeroom should hold, and the counters wrote down what is actually on the shelves. The Tackle Room came up short on hooks, bobbers and bait. The Honey Vault can't find sixty beeswax candles. The Net Locker is full of cork floats nobody ever booked. And the Boathouse counted its life rings one shelf at a time and now swears five in six of them are…

Your task

Return the name of the storeroom with the largest total discrepancy. `ledger` holds the quantity each storeroom should have of each item. `shelf_counts` holds what the counters found; an item kept on several shelves has several rows, and its count is their total. An item's discrepancy is the difference between its ledger quantity and its count, as a positive number whichever side is larger; an item missing from either table counts as zero there. The same item name can appear in more than one storeroom, and each storeroom's stock is separate. A storeroom's total is the sum of its items' discrepancies. On a tie, return the alphabetically first storeroom name. You will be working with the `ledger` and `shelf_counts` tables.

Tables

ledger (25 rows)

columntype
storeroomVARCHAR
itemVARCHAR
on_booksINTEGER

shelf_counts (39 rows)

columntype
count_idINTEGER
storeroomVARCHAR
itemVARCHAR
shelfVARCHAR
countedINTEGER

Database: https://dbquacks.com/data/databases/weekly_202647_20_stocktake.duckdb

This page as markdown: https://dbquacks.com/challenges/weekly/20.md

Play this challenge →

More: practice SQL exercises online · SQL interview questions