SShortSingh.
Back to feed

SQL Window Functions Explained: PARTITION BY, RANK, LAG and More

0
·15 views

SQL window functions allow calculations across a set of rows while preserving each individual row in the result, unlike GROUP BY which collapses records into groups. Functions such as ROW_NUMBER() and RANK() enable row ranking within a dataset, with RANK() assigning equal ranks to tied values while ROW_NUMBER() always produces unique sequential numbers. PARTITION BY divides data into subgroups before a calculation is applied, making it possible to compute metrics like department-level averages alongside employee-level detail. LAG() and LEAD() retrieve values from preceding or following rows respectively, enabling row-to-row comparisons such as tracking changes in daily sales figures. Together, these functions extend SQL's analytical capability beyond simple aggregation, making them valuable for tasks like running totals, trend analysis, and ranked reporting.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingHacker News ·

Memorilla Proposes Latent Semantic Memory to Help LLMs Retain Context

A new arXiv paper introduces Memorilla, a system that proposes latent semantic memory for large language models. The approach aims to give LLMs a way to store and retrieve information within their latent representations. The source material is limited to a link and a Hacker News discussion thread with few comments, so detailed technical results are not available here. Readers interested in the method's design and evaluation should consult the full paper directly.

0
ProgrammingHacker News ·

Size-It-Up: A Browser Game Surfaces on Hacker News

A new game titled Size-It-Up has been shared on Hacker News, linking to its page at magnitudle.com. The post received 6 points and no comments at the time it was captured. The source material provides only the title, link, and engagement figures, with no description of gameplay, developers, or release details. Further information about the game's mechanics or purpose is not available from the item itself.

0
ProgrammingHacker News ·

Perplexity Unveils AI Agents Designed for Complex, Ongoing Work

Perplexity has announced a new set of AI agents aimed at handling workplace tasks. The company describes them as teammates built for complex, ongoing work rather than one-off queries. The announcement was shared through a blog post on Perplexity's website. The source article provided limited detail beyond the headline and its title, so specifics on capabilities, pricing, and availability are not confirmed here. The story gained modest traction on Hacker News, where it drew a few points and a single comment.

0
ProgrammingDEV Community ·

Study finds many Lovable-built apps expose Supabase data on first page load

Researchers at Crissy Field examined 159,041 Lovable projects, of which 34,545 used Supabase. Of 24,664 sites examined more closely, many were already serving database records to visitors without any login. Further read-only requests with the public key surfaced email addresses in thousands of sites, along with more sensitive items such as passport details and chat messages. The authors caution that data appearing on first load is not automatically a confirmed leak, and the roughly 30% figure applies only to the sampled sites, not all Lovable apps. Security advisers stress that anon keys are meant to be public, so the real risk lies in missing or permissive row-level security rules.