ORDER BY in Window

SQL · Window Functions

Theory

ORDER BY inside OVER() sets the order in which rows are walked through within each partition. It is separate from the ORDER BY at the end of the query: the window ORDER BY decides how the calculation runs, the final ORDER BY decides how the result is displayed. They can be different.

This ordering is what makes ranking (ROW_NUMBER, RANK), offsets (LAG, LEAD) and running totals meaningful. It also quietly changes how aggregates behave. SUM(revenue) OVER () gives the grand total on every row, but SUM(revenue) OVER (ORDER BY month) gives a running total, because adding ORDER BY switches the default frame to 'from the first row up to the current row'. If the ORDER BY column has ties, tied rows are treated as one step in that default frame, so add a tie-breaker column when you need a strict row-by-row order.

Why we use it

Many business questions depend on sequence: month-on-month growth, rank by score, cumulative sales so far. Without an ordering inside the window, the database has no notion of 'previous', 'first' or 'so far'.

When to use it

Use ORDER BY in OVER for any ranking, LAG/LEAD, running total or moving average. Leave it out when you want a plain group total on every row. Always include a unique tie-breaker (like id) when the result must be deterministic.

How it works

  1. Rows are split into partitions
  2. Rows in each partition are sorted by the window ORDER BY
  3. Aggregate like SUM/AVG? Yes: Running frame: start to current. No: Rank/offset uses the order.
  4. Compute the value for each row in sorted order
  5. Final ORDER BY sorts the output for display
  6. Result returned

Syntax

function() OVER (ORDER BY column)

Example

SELECT name, salary,
  RANK() OVER (ORDER BY salary DESC) AS rnk
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