This Oracle tutorial explains how to use the Oracle SELECT statement with syntax, examples, and practice exercises.
The Oracle SELECT statement is used to retrieve records from one or more tables in an Oracle database.
The syntax for the SELECT statement in Oracle/PLSQL is:
The columns or calculations that you wish to retrieve. Use * if you wish to select all columns.
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
Optional. The conditions that must be met for the records to be selected. If no conditions are provided, then all records will be selected.
Let's look at how to use an Oracle SELECT query to select all fields from a table.
In this Oracle SELECT statement example, we've used * to signify that we wish to select all fields from the homes table where the number of bathrooms is greater than or equal to 2. The result set is sorted by home_type in ascending order.
You can also use the Oracle SELECT statement to select individual fields from the table, as opposed to all fields from the table.
For example:
This Oracle SELECT example would return only the home_id, home_type, and bathrooms fields from the homes table where the home_id is less than 500 and the home_type is 'two-storey'. The results are sorted by home_type in ascending order and then bathrooms in descending order.
You can also use the Oracle SELECT statement to retrieve fields from multiple tables by using a join.
This Oracle SELECT example joins two tables together to gives us a result set that displays the home_id and customer_name fields where the customer_id value matches in both the customers and homes table. The results are sorted by home_id in ascending order.
Based on the contacts table below, select all fields from the contacts table whose last_name is 'Smith', contact_id is greater than or equal 1000 and contact_id is less than or equal to 2000 (no sorting is required):
The following Oracle SELECT statement would select these records from the employees table:
Or you could write the solution using the BETWEEN clause as follows: