SQL for computing h-score h-index
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The h-index of an author is the largest number h such that the author has at least h papers with at least h citations each. SQL is a good fit for this because the calculation is basically a ranking problem over each author's papers.
The Data Model
Assume a table like this:
Sample data:
For AuthorID = 1, the sorted citation counts are 10, 8, 5, 4, 3. That author has four papers with at least four citations, but not five papers with at least five citations, so the h-index is 4.
The Core SQL Idea
The standard pattern is:
- rank each paper within an author by citation count descending
- keep rows where
citations >= rank - take the maximum such rank per author
Window functions make this simple.
Query for All Authors
This works because paper_rank represents "the nth most cited paper." If the nth most cited paper still has at least n citations, then n is a valid h-index candidate.
Query for One Author
If you only need a single author, add a filter:
This is easier to read when debugging one author's result by hand.
Why ROW_NUMBER Works Better Than RANK
People often wonder whether RANK() or DENSE_RANK() is more appropriate because citations can tie. For h-index computation, ROW_NUMBER() is usually the cleanest tool because the definition depends on position in the sorted list of papers, not on grouping equal citation counts together.
For example, if an author has citation counts:
- 9
- 9
- 2
The paper positions are still first, second, and third. What matters is whether the third paper has at least 3 citations. It does not, so the h-index is 2.
Handling Authors with No Qualifying Papers
One subtlety: if you want every author in the result, including those with h-index 0, then you need a table of authors and a left join. Otherwise authors with no qualifying rows disappear from the grouped result.
Example:
That version is more complete for reporting systems.
Performance Notes
For typical bibliometric datasets, the window-function solution is efficient and readable. Helpful indexes include:
- '
(AuthorID, Citations DESC)' - or at least
(AuthorID, Citations)
The database still has to order papers within each author, but indexing can reduce the cost significantly.
If the dataset is huge and recalculated often, materializing sorted citation summaries or precomputed author metrics may be worth it.
Common Pitfalls
Using COUNT(*) alone is not enough because h-index depends on the relationship between citation count and sorted paper position.
Using RANK() with ties can produce unintuitive results because h-index cares about paper positions, not just distinct citation levels.
Forgetting COALESCE can leave authors with no qualifying papers out of the result or return NULL instead of 0.
Failing to specify a stable secondary sort such as PaperID can make tied citation rows appear in arbitrary order across runs.
Summary
- The h-index is the maximum rank where citations are still at least as large as the rank.
- In SQL, compute it by sorting papers per author with
ROW_NUMBER(). - Filter rows where
Citations >= paper_rank, then take the maximum rank. - Use a left join and
COALESCEif you need authors with h-index 0 included. - Window functions make the query compact, readable, and efficient for this problem.
Related reading
- SQL select only rows with max value on a column
- start index at 1 for Pandas DataFrame
- Statistical approach to chess?
- Stratified splitting of pandas dataframe into training, validation and test set
- SQL Group By with an Order By
- SQL How to perform string does not equal
- String analysis
- String Distance Matrix in Python

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.