GROUP BY vs Window Functions in SQL

I used to be confused about the GROUP BY and Window Functions. The mental model that clicked for me: GROUP BY summarizes. Window functions annotate. Here's a distinction that changes how you write queries. ◉ GROUP BY collapses rows: You want a summary (e.g., sum, agg,...) per group. It gives you one row per group. The original rows disappear. ◉ Window functions: Every original row remains, but now it carries the aggregation/computation (e.g., sum, min, row_number,...) as well. -- If you've ever written a GROUP BY and then joined the result back to the original table... You probably needed a window function from the start. If you find this helpful, please: 𖤘 Save ↻ Repost #sql #dataengineering -- If you like this piece, you might love my newsletter, which includes 180+ articles to help you become a "production-ready" data engineer. Join 𝟭𝟴,𝟬𝟬𝟬+ DEs here for 𝗙𝗥𝗘𝗘: https://vutr.substack.com/

  • diagram, table

im going to write something on Substack, but I don't know which tool to use to create images like this?

Like
Reply

When learning window functions focus on how the window frames are formed too.

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories