Python and SQL
Some tasks mix the two languages: SQL fetches the rows, Python does the logic that is awkward in SQL. The interviewer then watches three things. How do you pass a value into the query? Which part of the work runs where? What happens when the result is large? An f-string in a query or a SELECT * filtered in a Python loop is noticed at once.
The db module on this site
Section titled “The db module on this site”Python exercises on this site reach the same PostgreSQL as the SQL pages through a small db module. Its interface is close to the Python database standard (DB-API) and to DuckDB’s client: connect(), execute(sql, parameters), fetchall(), fetchone(), fetchmany(n).
import db
con = db.connect()rows = con.execute( "SELECT channel, period_month, amount_usd FROM advances WHERE merchant_id = ? ORDER BY period_month, channel", ["mer_01"],).fetchall()print(len(rows))print(rows[:2])print(sum(amount for _, _, amount in rows))6[('north_market', datetime.date(2026, 6, 1), Decimal('68.38')), ('south_market', datetime.date(2026, 6, 1), Decimal('20.80'))]359.72- Each row is a tuple.
NUMERICarrives asDecimal,DATEasdate,TIMESTAMPasdatetime, so money stays exact. ?marks a parameter. The module sends the values to PostgreSQL separately, as$1,$2, and never pastes them into the SQL text.- Each
executeruns in its own transaction that is rolled back, so anINSERTin an exercise does not change the data for the next run.
Parameters, never f-strings
Section titled “Parameters, never f-strings”An f-string pastes the user’s text into the SQL, and the text becomes code. A runnable example on sqlite3, which comes with Python:
import sqlite3
con = sqlite3.connect(":memory:")con.execute("CREATE TABLE products (id TEXT, merchant_id TEXT)")con.executemany("INSERT INTO products VALUES (?, ?)", [("prd_01", "mer_01"), ("prd_02", "mer_02"), ("prd_03", "mer_02")])
merchant = "mer_01' OR '1'='1" # what a user typed
unsafe = f"SELECT id FROM products WHERE merchant_id = '{merchant}'"print(con.execute(unsafe).fetchall()) # every merchant's products
safe = "SELECT id FROM products WHERE merchant_id = ?"print(con.execute(safe, [merchant]).fetchall()) # the value is data, not SQLprint(con.execute(safe, ["mer_01"]).fetchall())[('prd_01',), ('prd_02',), ('prd_03',)][][('prd_01',)]The f-string version returned every merchant’s products. With a parameter, the whole string is one value, and no merchant has that id. Parameters give three things:
- No SQL injection. The value is never read as SQL: the driver sends it apart from the query or quotes it safely.
- Types and quoting. The driver handles
Decimal,date,Noneand a name likeO'Brien. - Plan reuse, with some drivers. The query text stays the same for every value, so a driver can prepare it once: asyncpg does, psycopg 3 only after the same query has run a few times (
prepare_threshold, 5 by default), and psycopg2 puts the values into the text on the client and prepares nothing.
The placeholder depends on the library. PEP 249 calls this the paramstyle:
| Library | Placeholder | Call |
|---|---|---|
this site’s db, DuckDB, sqlite3 | ? | con.execute(sql, [a, b]) |
| psycopg (PostgreSQL) | %s or %(name)s | cur.execute(sql, (a, b)) |
SQLAlchemy text() | :name | conn.execute(sa.text(sql), {"name": a}) |
| asyncpg | $1, $2 | await con.fetch(sql, a, b) |
Some interview editors connect Python to a PostgreSQL database through SQLAlchemy. The same query there (run here on SQLite with two products):
with engine.connect() as conn: rows = conn.execute( sa.text("SELECT id, name FROM products WHERE merchant_id = :merchant_id"), {"merchant_id": "mer_01"}, ).mappings().all() # a list of dict-like rowsprint(rows)print(rows[0]["name"])[{'id': 'prd_01', 'name': 'Product 01'}]Product 01Two cases need care:
-
A list of values for
IN. Generate one placeholder per value. The values still go as parameters:merchants = ["mer_01", "mer_02"]marks = ", ".join("?" * len(merchants)) # "?, ?"sql = f"SELECT id FROM products WHERE merchant_id IN ({marks}) ORDER BY id"print(sql)print(con.execute(sql, merchants).fetchall())SELECT id FROM products WHERE merchant_id IN (?, ?) ORDER BY id[('prd_01',), ('prd_02',), ('prd_03',)] -
Names of tables and columns cannot be parameters.
ORDER BY ? DESCwith["id"]sorts by the constant string'id', not by the column: it returnsprd_01, prd_02, prd_03in insertion order instead of starting withprd_03. Check the name against a fixed set (if column not in {"id", "merchant_id"}: raise ValueError(...)) and only then put it into the text.
How do you pass a user value into SQL from Python?
Say your answer out loud first, then open it.
Show the answer
“Only as a parameter: cur.execute('… WHERE product_id = %s', (product_id,)) or :product_id with SQLAlchemy. Never an f-string — that is SQL injection, and parameters also handle types and quoting.”
If asked about column names, add: identifiers cannot be parameters, so check them against a list of allowed names.
Where the work belongs
Section titled “Where the work belongs”The database filters, joins and aggregates next to the data, with indexes. Python should receive a small result. September net revenue by channel for one merchant:
from datetime import datetime
SQL = """SELECT t.channel, COUNT(*) AS n, SUM(t.revenue_usd) AS net_usdFROM transactions tJOIN products p ON p.id = t.product_idWHERE p.merchant_id = ? AND NOT t.is_test AND t.start_time >= ? AND t.start_time < ?GROUP BY t.channelORDER BY t.channel"""rows = con.execute(SQL, ["mer_01", datetime(2026, 9, 1), datetime(2026, 10, 1)]).fetchall()for channel, n, net in rows: print(channel, n, net)north_market 8 114.93south_market 9 88.92web 4 60.96Three rows travel to Python. The alternative, SELECT * FROM transactions and a loop with if, moves all 8,294 rows over the network and builds 8,294 tuples in memory to keep 21 of them. On a table with millions of rows, the cost grows with every row.
What stays in Python: logic that is awkward in SQL, like a loop that allocates a payment over debts or finds the longest streak of days, and anything that calls another service per row. Even then, let SQL do the filtering first. The SQL + Python tasks practise exactly this split.
Should you filter and group in Python or in SQL?
Say your answer out loud first, then open it.
Show the answer
“In SQL: the database filters and aggregates close to the data and uses indexes, and I fetch a small result. In Python I keep the logic that is hard to express in SQL, like an allocation loop. Fetching all rows and filtering in Python is slow and wastes memory.”
A good addition: “For a big table I’d check that an index serves the filter, with EXPLAIN.” See Indexes and EXPLAIN.
Fetching in batches
Section titled “Fetching in batches”fetchall() builds the whole result as a list. When Python needs every row (for example, to send each one to another service), read in batches:
from decimal import Decimal
cur = con.execute("SELECT revenue_usd FROM transactions WHERE channel = ? AND NOT is_test", ["north_market"])total, batches = Decimal("0"), 0while rows := cur.fetchmany(500): batches += 1 for (price,) in rows: total += priceprint(batches, total)9 56826.77That is 4,442 rows in 9 batches, and the total matches SUM(revenue_usd) in SQL. A real sum belongs in SQL, of course. The loop only shows the pattern.
Whether fetchmany saves memory depends on the driver. sqlite3 reads rows as you fetch them. psycopg’s default cursor receives the whole result during execute, so you need a named (server-side) cursor to stream. The module on this site also holds the result in memory. The same idea works for writing: insert in batches of a few hundred rows with executemany (see Iteration and generators).
Transactions from code
Section titled “Transactions from code”Writes that must succeed or fail together go into one transaction. An advance and its ledger entry are the usual example:
import sqlite3
con = sqlite3.connect(":memory:")con.execute("CREATE TABLE advances (advance_id TEXT PRIMARY KEY, amount_usd TEXT)")con.execute("CREATE TABLE ledger_entries (entry_id TEXT PRIMARY KEY, advance_id TEXT, amount_usd TEXT)")
def record_advance(con, advance_id, amount, entry_id): with con: # commit at the end, rollback on an exception con.execute("INSERT INTO advances VALUES (?, ?)", [advance_id, amount]) con.execute("INSERT INTO ledger_entries VALUES (?, ?, ?)", [entry_id, advance_id, amount])
record_advance(con, "adv_1", "100.00", "le_1")try: record_advance(con, "adv_2", "50.00", "le_1") # le_1 already existsexcept sqlite3.IntegrityError as e: print("rolled back:", e)
print(con.execute("SELECT advance_id FROM advances").fetchall())print(con.execute("SELECT entry_id, advance_id FROM ledger_entries").fetchall())rolled back: UNIQUE constraint failed: ledger_entries.entry_id[('adv_1',)][('le_1', 'adv_1')]The second ledger entry failed, so the second advance was rolled back too. There is no advance without its entry. The same pattern in other libraries: with conn.transaction(): in psycopg 3, with engine.begin() as conn: in SQLAlchemy.
Two rules:
- No calls to other services inside a transaction. The transaction holds locks and a connection while it waits for the bank. If it rolls back after a timeout, the payment may already be sent.
- The database guards uniqueness. A check in Python (“is there a payout with this key?”) races with a second worker. A unique key does not. See Duplicates and idempotency.
Threads, processes and the GIL
Section titled “Threads, processes and the GIL”A thread is useful while the program waits. During a network call or a database query, the thread releases the GIL (global interpreter lock), and other threads run:
import timefrom concurrent.futures import ThreadPoolExecutor
def fetch_product_report(product_id): time.sleep(0.2) # stands for a network call: the thread waits return product_id, "ok"
products = ["prd_01", "prd_02", "prd_03", "prd_04", "prd_05"]
t0 = time.perf_counter()results = [fetch_product_report(p) for p in products]print(f"one by one: {time.perf_counter() - t0:.1f} s")
t0 = time.perf_counter()with ThreadPoolExecutor(max_workers=5) as pool: results = list(pool.map(fetch_product_report, products))print(f"5 threads: {time.perf_counter() - t0:.1f} s")print(results[:2])one by one: 1.0 s5 threads: 0.2 s[('prd_01', 'ok'), ('prd_02', 'ok')]For CPU-heavy Python code, threads do not help: in the standard CPython build, only one thread runs Python code at a time. In one run, four calls that each sum 3 million squares took 0.6 s one by one and 0.6 s in four threads. For that kind of work use processes (ProcessPoolExecutor), or move the work to SQL. Python 3.13 added an optional free-threaded build without the GIL, but the usual build still has it.
Threads bring a second problem: shared state. count += 1 or “check the deque, then append” are several steps, and two threads can interleave between them. Protect such code with a lock:
import threading
lock = threading.Lock()
with lock: # only one thread at a time inside this block ... # read, change, writeAlso, give each thread its own connection or take one from a pool. A sqlite3 connection refuses use from another thread by default. With psycopg, use a connection pool, so threads do not queue on one connection.
Your cache (or rate limiter) is called from many threads. Is it safe?
Say your answer out loud first, then open it.
Show the answer
“Not as written: read, change and write are separate steps, so two threads can interleave. I would protect them with a threading.Lock. If there are several processes or servers, the state has to live outside — Redis with an atomic operation, or a unique constraint in the database.”
The lock goes around the whole read-change-write, not around each line. See Time windows for the limiter it usually refers to.
Practice
Section titled “Practice”This page has no drills. Run the query below, then change it: count the advances per channel instead of listing them, pass the month as a second parameter, and try to break it with an f-string to see the difference. The full tasks are on SQL + Python.
import db con = db.connect() rows = con.execute( "SELECT channel, period_month, amount_usd FROM advances WHERE merchant_id = ? ORDER BY period_month, channel", ["mer_01"], ).fetchall() for channel, month, amount in rows: print(channel, month, amount)
Further reading
Section titled “Further reading”- PEP 249: paramstyle — the five placeholder styles of the Python database standard.
- sqlite3: How to use placeholders —
?and named parameters, and why not string formatting. - sqlite3: Transaction control — commit, rollback and
with con:. - ThreadPoolExecutor — running I/O-bound calls in threads.
- Global interpreter lock — the glossary entry: what the GIL is and when it is released.
Related topics
Section titled “Related topics”- Filters and dates — the half-open date range that the parameters fill.
- SQL + Python — full tasks where SQL returns days and Python finds the streak.
- Database concepts — transactions, isolation levels and row locks in more depth.
- Indexes and EXPLAIN — making the filter that stays in SQL fast.
- Iteration and generators — batches and streams on the Python side.
Site content: CC BY 4.0. Code: MIT. The exercises run in your browser: PostgreSQL by PGlite, Python by Pyodide.