Ravindra BagaleCourses & study guides मराठी Track your progress

24. How to Load SQL Query Results into a pandas DataFrame

24.5 Filter in SQL vs Filter in pandas

Why. You can WHERE city = 'Pune' in SQL, or load more rows and df.loc[df["city"] == "Pune"] in pandas. Wrong choice either floods memory or forces ten round-trips.

What.

  • Filter in SQL when the database can drop rows early (city, date, status) and the table is large.
  • Filter in pandas for flexible exploration, multiple what-if cuts, and logic that is awkward in SQL for you today.
  • Aggregate in SQL (GROUP BY) when you only need a summary and the raw grain is huge.
  • Aggregate in pandas when you will re-slice the same extract many ways (Chapter 7 groupby).

How.

Choose a lane

  1. Write the business question in one sentence (“Delivered GMV by city for 23 Oct 2026”).
  2. Push stable filters into SQL: date, status, city list.
  3. SELECT only columns you need (skip unused text blobs).
  4. Pull into pandas.
  5. Do exploratory cuts, merges, and charts in pandas.
  6. If the extract is still huge, tighten the SQL — do not keep calling read_sql_table.

Worked example A — Same answer, two places.

import sqlite3
import pandas as pd

con = sqlite3.connect("data/blinkit_lab.db")

sql_filtered = """
SELECT city, amount_inr
FROM orders
WHERE status = 'Delivered' AND city IN ('Pune', 'Nashik')
"""
df_sql = pd.read_sql_query(sql_filtered, con)

df_all = pd.read_sql_query(
    "SELECT city, status, amount_inr FROM orders",
    con,
)
con.close()

df_pd = df_all.loc[
    (df_all["status"] == "Delivered")
    & (df_all["city"].isin(["Pune", "Nashik"])),
    ["city", "amount_inr"],
]

print("SQL filter GMV", df_sql["amount_inr"].sum())
print("pandas filter GMV", df_pd["amount_inr"].sum())

Worked example B — Heavy table habit.

Zoya’s orders table has millions of rows. She never read_sql_table("orders"). She asks SQL for one festival week and six Maharashtra cities, then plots in seaborn.

Worked example C — Decision card.

Situation Prefer
8-row lab DB Either SQL or pandas filter
Millions of rows, one city SQL WHERE
Same 50k extract, ten chart ideas Pull once; filter in pandas
Only city totals needed SQL GROUP BY then small DataFrame

Ravindra Bagale's Tip

Mothe table = SQL madhe first cut. Chhote extract = pandas madhe khel. Donhi shi shikha.