SQL SELECT Statement
SELECT command is the main SQL to query a database.
Syntax
SELECT [ ALL | DISTINCT | TOP ] column_name [ AS alias_name]
FROM table_1 [, table_2, ... , table_n ]
[ JOIN join_condition]
[ WHERE search_condition ]
[ GROUP BY group_by_expression ]
[ HAVING search_condition ]
[ ORDER BY order_expression [ ASC | DESC] ]
A simple table (Employee)
ID |
Surname |
Name |
Age |
Salary |
City |
1 |
Datson |
William |
35 |
1800 |
Rome |
2 |
Arric |
John |
28 |
1300 |
Berlin |
3 |
Veras |
Sahara |
39 |
1500 |
Vienna |
4 |
Santos |
Julius |
40 |
1700 |
Paris |
Some examples:
SELECT * FROM Employee
Returns the following result:
ID |
Surname |
Name |
Age |
Salary |
City |
1 |
Datson |
William |
35 |
1800 |
Rome |
2 |
Arric |
John |
28 |
1300 |
Berlin |
3 |
Veras |
Sahara |
39 |
1500 |
Vienna |
4 |
Santos |
Julius |
40 |
1700 |
Paris |
An example with alias and data sorting
SELECT Surname AS Employee, City
FROM Employee
ORDER BY Surname
Returns the following result:
Employee |
City |
Arric |
Berlin |
Datson |
Rome |
Santos |
Paris |
Veras |
Vienna |
Two examples with a selection condition:
SELECT * FROM Employee
WHERE Age > 30
Returns the following result:
ID |
Surname |
Name |
Age |
Salary |
City |
1 |
Datson |
William |
35 |
1800 |
Rome |
3 |
Veras |
Sahara |
39 |
1500 |
Vienna |
4 |
Santos |
Julius |
40 |
1700 |
Paris |
SELECT * FROM Employee
WHERE Age > 30 AND Salary < 1600
Returns the following result:
ID |
Surname |
Name |
Age |
Salary |
City |
3 |
Veras |
Sahara |
39 |
1500 |
Vienna |
Category: SQL (Structured Query Language)