Jamal Hansen

@ham-jansen.bsky.social

Data scientist and team lead. I write about SQL, Python, and practical AI at jamalhansen.com. 5 kids, an MBA, and a blog publishing schedule that somehow hasn't fallen apart yet.

surround yourself with kind and constructive folks, enjoy the time your paths are shared, help each other, learn from others, give back, and build something together you're all proud of when you'll pass the torch

SQL subqueries = Python helper functions. Need an intermediate result? In Python you write a helper function and call it. In SQL you nest a query inside another query. This post covers the three types (scalar, list, table), correlated vs non-correlated. jamalhansen.com/blog/subquer...

Subqueries: When SQL Needs Helper Functions

Nest queries inside queries, like Python helper functions. Use them in WHERE, SELECT, or FROM to compute intermediate results.

jamalhansen.com

Don't pull rows into Python to count them. Counter(c['city'] for c in customers) works, but sends every record over the network. SQL's GROUP BY does it on the server: SELECT city, COUNT(*) FROM customers GROUP BY city Five groups = five rows. Read more: jamalhansen.com/blog/group-b...

GROUP BY: Aggregating Your Data

GROUP BY creates buckets and counts them. It's like Python's `collections.Counter` or pandas `groupby()`. Learn COUNT, SUM, AVG, MIN, and MAX.

jamalhansen.com

Run a SQL query twice. Get different row orders. Not a bug. Databases optimize execution differently each time. If order matters, you have to be explicit with ORDER BY SQL's advantage over Python here: mixed sort directions in one line. ORDER BY date DESC, name ASC jamalhansen.com/blog/order-b...

ORDER BY: Sorting Your Results

SQL returns rows in no guaranteed order. Run the same query twice and you might get different results. ORDER BY gives you control, like Python's sorted() with key functions.

jamalhansen.com

New here (well, new-ish). I'm Jamal. Data scientist, blogger, dad of 5, MBA student. Currently on paternity leave, which means I have one hand on a baby and the other on a keyboard at all times. Looking to connect with folks in the data/Python/SQL space.

SELECT * returns everything. You probably only need two columns. But SELECT does more than pick columns. It renames with AS, computes expressions, and transforms text. It maps directly to Python list comprehensions. DISTINCT can mask problems in your query. Why? jamalhansen.com/blog/select-...

SELECT: Choosing Your Columns

SQL's SELECT is more than picking columns. Rename with AS, compute expressions, and use DISTINCT for unique values.

jamalhansen.com

I'm writing a free weekly series: SQL for Python Developers. 25 posts, 6 published so far. Every concept gets a Python comparison. Every post uses DuckDB so you can follow along with just pip install. If you write Python and avoid SQL, this is for you. 🧵