Query Exercises and Answer Keys

Hands-on T-SQL query exercises and their answer or discussion posts.

31 associated posts31 primary posts

Performance Tuning

Query Exercise: Looking for Email Addresses

Personally identifiable information (PII) is freakin' everywhere.

When companies first start looking to identify and lock down their data, they think it's going to be as easy as identifying common columns like EmailAddress, DateOfBirth, SocialSecurityNumber, and so forth. They think, "We'll just encrypt those columns and we'll be fine."

Read more about Query Exercise: Looking for Email Addresses 12 comments — Join the discussion
Performance Tuning

Query Exercise Answer: Fixing a Slow Computed Column

In last week's Query Exercise, we added a user-defined function to the Users table to check whether their WebsiteUrl was valid or not. I noted that even with an index on Reputation, SQL Server 2022 simply ignored the index, did a table scan, and spent 2 minutes of time calling the user-defined function on a row-by-row basis.

Read more about Query Exercise Answer: Fixing a Slow Computed Column 5 comments — Join the discussion
Performance Tuning

Query Exercise Answer: Beating ChatGPT at Finding Good Question Times

For this week's Query Exercise, I asked you to write a better query than ChatGPT wrote. Your goal was to find the best days and times to post questions on Stack Overflow.

I found it interesting that a lot of the initial answers focused on the times when there were the most questions, or which questions were the most highly upvoted. For me, the best time to post a question is when you have the highest likelihood of getting the right answer, quickly.

Read more about Query Exercise Answer: Beating ChatGPT at Finding Good Question Times 2 comments — Join the discussion
Performance Tuning

Query Exercise: Beat ChatGPT at Finding Good Question Times

What are the best days of the week and times of the day to post a question at StackOverflow.com? It seems like a simple question, but it's surprisingly nuanced. I asked ChatGPT's latest version, 4o, and its answer made me laugh out loud. First off, the T-SQL is terrible: it creates a completely unnecessary temp…

Read more about Query Exercise: Beat ChatGPT at Finding Good Question Times 14 comments — Join the discussion
Performance Tuning

Finding Sister Locations to Help Each Other: Answers & Discussion

This week's query exercise asked you to find two kinds of locations in the Stack Overflow database:

Locations populated with users who seem to be really helpful, meaning, they write really good answers
Locations where people seem to need the most help, meaning, they ask a lot of questions, but they do not seem to be answering those of their neighbors

Read more about Finding Sister Locations to Help Each Other: Answers & Discussion 2 comments — Join the discussion
Performance Tuning

Query Exercise: Finding Sister Locations to Help Each Other

For this week's query exercise, let's start with a brief query to get a quick preview of what we're dealing with:
[crayon-6a6eb820773ec738748551/]

That query has a few problems, but hold that thought for a moment. (You're going to have to solve those problems, but I just wanted to show you the sample data at first to give you a rough idea of what we're dealing with.)

Read more about Query Exercise: Finding Sister Locations to Help Each Other 12 comments — Join the discussion
Performance Tuning

Find Recent Superstars: Answers & Discussion

Your query exercise for this week was to write a query to find users created in the last 90 days, with a reputation higher than 50 points, from highest reputation to lowest. Because everyone's Stack Overflow database might be slightly different, we had to start by finding the "end date" for our query. I'm working with the 2018-06 export that I use in my training classes, so here's my end date:

Read more about Find Recent Superstars: Answers & Discussion 1 comment — Join the discussion
Performance Tuning

Query Exercise: Finding Long Values Faster

Our developers have come to us with a problem query that isn't as fast as they'd like. Using any Stack Overflow database:
[crayon-6a6eb82078033706726743/]
It has an index, but SQL Server refuses to use that index in the execution plan:

If we force the index with a query hint, we do indeed get dramatically lower logical reads. In my particular database's case, the clustered index scan is 141,573 logical reads - but scanning the DisplayName index alone is just 38,641 logical reads.

Read more about Query Exercise: Finding Long Values Faster 45 comments — Join the discussion
Performance Tuning

Database Changes to Find Tagged Questions Faster: Answers and Discussion

In the last Query Exercise discussion, we hit a performance wall when we were trying to quickly find questions with a specific tag. We threw up our hands and said it's time to make database changes, but we had a few restrictions: We have a lot of existing code that uses the Posts.Tags column That…

Read more about Database Changes to Find Tagged Questions Faster: Answers and Discussion 5 comments — Join the discussion
Performance Tuning

Finding Tagged Questions Faster: Answers & Discussion

Your query exercise was to take this Stack Overflow query to find the top-voted questions for any given tag:
[crayon-6a6eb82078ad9832551705/]
That's currently using this index in its execution plan:
[crayon-6a6eb82078ade899811478/]
And answer 3 questions:

What kinds of tags will perform worse than others for this query?
Could you change the query to perform better?
Could you change the indexes to perform better, without changing the table structure?

Read more about Finding Tagged Questions Faster: Answers & Discussion 11 comments — Join the discussion
Performance Tuning

Finding the Best Time for Maintenance: Answers & Discussion

Your Query Exercise was to find the best time to do database maintenance by querying the Users table, looking for a one-hour window with the lowest number of created users. We kept this one pretty simple by looking at the data in just one table, and we didn't hassle with time zones. We just wanted the 3 lowest-load hours, in this format:

Read more about Finding the Best Time for Maintenance: Answers & Discussion 3 comments — Join the discussion