WeCodeAcademy

Step 6: Cross-check with SQL

Real data usually lives in a database, and the same questions can be asked in SQL. The practice database has customers, products and orders. First, revenue and units by product category, using a join and GROUP BY:

Examplesql
Open in editor »

Then, which cities place at least two orders, with their revenue and average order? HAVING filters the groups after they are formed:

Examplesql
Open in editor »

The Python and SQL versions mirror each other: a Python dictionary of totals is a GROUP BY, sorted(..., reverse=True) is ORDER BY ... DESC, and the if that filters groups is HAVING. Use Python when the data is a file you are cleaning; use SQL when it already lives in a database.

Your turn: write the code

Return the name of each product with the total units ordered, most units first. Columns: name, units.

Quick check

Which SQL clause filters groups after GROUP BY?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.