WHERE: Filtering Numbers with Ranges and Math

We’ve been querying the Tags table, filtering by TagName, but let’s just go back to the Tags table. Here’s the results from:

StackOverflow Tags Results
StackOverflow Tags Results

See the Count? That’s the number of times the tag has been used.

WHERE with Number Comparisons

Let’s find the really popular tags – the ones that have been used over 100,000 times:

Note that I’m not using a comma or a period, like 100,000 – SQL Server doesn’t like it when you stick punctuation in there unless we’re dealing with decimal places, like 3.14159. (Or 3,14159 depending on your location. SQL Server is pretty smart like that.)

Run this query at Data.StackExchange.com, and you get:

StackOverflow Tags where Count > 100000
StackOverflow Tags where Count > 100000

Presto, we get a few dozen really popular tags.

Similarly, all of these will work:

Granted, of course, that last one is an awfully odd way to filter Tags, but it’ll work.

For more details about which operators you can use, check out Books Online:

WHERE with Column Comparisons

You can also do comparisons between columns. Let’s find tags where their Id is higher than the number of times the tag has been used:

Run that query, and we get some new tags that haven’t been used too often:

N00bs
N00bs

 

Now, you try.

Click Users at the top of StackOverflow, and check out the list of users:

StackOverflow users list
StackOverflow users list

Write a query to show only the users with a Reputation score higher than 250,000 points.

When you’re done with that, move on to the next lesson.