In this Video, I am going to discuss how to use the Select Statement in SQL Server with syntax and examples. Please read our previous Video where we discussed the Difference Between Sequence and Identity in SQL Server with examples. As part of this Video, we are going to discuss the following pointers related to the Select Statement.
What is Select Statement in SQL Server?. The Complete Microsoft SQL Server Developer Course 2023 [Videos].
- What is the Use of Select Statement in SQL Server?
- Syntax and examples of Select Statement.
- Parameters or Arguments used in Select Statement.
- How to Select all fields from one table in SQL Server?
- How to Select individual fields from one table in SQL Server?
What is the Use of Select Statement in SQL Server?
The Select Statement in SQL Server is used to returns records in the form of a result set from one or more tables or views. Generally, the Select Statement retrieves the data in the form of rows from one or more database tables or views.
The SQL Select Query does not store any data itself. It simply displays the data that is stored in database tables. The Select Statement can retrieve and shows the data from one or more database tables, from other queries, or from a combination of the above two.
Select Statement Syntax in SQL Server:
SELECT Column_List FROM Table_Name
If you want to select all the columns of a table or view then you can also use “*”, but for better performance use the column list instead of using “*”.
SELECT * FROM Table_Name
You can even use where condition in the select statement as shown below
SELECT expressions FROM tables [WHERE conditions];
However, the full syntax for the SELECT statement in SQL Server (Transact-SQL) is:
Parameters or Arguments used in Select Statement in SQL Server:
We can use the following parameters or arguments along with the Select Statement in SQL Server.
ALL: It is optional. when we use ALL then the Select query will return all the matching rows from the table.
DISTINCT: It is optional. If you want to remove the duplicate rows from the result set then you need to use the DISTINCT keyword in the Select query. We will discuss the DISTINCT keyword in detail in our upcoming Videos
TOP (top_value): It is optional. If the TOP Clause is specified in the Select query, then it will return the top number of rows in the result set based on top_value. For example, if you specified as TOP (10) in the select query then it will return the top 10 rows from the full result set.
PERCENT: It is optional. If we specified the PERCENT Clause in the select query, then the top rows are retrieved based on the percentage of the total result set (as specified by the top_value). For example, if we specify TOP (10) PERCENT in the select query then it will return the top 10% of the full result set.
WITH TIES: It is optional. If WITH TIES specified in the select query, then the rows tied in the last place within the limited result set are returned.
Expressions: The columns or calculations that we wish to retrieve in the select list. Use * if you wish to select all columns.
Tables: The tables from which you want to retrieve the records. There should be at least one table in the FROM clause of the select query.
WHERE conditions: It is optional. The conditions that must be met for the records to be selected in the result set.
GROUP BY expressions: It is optional. It collects the records and then groups the records by one or more columns.
ORDER BY expression: It is optional and it is basically used to sort the records in ascending or descending order.
Example: Execute the below script to create the Gender and Person table
SELECT * FROM Gender
SELECT * FROM Person
Example: Select all fields from one table
Lets look at how to use a SQL Server SELECT query to select all fields from a table.
SELECT * FROM Person WHERE Age > 26 ORDER BY ID ASC
In this SQL Server SELECT statement example, weve used * to signify that we wish to select all the columns from the Person table where the Age is greater than 26. The result set is sorted by ID in ascending order.
Example: Select individual fields from one table
You can also use the SQL Select statement to select individual fields of a table.
This SQL Server Select example would return only the ID, Name, EmailID, and CITY from the Person table where the ID is less than 5 and the Age is greater than 25. The results are sorted by GenderID in descending order and then ID in ascending order.