The SQL SELECT Statement allows us to retrieve or fetch records from one or more (by using Oracle SQL Joins) tables from our database.
SQL SELECT Statement Syntax
SELECT column(s) FROM table_name(s) WHERE conditions;
SQL SELECT Statement Examples
Suppose we have a table named “Employee” with the data as shown below.
Employee_ID | Employee_Name | Salary | Department | Commission |
101 | Emp A | 10000 | Sales | 10 |
102 | Emp B | 20000 | IT | 20 |
103 | Emp C | 28000 | IT | 20 |
104 | Emp D | 30000 | Support | |
105 | Emp E | 32000 | Sales | 10 |
Now we will see what the SQL SELECT Statement does when used in different scenarios
SQL SELECT Statement – Using ‘*’ to view all records from a table
The SQL SELECT Statement can be used with the ‘*’ wildcard character to fetch all the columns and rows from a table.
For example, the below SQL SELECT Statement, once executed, will return all the data from the employee table.
SELECT * FROM employee;
The result of the above SQL SELECT Statement query will be:
Employee_ID | Employee_Name | Salary | Department | Commission |
101 | Emp A | 10000 | Sales | 10 |
102 | Emp B | 20000 | IT | 20 |
103 | Emp C | 28000 | IT | 20 |
104 | Emp D | 30000 | Support | |
105 | Emp E | 32000 | Sales | 10 |
Again, in the above SQL SELECT Statement query, we have used the ‘*’ wildcard character to view all the records from the employee table.
SQL SELECT Statement – Selecting individual fields from a table
We can use the SQL SELECT Statement to select and view only certain fields from a table.
For example, the below SQL SELECT Statement query will return “employee_id”, “employee_name” and “department” fields only from the employee table.
SELECT employee_id ,employee_name ,department FROM employee;
The result of the above SQL SELECT Statement query will be;
Employee_ID | Employee_Name | Department |
101 | Emp A | Sales |
102 | Emp B | IT |
103 | Emp C | IT |
104 | Emp D | Support |
105 | Emp E | Sales |
Here we can see that in the SQL SELECT Statement we have explicitly mentioned that we wish to see only the “employee_id”, “employee_name” and “department” columns from the employee table.
SQL SELECT Statement – Using WHERE Clause
The SQL SELECT Statement can be used with the SQL WHERE Clause to filter records based on requirements.
For example, the below SQL SELECT Statement will return “employee_name” and “Salary” of employees working in “Sales” department from the employee table.
SELECT employee_name ,salary FROM employee WHERE department='Sales';
The data returned by the above SQL SELECT Statement will be:
Employee_Name | Salary |
EMP A | 10000 |
EMP E | 32000 |
The column names after the SQL SELECT Statement mentions that we want to view the data only for “Employee_Name” and “Salary” columns for “Sales” department only from employee table.
SQL SELECT Statement – Selecting fields from several tables
The SQL SELECT Statement can fetch columns from many tables at once.
For example,
SELECT orders.order_id ,suppliers.supplier_name FROM suppliers ,orders WHERE suppliers.supplier_id = orders.supplier_id;
The above SQL SELECT Statement joins two database tables namely “Orders” and “Suppliers” and returns us the result based in condition that the value for “Supplier_Id” field is available in both the joined tables.
Learn more about Oracle SQL Joins.