S
Seoffix.com
Sign In Get Pro Membership
SQL Intermediate
Estimated Duration: 20 min

SQL: Advanced Window Functions for Domain Rank Slicing

Partition and rank millions of domains across top-level domains using ROW_NUMBER() and DENSE_RANK().

SQL Window Functions: ROW_NUMBER() and PARTITION BY


Traditional GROUP BY aggregates multiple rows into a single summary line. In contrast, **Window Functions** compute values across a set of rows while retaining each individual row's identity.

In SEO competitive research, you often need the **Top 3 domains for each TLD** (e.g. .com, .in, .org) without executing separate queries for every suffix.

In this lesson, you will master ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...) to partition ranking datasets.
Target Output
domain | tld | global_rank
google.com | com | 1
youtube.com | com | 2
wikipedia.org | org | 5
archive.org | org | 12
Need a Hint?
  • Use ROW_NUMBER() OVER (PARTITION BY tld ORDER BY global_rank ASC)
  • Alias the resulting window column as rank_in_tld
  • Filter the outer query where rank_in_tld <= 2
solution.js
Terminal Output Ready
Click "Run Code & Test" to execute your solution.