The spaces that break routing

The spaces that break routing

You are a data engineer at a logistics operator. Handheld scanners in the warehouse add random spaces before and after the destination city name, so the sorting hub fails to match the destination against its directory. The operations team needs a report: the cleaned city name and exactly how many whitespace characters had to be removed per shipment — that tells them which scanners misbehave the most. Return columns: - shipment_id — shipment id (integer); - city_clean — city name with leading and trailing spaces removed (do not touch spaces between words); - chars_removed — how many characters were removed (length of the raw string minus length of the cleaned one, integer). Exclude shipments where the city is NULL. If a value consisted of spaces only, city_clean becomes an empty string — keep such rows in the report. Sorting: by chars_removed descending, ties by shipment_id ascending.

Expected output sample

This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables. Your answer must use the same column names as this sample — alias them with AS if needed.

shipment_idcity_cleanchars_removed
3Antwerp4
2Hamburg3
7Genoa3
PostgreSQLv16

Your query result will appear here

Focus radio
Paused · SomaFM · Fluid