Skip to course content
Free SQL course

SQL for Data Analysis and AI

Module 11 Activity

Scenario

Half your team works in SQL and half in pandas. Two people report different average order values from the same database. You are going to build the thing that makes that impossible to repeat.

Task

  1. In SQL, compute order count and average order value grouped by status.
  2. Pull the orders table into a pandas DataFrame and compute the same figures there.
  3. Compare the two programmatically - with an assertion that fails loudly, not by eye.
  4. Deliberately introduce the classic mismatch: let pandas and SQL disagree on how NULLs are handled, and

observe which figure moves.

  1. Decide where each step belongs, and write down the rule you will follow.

Deliverable

A runnable script that queries, computes both ways, asserts equality, and exits non-zero on mismatch - plus two sentences on which work belongs in SQL and which in pandas.

Check your work

StatusOrdersAverage order value
completed988₹2,718.53
pending12₹1,296.50

Both routes must produce these. If pandas gives you a slightly different average, check rounding first - compute at full precision and round only at the point of display, in both places.

For step 4, the reliable way to make them disagree is to introduce a NULL-heavy column such as feedback.rating: SQL's AVG skips NULLs, and pandas' .mean() skips NaN by default too - but .fillna(0).mean() does not, and neither does a manual sum() / len(). The average drops from 3.05 to 2.68 the moment you divide by the wrong denominator.

The rule worth writing down

Aggregate in SQL; shape, iterate, and visualise in pandas. Moving 4,812 rows into Python to count them is slower, more fragile, and produces a number nobody else can reproduce from the database. Pull the smallest result set that answers the question, and let the database do the arithmetic it is built for.