Hot answers tagged

22 votes
Accepted

Excuse me, how many of you are new here? Mind if I JOIN you?

Technically, the full outer join is insufficient. If during a certain week there was not a single user who posted their first question or answer, the plot for that week needs to be 0. It would be ...
15 votes
Accepted

Searching for bad Minecraft questions on Gaming.SE

The use of the Tags field on the Post is a poor choice for the query. It relies on a table scan, which is slow, because it has to check each question. Note, you can use the ...
  • 97.1k
14 votes
Accepted

"How can I make this SEDE query better?" (bad title finder)

Your query is well structured, and consistent, but there is one issue you have failed to incorporate.... not all bad questions have a user. When questions are migrated, or there's a user deleted, the ...
  • 97.1k
14 votes

Excuse me, how many of you are new here? Mind if I JOIN you?

'2011-Jan-01' --There was very little activity prior to this date. Including it skews the graph. And then ...
14 votes
Accepted

A wounded Zombie finder, or: how to find the almost dead undead?

Not utterly wrong, but q.PostTypeId = 1 and AND a.PostTypeId = 2 are superfluous. The only post types with a non-null ...
  • 1,089
13 votes
Accepted

Stack Exchange User Activity Score

TOP 1 When selecting values that correspond to Unique fields referenced by constants (OwnerUserId), you're only really ...
  • 11.9k
11 votes

Searching for bad Minecraft questions on Gaming.SE

This doesn't do anything at all. ClosedDate IS NULL OR ClosedDate IS NOT NULL Saying "where is or not" results in the same thing as not including the statement ...
  • 30.8k
10 votes
Accepted

Query to count interactions between users

Before we begin with the code... I just want to address one thing regarding test cases with sample data. To get the best out of a performance review of your queries, try to provide a sample that's as ...
  • 20.3k
10 votes
Accepted

SQL trigger to log when employee records are updated

First some comments. Usage of Deprecated Features Currently your trigger does a SELECT that returns a resultset from a trigger. That was at one time supported but ...
  • 2,009
9 votes
Accepted

Replacement for sp_MSforeachdb

First off, please heed the advice from @Zak about Version Control System (VCS). That should be the first take-away from this. Slashy comment block You have 31 lines, 4462 characters of slashy ...
  • 20.3k
9 votes
Accepted

Build WHERE clause for search conditions

Sanitizer is a dangerous misnommer IMO. A better name could be NameQuoter, since that's what it does: it uses the RDBMS-specific ...
8 votes
Accepted

Popular questions by view count

Good things You use good local variables, and you are consistent with your naming. The typical naming for T-SQL is using PascalCase, however there are no standards ...
  • 20.3k
8 votes

Excuse me, how many of you are new here? Mind if I JOIN you?

You can do this more succinctly by first grouping by the User and the Post Type to get a set of every first question and answer. Then group that set by the type of post and the week it occurred in. ...
  • 252
8 votes

Find & Delete Duplicate Records on All Tables in current MSSQL Database

You started out with all your keywords being capitalized and then you abandoned your capitalization haphazardly throughout the script, this makes it a little hard to read because your brain says that ...
  • 28.6k
8 votes
Accepted

Returning two dates based on input

Misspellings There are several misspelled words you should correct (see line comments): ...
  • 20.3k
7 votes
Accepted

Percentage of questions that are unanswered or answered

Comments Your comments are mostly clutter, and I mean that because your naming is good enough to where the comments are mostly irrelevant. Also, if you do need to make multi-line comment, you can use ...
  • 20.3k
7 votes

"How can I make this SEDE query better?" (bad title finder)

I noticed that you put Comma first in your select statements, something that I also do is to put the conjunctions first in my where conditions, it has similar advantages ...
  • 28.6k
7 votes
Accepted

FizzBuzz in T-SQL

In SQL (any flavor) you usually want to avoid loops, if at all possible, in favor of set-based operations. However, in your case, there's not really a set, so-to-speak, other than a series of ints <...
  • 20.3k
7 votes
Accepted

Execute every Monday that is not the Monday following last Saturday in month

Sometimes, comments aren't the clearest way to make a script readable: IF ( (--is it a monday? SELECT DATEPART(dw,@now) ) = 2 ...
7 votes
Accepted

Deduplicating value pairs between 2 columns

If val1 and val2 are comparable it is easy to find the distinct values, using set-based operations, by normalizing first and ...
  • 2,919
7 votes

SQL trigger to log when employee records are updated

You are basically reformatting one group of fields into another. I'm not sure if that's a good idea - you might want to just make a straight-up copy of the fields, since that improves your "audit" ...
  • 12.2k
6 votes
Accepted

Creating Sequential IDs

Minutia A table called V ... really? Columns Column1 and Column2 .... why are you using ...
  • 97.1k
6 votes
Accepted

SQL to find table containing all specified columns

In this case, I would consider using a CTE to contain the columns you are interested in. This saves having to declare the variables, and also saves the inserts, etc. The basic concept is the same as ...
  • 97.1k
6 votes
Accepted

Coin flips and Dice rolls in a stored procedure

Overall, I think your procedure is very good. It's consistent in layout, capitalisation and style, and well commented. I had no problems reading through it and understanding what it was doing (and how)...
6 votes
Accepted

Top Python badged users from NYC

Questions First to address some of your questions: Would this sql work with all relational DB's that accept SQL? No, it would not. In fact it's quite unrealistic (if not impossible) to write SQL ...
  • 20.3k
6 votes

Stack Exchange User Activity Score

This is too much for a comment, adding to @Quill's answer: In your Select you use FROM Users, Comments, Posts which is an unconstraint cross join, i.e. every row ...
  • 1,172
6 votes

T-SQL UDF to replace replace base URLs

Your code looping over string functions doesn't seem like something SQL Server is optimized for. Database systems are optimized for set based operations, not looping. I understand the confusion ...
  • 161
5 votes

"How can I make this SEDE query better?" (bad title finder)

Let me address a possible DRY issue in the query in exchange for possible poorer performance. I'm in particular talking about this bit: ...
  • 265
5 votes

SQL procedure with cursor to check if values in table 1 are also in table 2

Doing that (checking if values in Table1 are or aren't in Table2) is typically done with a ...

Only top scored, non community-wiki answers of a minimum length are eligible