Sunday, 12 February 2012

Select statement in SQL SERVER 2008



The SQL Server (Transact-SQL) SELECT statement is used to retrieve records from one or more tables in a SQL Server database.

Syntax
In its simplest form, the syntax for the SELECT statement in SQL Server (Transact-SQL) is:
SELECT expressions
FROM tables
[WHERE conditions];

Example - Select all fields from one table
Let's look at how to use a SQL Server SELECT query to select all fields from a table.
SELECT *
FROM TableName
WHERE quantity > 5
ORDER BY columnName ASC;



Select individual fields from one table
You can also use the SQL Server SELECT statement to select individual fields from the table, as opposed to all fields from the table.
For example:
SELECT columnName , columnName , columnName
FROM TableName
WHERE columnName >=666
AND columnName = 'Soft'
ORDER BY columnName DESC, columnName ASC;

Example - Using TOP keyword
Let's look at a SQL Server example, where we use the TOP keyword in the SELECT statement.
For example:
SELECT TOP(3)
columnName , columnName , columnName
FROM TableName
WHERE columnName = 'Soft'
ORDER BY columnName ASC;

Example - Using TOP PERCENT keyword
Let's look at a SQL Server example, where we use the TOP PERCENT keyword in the SELECT statement.
For example:
SELECT TOP(10) PERCENT
columnName , columnName , columnName
FROM TableName
WHERE columnName = 'Soft'
ORDER BY columnName ASC;

select statement in sql server