The title of this section may read like a bad tabloid headline, but in all seriousness it means that the SQL SELECT statement returns columns in the order in which you list them in the SELECT statement. The column may be any expression or any column in the FROM clause.
Following the FROM clause and the WHERE clause, the next logical step in the query is the list of returned expressions.
The Star
The *,
commonly called “star,” is a special wildcard that includes all columns
in their table order. If the query pulls from multiple tables, the * includes all columns from every table. Alternatively, tablename.* includes only the columns from the named table. Also, if you alias any tables in the FROM clause, you can return a complete column list of a specific example by using table alias.*.
Aliases
The name of the column in the
underlying table becomes the name of the column in the result set.
Optionally, you can change the column name using a column alias.
Expressions and constants have a blank column heading in the result set unless an alias is provided.
The AS
keyword is optional, but just as with a table alias, using it is a good
practice that improves the readability of the code and helps prevent
errors.
To use an alias that's identical to a SQL Server
keyword or that includes a space, enclose the alias in square brackets,
single quotes, or double quotes. Although the square brackets are not
technically required if the alias is the same as an object name (that
is, table or column name), you an explicitly specify that the alias is
not a keyword.
The following code demonstrates adding aliases to columns:
USE AdventureWorks;
SELECT
Name AS ProductName,
‘abc',
SellStartDate + 365 OneYearSellStartDate
FROM Production.Product;
Result:
ProductName OneYearSaleStartDate
-------------------------- ---- ------------------------
Adjustable Race abc 2003-06-01 00:00:00.000
Bearing Ball abc 2003-06-01 00:00:00.000
BB Ball Bearing abc 2003-06-01 00:00:00.000
. . .
The first column's name is changed from Name to ProductName
by means of an alias. The second column is an expression without an
alias, so it has no column name. A better practice is to name
expression columns using an alias, as demonstrated in the third column.
Accidental aliases are a common source of errors. Take a careful look at the next query:
USE AdventureWorks;
SELECT
Name
‘abc',
SellStartDate + 365 OneYearSellStartDate
FROM Production.Product;
Result:
abc OneYearSaleStartDate
------------------------- -------------------------
Adjustable Race 2003-06-01 00:00:00.000
Bearing Ball 2003-06-01 00:00:00.000
BB Ball Bearing 2003-06-01 00:00:00.000.. .
The second column isn't abc as in the previous query. Instead, because of a missing comma, the ‘abc' in the query became an accidental alias for the first column.
What's Wrong with Select *?
For some developers, using SELECT * is
common practice. However, this method of programming can pose a few
challenges. If you talk to five SQL Server DBAs, it is likely that you
get five different reasons not to use the SELECT *. On the other hand,
if you talk to five SQL developers, they might give you five different
reasons to use SELECT *. Basically you should avoid using it for
several reasons, but if you are ever faced with the challenge of
validating, here are a few reasons why:
- It is a waste of resources. In most cases more data is returned than needed.
- If you use a SELECT * in your query, how do you build a covering index to satisfy the needs of the query?
- Adding a column to the table could potentially break the application.
These are only a few, but there are
many more and they all are valid. Just a quick Internet search should
reveal a list that provides enough ammunition for any DBA.
Qualified Columns
A common problem with queries is that
column names are duplicated in multiple tables. Including the column in
the select list by column names alone can cause an “ambiguous column
name” error. Basically, SQL Server complains that it doesn't know which
column you refer to. Even if they contain the same exact data, SQL
Server must know which column to select.
CREATE TABLE t1 (col1 INT);
CREATE TABLE t2 (col1 INT);
SELECT col1
FROM t1
CROSS JOIN t2;
Result:
Msg 209, Level 16, State 1, Line 2
Ambiguous column name ‘col1'.
The solution, of course, is to qualify the column name by including the table:
SELECT t1.col1
FROM t1
CROSS JOIN t2;