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

GROUP BY aggregates summarize rows and return one row per group. Window functions calculate values across related rows but keep each query row, so you can see a detail row alongside its group average, rank, or running total. The practical choice is whether you want to replace detail with a summary or add a calculation to the detail.

At a glance: what changes in the result?

Question Aggregate with GROUP BY Window function with OVER
Output shape One row per group; detail rows are summarized. One result alongside each row at the query’s current grain.
Typical syntax AVG(salary) with GROUP BY department AVG(salary) OVER (PARTITION BY department)
Best for Summaries such as revenue by country or average salary by department. Rankings, running calculations, or group context displayed beside individual rows.
Filtering the result Use HAVING to filter groups after aggregation. Usually calculate in a subquery or CTE, then filter in an outer query.
Portability Check aggregate function support in your database. Check function, frame, and syntax support for your database and version.

PostgreSQL defines a window function as one that “performs a calculation across a set of table rows that are somehow related to the current row.” Its key distinction from an ordinary grouped aggregate is that the window result does not collapse those rows. See the PostgreSQL window functions documentation.

What does GROUP BY do?

GROUP BY changes the grain—the level of detail—of the query result. If you group employees by department and calculate an average salary, the result contains a department and its average, not a separate row for every employee.

SELECT department, AVG(salary) AS department_avg
FROM employees
GROUP BY department;

This is the right shape when the question is “What is the average salary in each department?” If you also select employee-level columns, the database generally requires them to be grouped or aggregated, because the output represents groups rather than individual employees.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

What does a window function do?

A window function computes across a set of rows related to the current row, then returns its result on that row. Add OVER to an aggregate such as AVG, and use PARTITION BY to define the groups used for the calculation:

SELECT department, employee_id, salary,
       AVG(salary) OVER (PARTITION BY department) AS department_avg
FROM employees;

This returns each employee’s department, ID, and salary, with that department’s average repeated beside each employee. The PostgreSQL documentation uses this same essential distinction: its department average window calculation preserves one output row for each employee.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

PARTITION BY versus GROUP BY

Both clauses can organize rows into groups, but they affect the result differently. GROUP BY produces a group-level result and reduces the number of rows. PARTITION BY divides the rows considered by a window calculation; it does not, by itself, combine the rows in the output.

  • Choose GROUP BY department when you want one result for each department.
  • Choose OVER (PARTITION BY department) when you want each detail row plus a calculation for its department.
  • Leave out PARTITION BY when the window should use all rows in the query result as one partition. MySQL documents that an empty OVER() uses all query rows and repeats the calculation for each row in its window function concepts and syntax reference.

How to read OVER, ORDER BY, and frames

OVER identifies a calculation as a window calculation. Inside it, PARTITION BY sets the related groups, while its ORDER BY defines order for calculations that depend on sequence. This window ordering is separate from the query’s final ORDER BY, which sorts the displayed result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

A frame can restrict an ordered window to a subset of its partition, which is useful for running or moving calculations. Frame syntax and default behavior depend on the database, so specify the intended frame deliberately and consult the relevant engine documentation rather than assuming identical behavior across SQL dialects. PostgreSQL and MySQL describe window syntax in their respective references: PostgreSQL and MySQL 8.4.

Which one should you use?

  • One summary per group: use an aggregate with GROUP BY, such as revenue by country.
  • Detail plus a group statistic: use an aggregate with OVER (PARTITION BY ...), such as every transaction alongside its department total.
  • Rank or row number within a group: use a ranking window function and define the ranking order inside OVER.
  • Running or moving total or average: use an aggregate window with OVER (ORDER BY ...), selecting a frame appropriate to the calculation.
  • Top rows within each group: use a ranking window calculation, then filter its result in an outer query.

Microsoft lists moving averages, cumulative aggregates, running totals, and top-N-per-group among uses of the OVER clause in SQL Server.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why can’t I filter a window result in WHERE?

Window calculations happen after the query’s WHERE, GROUP BY, and HAVING processing. They are allowed in the SELECT list and final ORDER BY, but not directly in WHERE, GROUP BY, or HAVING in PostgreSQL. To keep, for example, only the top-ranked employees in each department, calculate the rank first and filter it in an outer query:

WITH ranked_employees AS (
    SELECT department, employee_id, salary,
           ROW_NUMBER() OVER (
               PARTITION BY department
               ORDER BY salary DESC
           ) AS position
    FROM employees
)
SELECT department, employee_id, salary, position
FROM ranked_employees
WHERE position <= 3;

The ranking is available to the outer query as a regular column. PostgreSQL’s window-function documentation shows this general subquery approach, and MySQL 8.4 likewise places window processing after WHERE, GROUP BY, and HAVING.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Can you aggregate first and then use a window function?

Yes. Since window processing follows ordinary aggregation, a query can first form grouped results and then calculate a window value over those results. For example, you can summarize sales by country and then compare each country’s total with the overall total. The reverse nesting is not generally interchangeable: PostgreSQL documents that an ordinary aggregate call may be an argument to a window function, but a window function cannot be an argument to an ordinary aggregate call.

Does every aggregate work as a window function?

No. The general pattern is to use an aggregate with OVER, but support varies by database, function, and version. Microsoft’s SQL Server aggregate-function documentation lists STRING_AGG, GROUPING, and GROUPING_ID as exceptions to aggregate functions that may take OVER. Check the manual for your target engine before relying on a particular function or frame option; SQL Server’s aggregate functions reference and OVER clause reference document its rules.

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.