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: Find the Best Time for Maintenance

If we've gotta take the database down for maintenance - perhaps a version upgrade, perhaps upgrading our own code, maybe scaling up the hardware - when's the best time to do it?

For this week's query exercise, the business has asked us to identify 3 1-hour periods with the least user activity, in this format:

Read more about Query Exercise: Find the Best Time for Maintenance 46 comments — Join the discussion
Performance Tuning

Find Posts with the Wrong CommentCount: Answers & Discussion

Your Query Exercise was to find denormalization accuracy problems: checking the accuracy of a reporting column, Posts.CommentCount. There were two parts: finding the top 100 most problematic Posts with the biggest variances, and thinking about a long term solution to keep the CommentCount accuracy as high as practical. Question 1: Finding the Problematic Posts Your…

Read more about Find Posts with the Wrong CommentCount: Answers & Discussion 4 comments — Join the discussion
T-SQL & Development

Find 40 Problems in This Stored Procedure.

Aaron Bertrand posted a challenge:
We’re going to use the AdventureWorks sample database (get your copy here), where the folks in marketing requested a list of users to e-mail a new promotional campaign. The customers need to meet at least one of the following criteria:

last placed an order more than a year ago
placed 3 or more orders in the past year
have ordered from a specific category in the past two weeks

Read more about Find 40 Problems in This Stored Procedure. Be the first to comment

[Video] Watch Brent Write Queries

This morning, I wanted to write a few new "background noise" queries for my Mastering classes. In those classes, the students run live workloads against the Stack Overflow database and troubleshoot performance issues, and the more of a varied workload I've got running, the more realistic it feels. For this session, I specifically wanted to write quick queries - queries that would produce real-world style data in a few seconds, in the style a user would actually write them.

Read more about [Video] Watch Brent Write Queries 3 comments — Join the discussion

SQL Interview Question: “How do you respond?”

Brent's in class this week!
So you get me instead. You can just pretend I'm Brent, or that you're Brent, or that we're both Brent, or even that we're all just infinite recursive Brents within Brents. I don't care.
Here's the setup
A new developer has been troubleshooting a sometimes-slow stored procedure, and wants you to review their progress so far. Tell me what could go wrong here.

Read more about SQL Interview Question: “How do you respond?” 40 comments — Join the discussion

SQL Interview Question: “Talk me through this query.”

Last month's post "For Technical Interviews, Don't Ask Questions, Show Screenshots" was a surprise hit, and lots of folks asked for more details about the types of screenshots I'd show. Over the next few weeks, I'll share a few more. Normally I'd show this query as a screenshot, but for easier copy/pasting into comments, I'm…

Read more about SQL Interview Question: “Talk me through this query.” 48 comments — Join the discussion