The SELECT Statement in SQL Server is used to select or retrieve records from one or more tables in the SQL database. The data returned is stored in a result table, called the result-set.
The SELECT statement is the most used in Structured Query Language (SQL). SQL SELECT is mainly used to access or retrieve the records from one or more tables and we can include also UNION statements and subqueries.
In SQL, you can retrieve data from multiple tables using SELECT statements with multiple tables, resulting in a CROSS JOIN of all the tables. If we retrieve data from multiple tables need to use join.
Syntax
The basic syntax of the SELECT statement as given below
SELECT <column1>, <column2>, ... FROM <TableName>
Here, column1, column2... are the table attribute name whose values you want to fetch. If we want to fetch all the fields from the table, then you can use the following syntax as given below.
SELECT * FROM <YourTableName>;
For Example
Consider the EmployeeInfo table having the following records as given below
FirstName | LastName | Address | Age |
Sylvia | Neupane | Kathmandu | 10 |
Debin | Bhattrai | Pokhara | 23 |
Rahul | Sharma | Mirmee | 32 |
Samura | Thapa | Butwal | 27 |
Devin | Rahut | Biratnagar | 19 |
The following code is an example, which would fetch the FirstName, LastName, and Address fields of the Employee available in the EmployeeInfo table.
SELECT FirstName,LastName,Address FROM EmployeeInfo
This would produce the following result as given below
FirstName | LastName | Address |
Sylvia | Neupane | Kathmandu |
Debin | Bhattrai | Pokhara |
Rahul | Sharma | Mirmee |
Samura | Thapa | Butwal |
Devin | Rahut | Biratnagar |
If you want to fetch all the fields from EmployeeInfo table, then you should use the following query as given below
SELECT * FROM EmployeeInfo;
This would produce the result given below
FirstName | LastName | Address | Age |
Sylvia | Neupane | Kathmandu | 10 |
Debin | Bhattrai | Pokhara | 23 |
Rahul | Sharma | Mirmee | 32 |
Samura | Thapa | Butwal | 27 |
Devin | Rahut | Biratnagar | 19 |