This month's discount Comprehensive Database Performance Health Check · Testimonials
SQLAuthority with Pinal Dave
Home
  • Consulting
    • Health Check
  • Free Videos
  • All Articles
    • AI
    • SQL Performance
    • SQL Tips and Tricks
    • Interview Questions and Answers
    • SQL Puzzle
    • SQL Video
    • SQLAuthority News
    • Personal
  • Books
  • Consulting
SQL Server Top Scoring High Scores.

How to Get Top N Records Per Group? – Ranking Function – Interview Question of the Week #156

January 14, 2018
Pinal Dave
SQL Interview Questions and Answers
Ranking Functions, SQL Function, SQL Scripts, SQL Server

I gave 40 candidates this puzzle: get the top N records per group from a student marks table. Here is the simple answer using a Ranking Function.

Read More
How to find median in sql server.

How to Find Median in SQL Server? – Interview Question of the Week #116

April 2, 2017
Pinal Dave
SQL Interview Questions and Answers
Ranking Functions, SQL Scripts, SQL Server

There is no MEDIAN function in T-SQL. Here is an easy way to find median values in SQL Server 2012 and later, plus what median really means.

Read More
A hand picking the three ripest red strawberries from each plant along a garden row

Top N per Group: Three Ways and Which Is Fastest

January 29, 2015
Pinal Dave
SQL Performance
Execution Plan, Ranking Functions, SQL Performance, SQL Server, SQL Top

Compare top N per group with ROW_NUMBER, APPLY and a correlated subquery, define tie behavior, and measure plans with a supporting index.

Read More
An old wooden comb on a bathroom shelf with gaps where some of its teeth have broken off.

Gaps and Islands: Finding Missing Ranges in a Sequence

January 27, 2015
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL DateTime, SQL Scripts, SQL Server

Solve gaps and islands with LEAD, ROW_NUMBER, and GENERATE_SERIES, then apply the same date patterns to missing days and login streaks.

Read More
A swallow returning to last year's mud nest under the eaves of an old stone barn.

First and Repeat Purchases: Tagging Customer Orders With ROW_NUMBER

August 10, 2014
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL Function, SQL Scripts, SQL Server

Tag repeat purchases with deterministic order numbering, calculate days to the second order, and compare customer cohorts fairly.

Read More
A wooden xylophone with bars stepping from long to short, a red mallet resting between two neighbouring bars.

LAG and LEAD: Comparing Each Row With the One Before It

April 2, 2014
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL Function, SQL Order By, SQL Server

Use LAG and LEAD for monthly comparisons, handle defaults and percent change, choose explicit frames, and detect status transitions.

Read More
MySQL - Reset Row Number for Each Group - Partition By Row Number

MySQL – Reset Row Number for Each Group – Partition By Row Number

March 9, 2014
Pinal Dave
SQL Tips and Tricks
MySQL, Ranking Functions

MySQL has no ROW_NUMBER() OVER (PARTITION BY), so here is how to reset row number for each group with a variable, using a simple example.

Read More
Previous 1 2 3 4 Next
  • Books
  • Testimonials
  • Privacy Policy

© 2006 – 2026 All rights reserved. pinal @ SQLAuthority.com