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
Three ranking functions on the same eight scores: RANK gives 1, 2, 2, 4, 5, 5, 5, 8, DENSE_RANK gives 1, 2, 2, 3, 4, 4, 4, 5, and ROW_NUMBER gives 1 to 8, with the tied rows marked in red

RANK( ), DENSE_RANK( ), and ROW_NUMBER( ): How Each Handles Ties

August 12, 2011
Pinal Dave
SQL Tips and Tricks
Ranking Functions, SQL Order By, SQL Scripts, SQL Server

RANK( ), DENSE_RANK( ), and ROW_NUMBER( ) return the same numbers until your data has a tie. This post ranks eight teas with all three functions and shows how each one handles tied scores. It also shows how to list the top three distinct scores and why ROW_NUMBER needs a tiebreaker.

Read More
Gouache painting: two wooden posts stand at opposite ends of a meadow

geometry STLength: A Path Is Longer Than Its Chord

August 11, 2011
Pinal Dave
SQL Tips and Tricks
SQL Datatype, SQL Function, SQL Scripts, SQL Server

geometry STLength measures the segments of a line. The example contrasts that path length with the shortest distance between its endpoints, including a closed line.

Read More
Thirteen seedlings listed with the number of plants in their bed. The five North plants show 5, the three South plants and three East plants show 3, and the two plants with no bed show 2

Partitioned Aggregates: Compare Each Row to Its Own Group

August 11, 2011
Pinal Dave
SQL Tips and Tricks
SQL Function, SQL Group By, SQL Scripts, SQL Server

Partitioned aggregates let every row see a number for its own group, such as how many plants share its garden bed. This post uses thirteen seedlings in four beds to show PARTITION BY with COUNT and AVG. It also shows the integer division trap that turns every share into 0, and how to get one row per group.

Read More
A detached ceramic lid beside an empty pale cup and a blue cup holding a stone on sage cloth.

FOR XML XSINIL: Emit Missing Fields as Explicit Nil Elements

August 10, 2011
Pinal Dave
SQL Tips and Tricks
SQL Function, SQL Scripts, SQL Server, SQL XML

FOR XML XSINIL can represent SQL NULL with an explicit element carrying the xsi:nil attribute. I compare that output with ordinary element omission. An empty string stays a separate input in both versions.

Read More
Two results for the same eight trip costs. SUM(Cost) returns one row, 1385.00. SUM(Cost) OVER ( ) returns all eight rows with 1385.00 beside each one

Aggregates With the OVER Clause: A Total Beside Every Row

August 10, 2011
Pinal Dave
SQL Tips and Tricks
SQL Function, SQL Group By, SQL Scripts, SQL Server

Aggregates with the OVER clause put a total, an average or a minimum next to every row, so the rows do not collapse into one. This post uses eight trip costs to show the error a plain SUM gives, the fix, and the percent of the total for each row. It also shows how WHERE changes the total and how to filter on the result.

Read More
Gouache painting of a stack of blank cards and an open document tray, representing a set of rows.

SQL Server: Use INSERT INTO SELECT Instead of a Cursor

August 10, 2011
Pinal Dave
SQL Tips and Tricks
Best Practices, SQL Scripts

Replace a row-by-row server inventory copy with INSERT INTO SELECT and test the results, including a loop that fails when IDs have gaps.

Read More
One brass key blank fits a precise profile jig while a similar blank with a different ridge rests outside

LIKE Character Ranges: [a-f], [^0-9] and Collation Traps

August 9, 2011
Pinal Dave
SQL Tips and Tricks
SQL Collation, SQL NULL, SQL String, Unicode

A bracket range sounds simple until the collation decides what sits between a and f. Test it once so a bad code never reaches your table.

Read More
Previous 1 … 581 582 583 584 585 586 587 … 844 Next
  • Books
  • Testimonials
  • Privacy Policy

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