Databases Reference
In-Depth Information
SELECT * FROM table_name
Return specific columns from a table or view:
SELECT Column1, Column2, Column3 FROM table_name
Column alias techniques:
SELECT Column1 AS Col1, Column2 AS Col2 FROM table_name
SELECT Column1 Col1, Column2 Col2 FROM table_name
SELECT Col1 = Column1, Col2 = Column2 FROM table_name
Literal values:
SELECT 'Some literal value'
SELECT 'Some value' AS Col1, 123 AS Col2
Returning an expression value:
SELECT (1 + 2) * 3
Returning the result of a function call:
SELECT CONVERT( VarChar(20), GETDATE(), 101 )
TO Return a fixed number of rows:
SELECT TOP 10 * FROM table_name ORDER BY Column1
SELECT TOP 10 Column1, Column2 FROM table_name ORDER BY Column2
Return a fixed number of rows with the ties for last position. If the value in the nth row is the same as the
subsequent row(s), these rows are also returned.
SELECT TOP 10 WITH TIES Column1, Column2 FROM table_name ORDER BY Column2
Return a percentage of all available rows:
SELECT TOP 25 PERCENT * FROM table_name ORDER BY Column2
SELECT TOP 25 PERCENT Column1, Column2 FROM table_name ORDER BY Column2
For SQL Server 2005 only, substitute a variable or expression for a top values number:
DECLARE @TopNumber Int
SET @TopNumber = 15
SELECT TOP @ TopNumber * FROM table_name ORDER BY Column2
Top values-based on an expression:
SELECT TOP (SELECT a_column_value FROM some_table ) * FROM another_table
Search WWH ::




Custom Search