Topics / 🗄️ SQL, pandas & Data Analysis / 📈 Level 17: Analyst's SQL

SQL exercise · about 5 minutes · free

Pivot with Conditional Sums

Managers want one row per player and one column per product. SQL can pivot without any special syntax.

Conditional aggregation puts a CASE inside an aggregate: SUM(CASE WHEN item = 'skin' THEN amount ELSE 0 END) adds up only skin sales. One expression per column gives you a pivot table.

Table. orders: player_id, item, amount; 1 | skin | 300; 1 | boost | 150; 1 | skin | 300; 2 | pass | 500; … | … | …. Operation: SUM(CASE WHEN item = …). Result pivot: player_id, skin, pass, boost; 1 | 600 | 0 | 150; 2 | 300 | 500 | 150; 3 | 300 | 0 | 150; … | … | … | …ordersplayer_iditemamount1skin3001boost1501skin3002pass500………pivotplayer_idskinpassboost16000150230050015033000150…………SUM(CASE WHEN item = …)
Your quest: For each player_id, show total spend on skin, pass and boost as three columns with those names.
Database: players(id, name, class, level, country) · matches(id, player_id, map, kills, deaths, score) · orders(id, player_id, order_date, item, amount) · app_events(user_id, event, day) · employees(id, name, dept, salary, manager_id, hire_date) · logins(user_id, day) · payment_events(payment_id, user_id, amount, status, updated_at)
Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
HintSUM(CASE WHEN item = 'skin' THEN amount ELSE 0 END) AS skin, ... GROUP BY player_id
Output appears here.

Open it in the full course → Free with Google: saves your progress and unlocks 627 lessons.

In this level you'll learn:
More in Level 17: Analyst's SQL

Share on LinkedIn