The SELECT statement in SQL is used to retrieve data from a database table.

We can use the SELECT statement to get data from the specified column(s) or all the columns.

SQL SELECT Syntax

The SELECT statement has the following syntax:

SELECT column1, column2, ...
FROM table_name;

SQL SELECT Statement Examples

Suppose we have a table called “Employees” with the following data:

+------------+-----------+----------+------------+
| EmployeeID | FirstName | LastName | Department |
+------------+-----------+----------+------------+
| 1          | Mark      | Otto     | Finance    |
| 2          | Jacob     | Thornton | IT         |
| 3          | Su        | Bird     | Marketing  |
| 4          | Sam       | Burger   | IT         |
+------------+-----------+----------+------------+

SELECT From a Column

To select “Department” column from the “Employees” table:

SELECT Department FROM Employees;

Output:

+------------+
| Department |
+------------+
| Finance    |
| IT         |
| Marketing  |
| IT         |
+------------+

SELECT From Multiple Columns

To select “Department” and “EmployeeID” columns from the “Employees” table:

SELECT EmployeeID, Department FROM Employees;

Output:

+------------+------------+
| EmployeeID | Department |
+------------+------------+
| 1          | Finance    |
| 2          | IT         |
| 3          | Marketing  |
| 4          | IT         |
+------------+------------+

SELECT * Statement

To select all the columns from the “Employees” table:

SELECT * FROM Employees;

Output:

+------------+-----------+----------+------------+
| EmployeeID | FirstName | LastName | Department |
+------------+-----------+----------+------------+
| 1          | Mark      | Otto     | Finance    |
| 2          | Jacob     | Thornton | IT         |
| 3          | Su        | Bird     | Marketing  |
| 4          | Sam       | Burger   | IT         |
+------------+-----------+----------+------------+

Filter Results Using WHERE Clause

We can filter the results from a SELECT query by using the WHERE clause.

For example, to select all employees in the “IT Department” use:

SELECT * FROM Employees WHERE Department='IT'

Output:

+------------+-----------+----------+------------+
| EmployeeID | FirstName | LastName | Department |
+------------+-----------+----------+------------+
| 2          | Jacob     | Thornton | IT         |
| 4          | Sam       | Burger   | IT         |
+------------+-----------+----------+------------+

Likewise, to select all employee Ids who are in the “IT Department” we use:

SELECT EmployeeID FROM Employees WHERE Department='IT'

Output:

+------------+
| EmployeeID |
+------------+
| 2          |
| 4          |
+------------+
See also