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-
ID | NAME | AGE | ADDRESS | Salary |
---|---|---|---|---|
001 | Rahul | 23 | Kota | 20000.00 |
002 | Clinton | 22 | Mumbai | 15000.00 |
003 | Kamal | 31 | Delhi | 25000.00 |
004 | Chitra | 28 | Kanyakumari | 65000.00 |
005 | Santanu | 26 | Madhya Pradesh | 38500.00 |
006 | Savitri | 24 | Bhopal | 4500.00 |
007 | Manila | 30 | Indonesia | 15000.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.
ID | NAME | SALARY |
---|---|---|
001 | Rahul | 20000.00 |
002 | Clinton | 15000.00 |
003 | Kamal | 25000.00 |
004 | Chitra | 65000.00 |
005 | Santanu | 38500.00 |
006 | Savitri | 4500.00 |
007 | Manii | 15000.00 |
Example 2:
In this example we SELECT only NAME, AGE, and ADDRESS from the database CUSTOMERS.
Output:
NAME | AGE | ADDRESS |
---|---|---|
Rahul | 23 | Kota |
Clinton | 22 | Mumbai |
Kamal | 31 | Delhi |
Chitra | 28 | Kanyakumari |
Santanu | 26 | Madhya Pradesh |
Savitri | 24 | Bhopal |
Manila | 30 | Indonesia |
Example 3:
OUTPUT:
NAME | Salary |
---|---|
Rahul | 20000.00 |
Clinton | 15000.00 |
Kamal | 25000.00 |
Chitra | 65000.00 |
Santanu | 38500.00 |
Savitri | 4500.00 |
Manila | 15000.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-