SQL Tutorial on SQL SELECT Query

the sql select statement is used to fetch the data from a database table which returns this data in the form of a result table. these result tables are called result-sets.

syntax

the basic syntax of the select statement is as follows −

select column1, column2, columnn from table_name;

here, column1, column2... are the fields of a table whose values you want to fetch. if you want to fetch all the fields available in the field, then you can use the following syntax.

select * from table_name;

example

consider the customers table having the following records −

+----+----------+-----+-----------+----------+
| id | name     | age | address   | salary   |
+----+----------+-----+-----------+----------+
|  1 | ramesh   |  32 | ahmedabad |  2000.00 |
|  2 | khilan   |  25 | delhi     |  1500.00 |
|  3 | kaushik  |  23 | kota      |  2000.00 |
|  4 | chaitali |  25 | mumbai    |  6500.00 |
|  5 | hardik   |  27 | bhopal    |  8500.00 |
|  6 | komal    |  22 | mp        |  4500.00 |
|  7 | muffy    |  24 | indore    | 10000.00 |
+----+----------+-----+-----------+----------+

the following code is an example, which would fetch the id, name and salary fields of the customers available in customers table.

sql> select id, name, salary from customers;

this would produce the following result −

+----+----------+----------+
| id | name     | salary   |
+----+----------+----------+
|  1 | ramesh   |  2000.00 |
|  2 | khilan   |  1500.00 |
|  3 | kaushik  |  2000.00 |
|  4 | chaitali |  6500.00 |
|  5 | hardik   |  8500.00 |
|  6 | komal    |  4500.00 |
|  7 | muffy    | 10000.00 |
+----+----------+----------+

if you want to fetch all the fields of the customers table, then you should use the following query.

sql> select * from customers;

this would produce the result as shown below.

+----+----------+-----+-----------+----------+
| id | name     | age | address   | salary   |
+----+----------+-----+-----------+----------+
|  1 | ramesh   |  32 | ahmedabad |  2000.00 |
|  2 | khilan   |  25 | delhi     |  1500.00 |
|  3 | kaushik  |  23 | kota      |  2000.00 |
|  4 | chaitali |  25 | mumbai    |  6500.00 |
|  5 | hardik   |  27 | bhopal    |  8500.00 |
|  6 | komal    |  22 | mp        |  4500.00 |
|  7 | muffy    |  24 | indore    | 10000.00 |
+----+----------+-----+-----------+----------+