T-SQL SELECT STATEMENT

In T-SQL, the SELECT statement is used to fetch the data from the database table that returns the data in form of the result. These tables are called result-set in SELECT STATEMENT.

Syntax of Select Statement:

Where column1, column2, column N are the fields of a table whose values we want to fetch. If we fetch all the fields available in this table, then we need to use the given syntax-

Example:

Consider the customer's table having the following records-

IDNAMEAGEADDRESSSalary
001Rahul23Kota20000.00
002Clinton22Mumbai15000.00
003Kamal31Delhi25000.00
004Chitra28Kanyakumari65000.00
005Santanu26Madhya Pradesh38500.00
006Savitri24Bhopal4500.00
007Manila30Indonesia15000.00

Following command is an example, which will fetch ID, Name and Salary fields of the customers available in CUSTOMERS table-

The above command will produce the following output.

IDNAMESALARY
001Rahul20000.00
002Clinton15000.00
003Kamal25000.00
004Chitra65000.00
005Santanu38500.00
006Savitri4500.00
007Manii15000.00

Example 2:

In this example we SELECT only NAME, AGE, and ADDRESS from the database CUSTOMERS.

Output:

NAMEAGEADDRESS
Rahul23Kota
Clinton22Mumbai
Kamal31Delhi
Chitra28Kanyakumari
Santanu26Madhya Pradesh
Savitri24Bhopal
Manila30Indonesia

Example 3:

OUTPUT:

NAMESalary
Rahul20000.00
Clinton15000.00
Kamal25000.00
Chitra65000.00
Santanu38500.00
Savitri4500.00
Manila15000.00

Populate One Table using the other one

We can populate data into a table through SELECT statement over another table. Another table has a set of fields, which is required to populate the first table.

The syntax of SELECT Statement is-