This website uses cookies

Read our Privacy policy and Terms of use for more information.

When I first learned SQL, I kept trying to write it like a for loop. "First do this, then do that, then return the result."

It didn't work. SQL isn't procedural. It's declarative.

Here's the shift: instead of describing steps, you describe the result you want.

Python for loop:

results = []
for customer in customers:
    if customer['is_premium']:
        results.append(customer['name'])

Python list comprehension (getting closer):

[c['name'] for c in customers if c['is_premium']]

SQL:

SELECT name FROM customers WHERE is_premium = true

Same logic. Same structure. If you can read a list comprehension, you can read SQL.

This week's post explores this mental model with a spreadsheet analogy and explains why SQL works this way.

Next week: we start writing real queries with the FROM clause.

-Jamal

Keep Reading