SELECT Statement (Transact-SQL)

Retrieves rows from a database table or view.

Note: The SELECT statement is the most fundamental statement in Transact-SQL. It is used to query the database for information.

Syntax

SELECT
    [ ALL | DISTINCT ]
    [ * | <expression> [ AS <column_alias> ] [ ,...n ] ]
    [ INTO new_table ]
    [ FROM
        [ <database_name>.<schema_name>. ]<table_or_view_name> [ AS <table_alias> ]
        [ ,...n ]
    ]
    [ WHERE <search_condition> ]
    [ GROUP BY
        { <expression> [ ,...n ] }
        [ WITH { CUBE | ROLLUP } ]
    ]
    [ HAVING <filter_condition_for_groups> ]
    [ ORDER BY
        <expression> [ ASC | DESC ] [ ,...n ]
    ]
    [ OFFSET { <constant> | @<variable> } { ROW | ROWS }
      [ FETCH { FIRST | NEXT } { <constant> | @<variable> } { ROW | ROWS } ONLY ]
    ]
    [ FOR [ BROWSE ] [ ,...n ] ]
    [ OPTION ( <query_option> [ ,...n ] ) ]
;

Parameters

ALL
Specifies that duplicate rows should be returned. This is the default behavior.
DISTINCT
Specifies that only unique rows should be returned. DISTINCT is applied to the entire row, not to individual columns.
*
Indicates that all columns from the specified tables should be returned.
<expression>
A constant, function, or column name that returns a value. This can include arithmetic or string operations.
AS <column_alias>
An alternative name for a column or expression in the result set. If an alias is not specified, the system generates one.
INTO new_table
Creates a new table and inserts the result of the query into it. The table must not already exist in the current database.
FROM
Specifies the tables or views from which to retrieve rows.
<table_or_view_name>
The name of the table or view to query. Can be a four-part name (database.schema.table).
WHERE
Filters rows based on a specified condition.
GROUP BY
Groups rows that have the same values in specified columns into a summary row.
HAVING
Filters groups based on a specified condition.
ORDER BY
Sorts the result set in ascending (ASC) or descending (DESC) order.
OFFSET ... FETCH ...
Used for implementing server-side cursors or for retrieving a subset of rows, typically for paging.
FOR BROWSE
A deprecated option that prevents client applications from updating the rows returned by the query. Use READ_ONLY for similar behavior.
OPTION
Allows you to specify query hints to influence query processing.

Examples

1. Select all columns from a table:

SELECT *
FROM Customers;

2. Select specific columns with aliases:

SELECT CustomerID AS ID, CompanyName AS [Customer Name]
FROM Customers;

3. Filter rows using the WHERE clause:

SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice > 50;

4. Group and aggregate data:

SELECT Country, COUNT(*) AS NumberOfCustomers
FROM Customers
GROUP BY Country
ORDER BY NumberOfCustomers DESC;

5. Use DISTINCT to get unique values:

SELECT DISTINCT City
FROM Customers;
Tip: Use TOP (SQL Server) or LIMIT (other SQL dialects) for retrieving a fixed number of rows, often used with ORDER BY.