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 sloping egg rack at a market stall, one hand taking the oldest egg from the front, another adding a fresh one.

DELETE TOP With ORDER BY: Removing the Oldest Rows First

May 1, 2024
Pinal Dave
SQL Tips and Tricks
CTE, SQL Delete, SQL Order By, SQL Server, SQL Top

Use DELETE TOP safely through an ordered CTE, preview oldest rows, apply a supporting index, and remove eligible rows in bounded batches.

Read More
A sieve holds a few kernels above a trough full of sifted grain, with a larger mound behind

TOP N Sort: Why a Small TOP Still Reads the Whole Table

November 3, 2021
Pinal Dave
SQL Performance
Execution Plan, SQL Index, SQL Order By, SQL Server, SQL Top

Explain TOP N Sort, compare an indexed ordered access path, and align filter and sort keys before adding indexes for frequent requests.

Read More
Distinct tile selection remains separate from a deliberate alignment jig.

SQL SERVER – DISTINCT Does Not Guarantee ORDER BY

November 5, 2020
Pinal Dave
SQL Tips and Tricks
Execution Plan, SQL Operator, SQL Order By, SQL Scripts, SQL Server

DISTINCT removes duplicate result rows without guaranteeing their order. My original claim that it always sorts output was incorrect.

Read More
Gouache painting of a market stall with two rows of sorted apples and a vermilion arrow post pointing at one row

ORDER BY a Parameter in SQL Server: CASE, Types and Plans

June 8, 2020
Pinal Dave
SQL Tips and Tricks
SQL CASE, SQL Order By, SQL Scripts, SQL Stored Procedure

To ORDER BY a parameter in SQL Server, write one CASE expression for each column and direction. This post builds the procedure, shows the type conversion and DISTINCT errors that trip people up, measures the cost of the sort that CASE forces, and compares OPTION (RECOMPILE) and dynamic SQL.

Read More
Gouache painting of rows of pebbles arranged smallest to largest on a flat rock with one vermilion pebble

Does GROUP BY Sort Results? Only ORDER BY Guarantees It

October 23, 2019
Pinal Dave
SQL Tips and Tricks
Execution Plan, SQL Group By, SQL Order By, SQL Scripts

Does GROUP BY sort the rows it returns? No: only ORDER BY guarantees an order. This post builds a small demo, shows one GROUP BY query returning two different orders, and reads the plans that explain why.

Read More
Gouache painting of a shelf of sorted pears with the first three covered and the next four in a vermilion basket

OFFSET FETCH in SQL Server: Get Rows 21 to 30 After ORDER BY

August 6, 2019
Pinal Dave
SQL Tips and Tricks
SQL Order By, SQL Paging, SQL Scripts, SQL Server

OFFSET FETCH reads a block of rows from the middle of a sorted result. The demo returns rows 21 to 30, builds pages from a page number and measures why deep pages cost more. A seek based alternative reads the same two pages every time.

Read More
Cooking spoons hang in ascending bowl sizes regardless of their handle colors

How to Sort a Varchar Column Storing Integers with Order By? – Interview Question of the Week #206

December 30, 2018
Pinal Dave
SQL Interview Questions and Answers
SQL Datatype, SQL Order By, SQL Scripts, SQL Server, SQL String

How do you sort a Varchar column storing integers with Order By when you cannot change the datatype? Here are two simple solutions I shared with a developer.

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

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