DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

Window Functions vs. Aggregate Functions in SQL (With Examples)

Aggregates with GROUP BY summarize rows; window functions with OVER add calculations while preserving detail rows. See SQL examples and learn how partitions, ordering, frames, and NULLs affect results.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use an aggregate with GROUP BY when you want a summary row for each group. Use a window function with OVER when you want a calculation—such as a group total, rank, or running total—while keeping the underlying rows in the result. The difference is the result shape: GROUP BY summarizes; a window calculation annotates rows.

What is the difference between aggregate and window functions?

Common aggregate functions include SUM, AVG, COUNT, MIN, and MAX. They calculate a value over a set of values. With GROUP BY, the query divides input rows into groups and returns a summary for each group. Microsoft describes aggregate functions as calculating a single value from a set of values in its SQL Server aggregate function reference.

A window expression also calculates across related rows, but it returns a calculated value for each row in the window instead of collapsing those rows into one result. Microsoft’s SQL Server documentation for the OVER clause puts it this way: “A window function then computes a value for each row in the window.”

Dimension Aggregate with GROUP BY Window calculation with OVER
Result rows Typically one result row per group Retains the qualifying detail rows
Useful question What is the total, average, or count for each group? What is this row’s value relative to its group or ordered neighbors?
Typical examples Payroll by department; sales by month; count by status Department total beside each employee; running total; moving average
Main SQL construct GROUP BY, sometimes with HAVING OVER, optionally with PARTITION BY, ORDER BY, and a frame

When should you use GROUP BY?

Use GROUP BY when the report needs a summary rather than the individual records that produced it. For example, this query returns one row per department:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
SELECT department_id,
       SUM(salary) AS department_payroll,
       AVG(salary) AS average_salary,
       COUNT(*) AS employee_count
FROM employees
GROUP BY department_id;

The values in department_id define the groups; each aggregate calculates a summary within its department. Microsoft’s PostgreSQL-oriented training also covers aggregate functions with GROUP BY and HAVING in its aggregate functions and grouping module.

A grouped query generally cannot return an arbitrary employee-level column as if it were part of the summary. In a grouped result, select group columns and aggregate expressions, subject to the rules of the database engine and query.

When should you use a window function?

Use a window function when each detail row must remain visible but should also carry a calculation based on related rows. In SQL Server, the OVER clause defines the window for a function. PARTITION BY divides rows into independent calculation groups without grouping the query result; if omitted, the window can cover the whole result set.

Keep each employee and show the department total

SELECT employee_id,
       department_id,
       salary,
       SUM(salary) OVER (PARTITION BY department_id) AS department_payroll
FROM employees;

Each employee remains a separate result row, and the department payroll appears alongside that employee. Microsoft’s SQL Server examples use the same general approach to display order-level aggregates beside order-detail rows.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Calculate a running total

SELECT account_id,
       transaction_date,
       transaction_id,
       amount,
       SUM(amount) OVER (
           PARTITION BY account_id
           ORDER BY transaction_date, transaction_id
           ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
       ) AS running_balance_change
FROM transactions;

PARTITION BY account_id starts a separate calculation for each account. The ordering puts transactions in sequence; transaction_id breaks ties when dates repeat. The explicit ROWS frame includes every ordered row from the account’s first row through the current row. This calculates a cumulative change, not necessarily an account balance: an opening balance must be included, or the amounts must already represent the appropriate balance changes.

What do PARTITION BY, ORDER BY, and frames control?

  • PARTITION BY: Creates separate calculation windows, such as one window per department or account. It does not reduce the result to one row per partition.
  • ORDER BY inside OVER: Establishes logical order for calculations that depend on sequence, such as running totals. Include a tie-breaker if the ordering column can repeat and the intended row sequence matters.
  • ROWS or RANGE frame: Limits which ordered rows participate in a calculation. Choose a frame that matches the intended calculation; do not assume defaults produce the desired cumulative or moving result.

In SQL Server, frame syntax requires ordering. Exact defaults, supported functions, frame behavior, and syntax vary across database engines and versions, so check the documentation for the engine you use. The examples here follow SQL Server’s documented OVER syntax; the concepts are useful more broadly, but the syntax and behavior should be verified before treating a query as portable.

How do NULL values affect aggregate counts?

SQL Server’s aggregate-function documentation says aggregates ignore NULL values, with COUNT(*) as the exception. Use COUNT(*) to count rows; use COUNT(column_name) to count only rows where that column is non-NULL. That difference matters when missing values are present.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Can you combine grouped aggregates and window functions?

Yes. They answer different questions and can be used in the same query when the desired result first needs a grouped summary and then a calculation across those summaries. Keep the intended grain clear: grouping changes which rows the query returns, while a window calculation operates across rows in its query result. Check your database’s rules for combining grouped expressions and window functions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.