Description
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];
However, the full syntax for the SELECT statement in SQL Server (Transact-SQL) is:
SELECT [ ALL | DISTINCT ] [ TOP (top_value) [ PERCENT ] [ WITH TIES ] ] expressions FROM tables [WHERE conditions] [GROUP BY expressions] [HAVING condition] [ORDER BY expression [ ASC | DESC ]];
Parameters or Arguments
- ALL
- Optional. Returns all matching rows.
- DISTINCT
- Optional. Removes duplicates from the result set. Learn more about the DISTINCT clause
- TOP (top_value)
- Optional. If specified, it will return the top number of rows in the result set based on top_value. For example, TOP(10) would return the top 10 rows from the full result set.
- PERCENT
- Optional. If specified, then the top rows are based on a percentage of the total result set (as specfied by the top_value). For example, TOP(10) PERCENT would return the top 10% of the full result set.
- WITH TIES
- Optional. If specified, then rows tied in last place within the limited result set are returned. This may result in more rows be returned than the TOP parameter permits.
- expressions
- The columns or calculations that you wish to retrieve. Use * if you wish to select all columns.
- tables
- The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
- WHERE conditions
- Optional. The conditions that must be met for the records to be selected.
- GROUP BY expressions
- Optional. It collects data across multiple records and groups the results by one or more columns.
- HAVING condition
- Optional. It is used in combination with the GROUP BY to restrict the groups of returned rows to only those whose the condition is TRUE.
- ORDER BY expression
- Optional. It is used to sort the records in your result set. ASC sorts in ascending order and DESC sorts in descending order.
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 inventory WHERE quantity > 5 ORDER BY inventory_id ASC;
Comments
Post a Comment