SQL GROUP BY with HAVING
A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.
The task
In SQL: `SELECT customer, SUM(amount) AS total FROM orders WHERE status = 'completed' GROUP BY customer HAVING SUM(amount) > 300 ORDER BY total DESC`. Return the same table in pandas, with the columns `customer` and `total`.
What this exercise teaches
pandas has no HAVING keyword because it does not need one. Once the totals are a DataFrame, a normal boolean filter on the total column does the same job. The order of the steps mirrors SQL: filter rows, group, aggregate, filter groups, sort.
A hint if you get stuck
WHERE filters rows before grouping. HAVING filters the grouped result, so it is an ordinary filter applied after the aggregation.
Try it now, free, no account needed
Your first 8 exercises on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro unlocks every exercise, timed mode and the certificate.
Open this exercise