The SQL command SELECT retrieves rows. The basic syntax is as follows:

SELECT [ALL | DISTINCT] [TOP(n) [PERCENT]] SelectList
[INTO NewTable]
[FROM TableSources]
[WHERE RowSearchCondition]
[ORDER BY OrderList [ASC | DESC]]

The SELECT command retrieves columns specified by the SelectList, from rows that come from TableSources .

The ALL keyword is the default and therefore optional. It returns all rows, even if there are duplicate rows.

The DISTINCT keyword can be used to eliminate duplicate rows from the results of a SELECT command. Here is a classic example of its usage:

The TOP keyword makes the SELECT command take the result set it would normally return, and return either the first n rows or the first n PERCENT. You can always get the "bottom" by having them in descending order. Note that this concept is implemented in different ways:

The SelectList is a comma-separated list of expressions that yield columns in the results of a SELECT command. Here are a few particular about the SelectList:

SELECT expression [AS] NewColumnName [, ...]
--It is customary to use the AS keyword.
SELECT NewColumnName = expression [, ...]
--This is particular to Transact-SQL.

The INTO clause uses the results of a SELECT command to make a new table NewTable, or a new local temporary table #NewTempTable (available only for the current connection) or a new global temporary table ##NewTempTable (available until the last connection using it closed).

The FROM clause specifies other table or view sources from which rows are pulled to make the results of a SELECT command. If a SELECT command is pulling data from tables or views, then it requires at least one table or view to be named in the FROM clause, otherwise it can skip the FROM clause.

The WHERE clause makes the SELECT command output only the rows that meet the RowSearchCondition.

The ORDER BY clause orders the rows of the results of a SELECT command according to each column specified in the comma-separated OrderList. Each column sorts by ASC (ascending, the default) or DESC (descending). Each column must be one of the columns in the SelectList. Each column in the list may be:

EGs

All rows

These SELECT commands are equivalent and return all rows and all columns from a table.

SELECT *
FROM tblA
SELECT ALL tblA.*

SELECT without using FROM

These SELECT commands don't need a FROM clause since they don't reference a table.

SELECT PI(), 5
--These are constants
SELECT DB_ID('pubs')
--This is a system function.

Returning a calculated column

This SELECT command returns a result set with some columns from a table and a calculated column.

SELECT Title, Rtrim(FName) + ' ' + Rtrim(LName) AS [Full Name]
FROM tblA
--The above returns results from all rows.
SELECT Title, Rtrim(FName) + ' ' + Rtrim(LName) AS [Full Name]
FROM tblA
WHERE Salary >= 75000
--The above returns results from some rows.

ORDER BY the results

This SELECT command sorts out its result set.

SELECT *
FROM tblCustomers
ORDER BY CustomerID 

Count rows

This SELECT command returns a count of the number of rows that meet the condition. SELECT commands of this format are frequently used as sub-queries, since it returns a single value.

SELECT Count(*)
FROM tblCustomers
WHERE Name = 'Fred'

Page Modified: (Hand noted: 2007-10-12 20:18:48Z) (Auto noted: 2007-11-17 06:45:19Z)