SQL

Window vs GROUP BY

Introduction to Window Functions

Window functions perform calculations across rows while keeping all rows in the result.

The Key Difference

GROUP BY collapses rows:

SELECT category, AVG(price) FROM products GROUP BY category;
-- Returns one row per category

Window functions keep all rows:

SELECT name, price, AVG(price) OVER() as overall_avg FROM products;
-- Returns ALL rows with the average added

Basic Syntax

function_name() OVER (window_specification)

Your Task

Select all products showing:

  • name, category, price
  • The overall average price across ALL products (as avg_price)

Use AVG(price) OVER() for the window function.

query.sql
Loading SQL engine...
Downloading SQL engine… (one-time)

Downloading SQL engine… (one-time)

This runs entirely in your browser and is cached for next time.