When you
issue a SELECT statement, SQL Server will evaluate the parts in a very specific
order:
1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER
Since the
SELECT part is where you will create aliases, you cannot use those aliases in
any part that comes before the SELECT. For example, any alias cannot be used in
the WHERE clause since it has not been parsed yet. But you can use the alias in
the ORDER BY.
You can just
select an aggregate function (such as SUM) and get a result. However, if you
combine an aggregate plus some other columns being selected, you will also
require a GROUP BY clause.
In general, it is best practice to explicitly
handle NULL values in the inputs you are selecting. A single NULL can cause you
whole expression to also become NULL. There are times when this is
automatically protected against. For example, if you do a COUNT() on some
column, it will skips NULL values and just give you the count of non-NULL
records.