RANK() vs DENSE_RANK(): #SQLNewBlogger

I haven’t done one of these in awhile, but I saw an article recently about this and decided to explain it to myself, but in a slightly different way. You’ll see how I checked on RANK() vs DENSE_RANK() below.

Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

Getting Started

Imagine you’ve never used these functions, but you need to rank some data. Let’s say that you have a series of things that measure sale and you want to know who has done the best job this week. I’ll start with AdventureWorks, and we’ll assume it’s June 2011. If I look at some data from that database, I see this. Order Headers and Order Details from that range.

2026-02_0125

Who are the top 5 salespeople?

I put this into SQL Prompt AI: who are the top 5 salespeople with ranking for the first week of june 2011. Give me the salespersonid, the rank and sales total

I got this code, though with 2011-06-01 as the first date, which didn’t work as there were no orders that week. I adjusted this back to May 31. You can see this uses RANK() as the aggregate.

2026-02_0126

I then asked about DENSE_RANK, because I heard about it and might want to use it.

2026-02_0127

Prompt adjusted the query to add DENSE_RANK, but I then clicked the “add explanation” next prompt prompt (is that a thing now?)

2026-02_0128

I now see an explanation, though in this example, this doesn’t help.

2026-02_0129

I then asked Prompt to help me understand.

2026-02_0130

I got back this code. You can see in the results, that both show me the ties for 2nd with people that have the same sales totals. However, with RANK, we jump to 5, since there are 3 people tied at 2 (presumbly taking 2,3,4th place). The dense ranking goes from the ties at 2 to 3.

2026-02_0131

I asked for an explanation:

2026-02_0132

I got this. This is the text from SQL Prompt AI (in italics):

# RANK vs DENSE_RANK: When to Use Each


The key difference between RANK and DENSE_RANK is how they handle ties:


## RANK
– Assigns the same rank to ties
– Skips the next rank(s) after ties
– Creates “gaps” in the ranking sequence
– Example: 1, 2, 2, 4, 5 (note the missing 3)


## DENSE_RANK
– Also assigns the same rank to ties
– Does NOT skip ranks after ties
– Creates a “dense” or consecutive ranking without gaps
– Example: 1, 2, 2, 3, 4 (no gaps)


## When to use DENSE_RANK:
1. **Consecutive Ranking Systems**: When you need a continuous sequence without gaps, such as in competitions where tied participants should receive the same position, but the next participant should be ranked immediately after.


2. **Top N Analysis**: When identifying items in the “top 10” or similar groups, DENSE_RANK ensures you get exactly 10 distinct ranks regardless of ties.


3. **Percentile Calculations**: When calculating percentiles or quartiles where a continuous ranking is needed.


4. **Educational Grading**: When multiple students with the same score should receive the same rank, but the next student should be ranked immediately after.


## When to use RANK:
1. **Traditional Competition Standings**: Olympic medals and many sports competitions use this approach – if two athletes tie for first place, the next finisher gets bronze (3rd place, not 2nd).


2. **Statistical Analysis**: When you need to preserve the mathematical properties of ranks where the sum of all ranks should equal the sum of consecutive integers.


3. **Scenario Where “Skipped” Positions Matter**: When it’s important to account for how many entities are ahead of each entity.


Your example query perfectly demonstrates this difference:
– Employees with $145,000 all get rank 2 with DENSE_RANK
– With RANK, they get position 2, but the next rank is 5 (skipping 3 and 4) because there are three employees sharing rank 2


Each function has its appropriate use cases depending on whether you want a continuous ranking sequence or one that accounts for the number of entities at each rank level.

What I Learned Here

I knew most of this, but in terms of trying to understand better where/why to use each, the explanation made sense. I hadn’t thought about the Educational Grading for DENSE_RANK, but that makes sense. I knew the others, and often that the TOP N needs the correct number of rankings.

For RANK, we use the competition ranking with volleyball, so I see that all the time, but I don’t do a lot of statistical analysis where this has come up, but it’s good to keep it in mind.

To me, I often go back to the client, or think about both of these when I rank things. I will do what Prompt AI did and put both in a query, see the differences and then decide (or let someone else decide) how to present the ranking data.

SQL New Blogger

When I started to explain this, I first opened the DOC pages and was going to use those to write this and thought, this is a good place to test AI models and see. I took a different tact and incorporated some AI into my work, because that’s where the world is going. Like it or not.

This went faster with AI, and less cognitive load from me. I wrote this post, but I used AI to help set things up, generate code, and get me there quicker. You could do the same thing and use a blog to showcase that you’re learning how AI is a tool you can use.

SQL Prompt can help you learn more about your code, in addition to all the cool time saving features. Give it a try today.

FWIW, I asked CoPilot the same query and got an answer (0 people), without code. When I asked for code, I did get it, but not quite what I wanted.

Posted in Blog | Tagged , , , | Leave a comment

Tools You Need

This is the oldest editorial we have on the SQL Server Central site. This is being re-run for the US President’s Day holiday as Steve is on vacation and we are celebrating 25 years of SQL Server Central.

I was browsing the web recently and caught this note on coders and their tools and a related article on must have tools. It seems the focus was more for programmers and network administrators, but there are definitely some good tools in the list for DBAs to understand.

However, since it’s Friday, it got me thinking…

What are your essential DBA tools?

By these I mean those pieces of software not included with SQL Server, that you find very handy. It can be a utility that serves some purpose or a programming aid, I’m wondering what tools outside of those that come with SQL Server do you consider essential.

For me I have to say that the one tool I find most handy right now is Litespeed. I’ve used this utility for backups to save space for years. We made a deal a long time ago with DBAssociates, who developed the tool, and we’ve stuck with it. As of now we’re quite a few versions back since Imceda and then Quest took over the tool, but we’re happy with the way it’s worked.

That’s not to say other products aren’t just as good or even a better value, but that’s the one tool I’ve found most handy for me with SQL Server.

So are you using compare tools? Programming IDEs? Something else I’m not thinking of that has proven to be an essential SQL Server DBA or developer tool?

Let us know. You might just make someone’s day.

Steve Jones

Posted in Editorial | Tagged | 3 Comments

Microsoft Security Changes and SQL Server

For almost as long as I’ve been working as a data professional, NTLM has been the security protocol used in Windows. Microsoft added Kerberos over 20 years ago, but NTLM is still a fallback. Like so many things Microsoft has worked on, they loathe breaking backwards compatibility, so NTLM has been available. However, it has issues, like the double hop problem, and there are numerous security issues with the protocol. I tested a security product over 20 years ago that could break NTLM passwords in under an hour. On old Pentium-based computers.

This week Rebecca Lewis posted an article about the upcoming changes in Windows where NTLM is being phased out. She audits various clients and finds many are still using NTLM for SQL Server connections. Her observation is many people aren’t aware of this, and I’d concur. There is an informational message that is written to the SQL Server error log, but how many of you are checking the log and acting on this or even understand what it means? How many of you might have developers (or yourself) using named pipes and be unaware? That’s an NTLM only connection.

Heck, I’ve got a friend fighting through SSL connections with SQL Server, which is something I rarely seen. This person will eventually no longer need to “trust server certificate” in every connection string, but I bet many of you are years away from implementing that. That’s another change Microsoft wanted implemented, and why modern drivers no only set this to true by default.

Later this year, NTLM v1 will phase out, but that’s not likely what most of you use with SQL Server. However, the next major Windows server release will disable NTLM v2, and you won’t remember this editorial or the announcement then. What will happen is Windows admins will upgrade systems and you won’t be able to connect.

Rebecca gives you some things to check, but since many of you might work in large estates, you’ll need time to ensure clients and servers get updated and NTLM isn’t the protocol you depend on. Trust me, if Windows or even a client driver upgrade remove this, you are in for a bad day (or week, or weeks) trying to get things working.

I’d also suggest you learn how SQL Server SSL connections work. I don’t know that many orgs will require this, but some might as security becomes more automate-able and more CSOs start to ask that we ensure no man-in-the-middle attacks reach our servers.

Steve Jones

Posted in Editorial | Tagged , , | Leave a comment

Expanding into Print

This is part of a few memories from the founders of SQL Server Central, celebrating 25 years of operation this month.

When we started SQL Server Central, our goal was to build a great resource that helped other people advance in their careers and also made some money. Our decisions in building the site were based around the digital world and treating the community as we would want to be treated. Over time, however, we realized that continuing to grow this business was hard in a digital-only world. We experimented and proposed helping others build similar sites, like ReportingServicesCentral (which would have been great) or NotificationServicesCentral (which would not), but ultimately, we weren’t experts enough in those areas and couldn’t find people willing to partner.

Everyone thought they could do it themselves and that the knowledge was the hard part, and execution was easy. The truth is that the reverse is the way it works.

In any case, there was a point in time when we were sending 6 newsletters a week (Monday through Saturday) and we didn’t think there was much room for growth there. Andy suggested that we compile all our articles into a Best of SQLServerCentral book. I didn’t think they would sell well (they didn’t), but we did enjoy giving them away at our annual SQL Server Central Party at PASS. We even added a second series where we compiled the Question of the Day series into books, based on the Two Minute Mysteries I read as a child. We called them SQL Server Stumpers. Those were hard to manage, and were multi-month long projects that I had to toil away at almost every week.

Then came the magazine: SQL Server Standard.

In the early 2000s, the Professional Organization for SQL Server was trying to grow as well. We knew magazines were popular and profitable back then, so we proposed helping them produce a magazine every other month (6 times a year). They were charging an annual membership fee and needed to give members more value, so we agreed to produce, publish, and ship a magazine to all their members. It was both a source of tremendous stress for me to manage, as well as a proud item I could point to every month.

I wish I had some online links, but this was intended to be a real-world, analog item. We hoped it would grow to be a substantial revenue item, but it never did and PASS shut it down after around a year of publication.

I was glad because these projects were a never-ending source of stress. Managing book projects that were 4-6 months, along with the every-other-month magazine, and a daily newsletter was overwhelming at times.

Our forays into the print world provided me with a lot of education about how books and magazines work, and gave me a few mementos.

Steve Jones

Listen to the podcast at Libsyn, Spotify, or iTunes.

Note, podcasts are only available for a limited time online.

Posted in Editorial | Tagged | Leave a comment