Pandasdrill

Level 6: From Excel and SQL Exercise 84 of 102

SQL WHERE, ORDER BY and LIMIT

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 order_id, customer, amount FROM orders WHERE status = 'completed' ORDER BY amount DESC LIMIT 3`. Return the same three rows and columns in pandas, largest amount first.

What this exercise teaches

Each SQL clause has a pandas step: WHERE is a boolean filter, ORDER BY ... DESC is sort_values(ascending=False), LIMIT 3 is head(3) and the SELECT list is the column selection at the end. nlargest(3, "amount") does the sort and the limit in one call.

A hint if you get stuck

Filter the rows, sort them from largest to smallest, keep the first three and then pick the columns.

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

More exercises in From Excel and SQL