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
A colorful paper chain along a kitchen window with one gap in the loops, scissors on the sill

A Daily Streak in SQL: Finding the Longest Run of Consecutive Days

October 29, 2019
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL DateTime, SQL Scripts, SQL Server

Find consecutive days with gaps and islands, rank the longest streak per user, and define a current streak using the right business date.

Read More
Gouache painting of five matching tea cups in a row with the second from the left in vermilion

Get the Second Row From a Table in SQL Server

October 28, 2019
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL Function, SQL Scripts, Temp Table

To get the second row from a table, first decide what second means, because a table has no order until you give it one. This post builds a small demo and compares ROW_NUMBER, OFFSET and FETCH, and TOP inside TOP. It also covers every second row, the second row in each group, ties and the cost of each method.

Read More
The first group of towels is set aside while the remaining sequence stays on its shelf

How to Skip Top N Rows in SQL Server? – Interview Question of the Week #237

August 11, 2019
Pinal Dave
SQL Interview Questions and Answers
Ranking Functions, SQL Function, SQL Paging, SQL Scripts, SQL Server

Question: How to skip top N Rows in SQL Server and retrieve the rest of the rows? Answer: I had asked the above question in one of the interviews last week.

Read More
Pumpkins in pairs on a farm stand, the smaller of each pair tied with a red ribbon, one unpaired pumpkin at the end.

Every Second Item Free: Pricing Promotions With ROW_NUMBER

May 27, 2019
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL Function, SQL Scripts, SQL Server

Price every second item as free with ROW_NUMBER, expand quantities into units, and calculate basket discounts without double counting.

Read More
Over and partion by query results.

SQL SERVER – Adding Values WITH OVER and PARTITION BY

June 16, 2018
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL Function, SQL Order By, SQL Scripts, SQL Server

How do you sum values for each group without GROUP BY and still keep every row? Here is how SUM with OVER and PARTITION BY answers a reader’s question.

Read More
Each of three trays displays its two tallest stones ahead of shorter stones

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
A chairlift of evenly spaced empty chairs climbing a snowy slope, with one gap where a chair is missing

Longest Streak of Consecutive Days in T-SQL: Gaps and Islands

November 1, 2017
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL DateTime, SQL Scripts, SQL Server

Find the longest streak of consecutive days in T-SQL with gaps and islands: group dates with ROW_NUMBER, handle duplicates, and list gaps.

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

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