The SELECT statement isavery commonSQL statementused to retrieve data from a table and return it as a result table.These result sets are called result-sets.

Because SELECT is very common and used frequently when you are dealing with a database, make sure you have syntactical and SELECT statements.

select statement in sql

select statement in sql

Read More:The DROP TABLE or DELETE TABLE statement in SQL

Syntax SELECT statement in SQL:

SELECT cot1, cot2, cotN FROM TEN_BANG;

This isthe most basicSQLSELECT syntax, cot1, cot2, cotN are the fields that contain the values you want to retrieve in the table.If you want to retrieve all the table fields, just use the SELECT syntax shortened as follows:

SELECT * FROM TEN_BANG;

Example of SELECT statement in SQL:

We use the table NHANVIEN created in the previous post:

+----+----------+-----+-----------+---------+
| ID |   TEN    | YOUR|  ADDRESS|   LUONG | 
+----+----------+-----+-----------+---------+ 
| 1  | Thanh    |  24 | Haiphong  | 2000.00 | 
| 2  | Loan     |  26 | Hanoi     | 1500.00 | 
| 3  | Nga      |  24 | Hanam     | 2000.00 | 
| 4  | Mạnh     |  29 | Hue       | 6500.00 | 
| 5  | Huy      |  28 | Hatinh    | 8500.00 | 
| 6  | Cao      |  23 | HCM       | 4500.00 | 
| 7  | Lam      |  29 | Hanoi     | 15000.00| 
+----+----------+-----+-----------+---------+

Here, we will separate the ID, TEN and LUONG of employees in the NHANVIEN table.

SQL> SELECT ID, TEN, LUONG FROM NHANVIEN;

The result of the SELECT statement looks like this:

+----+----------+---------+
| ID |   TEN    |   LUONG | 
+----+----------+---------+ 
| 1  | Thanh    | 2000.00 | 
| 2  | Loan     | 1500.00 | 
| 3  | Nga      | 2000.00 | 
| 4  | Mạnh     | 6500.00 | 
| 5  | Huy      | 8500.00 | 
| 6  | Cao      | 4500.00 | 
| 7  | Lam      | 15000.00| 
+----+----------+---------+

If you want to get all the fields of the NHANVIEN table, just use the following command:

SQL> SELECT * FROM NHANVIEN;

Then you will get the results table as the original table NHANVIEN:

+----+----------+-----+-----------+---------+
| ID |   TEN    | TUOI|  DIACHI   |   LUONG | 
+----+----------+-----+-----------+---------+ 
| 1  | Thanh    |  24 | Haiphong  | 2000.00 | 
| 2  | Loan     |  26 | Hanoi     | 1500.00 | 
| 3  | Nga      |  24 | Hanam     | 2000.00 | 
| 4  | Mạnh     |  29 | Hue       | 6500.00 | 
| 5  | Huy      |  28 | Hatinh    | 8500.00 | 
| 6  | Cao      |  23 | HCM       | 4500.00 | 
| 7  | Lam      |  29 | Hanoi     | 15000.00| 
+----+----------+-----+-----------+---------+

Leave a Comment

Close