Beginner SQL section: avoiding repeated expression

Posted by polygenelubricants on Stack Overflow See other posts from Stack Overflow or by polygenelubricants
Published on 2010-05-25T02:50:23Z Indexed on 2010/05/25 3:01 UTC
Read the original article Hit count: 241

Filed under:
|
|

I'm entirely new at SQL, but let's say that on the StackExchange Data Explorer, I just want to list the top 15 users by reputation, and I wrote something like this:

SELECT TOP 15
  DisplayName, Id, Reputation, Reputation/1000 As RepInK
FROM
  Users
WHERE
  RepInK > 10
ORDER BY Reputation DESC

Currently this gives an Error: Invalid column name 'RepInK', which makes sense, I think, because RepInK is not a column in Users. I can easily fix this by saying WHERE Reputation/1000 > 10, essentially repeating the formula.

So the questions are:

  • Can I actually use the RepInK "column" in the WHERE clause?
    • Do I perhaps need to create a virtual table/view with this column, and then do a SELECT/WHERE query on it?
  • Can I name an expression, e.g. Reputation/1000, so I only have to repeat the names in a few places instead of the formula?
    • What do you call this? A substitution macro? A function? A stored procedure?
  • Is there an SQL quicksheet, glossary of terms, language specification, anything I can use to quickly pick up the syntax and semantics of the language?
    • I understand that there are different "flavors"?

© Stack Overflow or respective owner

Related posts about sql

Related posts about beginner