Learn SQL
-- windows.percent_total_qualify

Percent-of-Total & QUALIFY in SQL

Empty OVER () plus the portable filter-outside

--concept

Divide row value by grand-total window SUM() OVER () — decimal-cast first (100.0, not 100). Partitioned variants give group shares with row detail GROUP BY cannot. Windows evaluate in SELECT after WHERE — filtering needs outer query/CTE (portable) or QUALIFY (Snowflake/BigQuery) which filters post-window inline.

--how it works
1

Grand share

salary * 100.0 / SUM(salary) OVER () — decimal first, whole result as one window.

2

Group share twin

OVER (PARTITION BY dept) — per-group shares with row detail intact.

3

Filter outside

Windows postdate WHERE — wrap in subquery/CTE, filter outside (portable).

4

QUALIFY shortcut

Snowflake/BigQuery filter post-window inline — same semantics, less nesting.

--visualization
salary*100.0/SUM()OVER() — empty OVER()=grand total; filter outside (or QUALIFY)
namepct
Cynthia22.41
Alice20.94
Stage 1 / 1
100.0 carries decimal (100 truncates). QUALIFY collapses nesting where supported.
--example
query.sql
1SELECT name, salary, salary * 100.0 / SUM(salary) OVER () AS pct FROM employees ORDER BY salary DESC LIMIT 2;
Result
namesalarypct
Cynthia9100022.41
Alice8500020.94

Empty OVER () treats the whole result as one window — grand total 406k. Each row divides by it with decimal math. GROUP BY could total but never keep row detail beside it.

--variants

Pay share of payroll

Row detail GROUP BY cannot give.

query.sql
1SELECT name, salary, salary * 100.0 / SUM(salary) OVER () AS pct FROM employees ORDER BY salary DESC LIMIT 2;
Result
namesalarypct
Cynthia9100022.41
Alice8500020.94
1Empty OVER () sums all 406k once.
2Each row divides with 100.0 decimal.
3100 (int) would truncate shares toward zero.

Top-3 per dept (portable vs QUALIFY)

Same filter, two spellings.

query.sql
1SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees) t WHERE rn <= 3;
2-- QUALIFY twin (Snowflake/BQ):
3-- SELECT *, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees QUALIFY rn <= 3;
Result
departmentkept
Engineering3
Sales2
Marketing1
1Compute ranks inside, filter outside — execution order honored.
2QUALIFY collapses the nesting where supported.
3Default to portable in shared codebases.
--key facts
OVER ()whole result as one window
QUALIFYpost-window filter where supported
100.0decimal carrier — 100 truncates
--use cases
Share/percent reports: payroll, revenue mix
Top-N per group in portable SQL
--under the hood
Grand-total window is one pass; GROUP BY totals + self-join would rescan.
QUALIFY is post-window predicate pushdown — version-check before relying.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.