COUNT() OVER

SQL · Window Functions

Theory

COUNT() OVER (...) counts rows in the window and writes the count on each row. COUNT(*) OVER (PARTITION BY dept) gives every employee their department's headcount, and COUNT(*) OVER () gives the total number of rows in the result. The same NULL rule as the aggregate applies: COUNT(column) OVER skips rows where that column is NULL, while COUNT(*) counts them.

This is useful for adding context to detail rows, such as 'Asha works in a team of 3', or for filtering groups by size while keeping the rows: wrap the query and keep rows where dept_count >= 2. With ORDER BY inside OVER, COUNT becomes a running count (how many rows so far). Some databases support COUNT(DISTINCT col) OVER, but many, including SQL Server, Postgres and SQLite, do not, so plan to use DENSE_RANK tricks or a subquery for that.

Why we use it

Group sizes are useful context next to detail rows: team size, number of orders per customer, how many rows share a key (for finding duplicates). COUNT() OVER provides that without a GROUP BY that would hide the detail.

When to use it

Use COUNT() OVER to show group size on every row or to find duplicates (count > 1) while keeping the rows. Use GROUP BY COUNT when you only want one count per group.

How it works

  1. Rows split by PARTITION BY
  2. Is it COUNT(*)? Yes: Count every row in the frame. No: Count only non-NULL values.
  3. ORDER BY inside OVER? Yes: Running count so far. No: Count of whole partition.
  4. Write the count on each row
  5. Detail rows kept, count added

Syntax

COUNT(*) OVER (PARTITION BY col)

Example

SELECT name, department,
  COUNT(*) OVER (PARTITION BY department) AS dept_headcount
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic