find and solve || findandsolve.com
Please wait.....

SQL SELECT Statement Example

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

FirstNameLastNameAddressAge
SylviaNeupaneKathmandu10
DebinBhattraiPokhara23
RahulSharmaMirmee32
SamuraThapaButwal27
DevinRahutBiratnagar
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

FirstNameLastNameAddress
SylviaNeupaneKathmandu
DebinBhattraiPokhara
RahulSharmaMirmee
SamuraThapaButwal
DevinRahutBiratnagar


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

FirstNameLastNameAddressAge
SylviaNeupaneKathmandu10
DebinBhattraiPokhara23
RahulSharmaMirmee32
SamuraThapaButwal27
DevinRahutBiratnagar
19