Updated March 22, 2023
Introduction to Oracle Queries
Query is a type of language used in Oracle database to retrieve data from tables, manipulate the data (insert records into the tables present in the database, update the values of column/columns of a particular table and also delete the records of the tables stored in database), also it is popularly known as SQL or Structured query language (structured query because Oracle database is structured database since data is stored in the form of rows and columns).
Types of Oracle Queries
Now let us look at the types of Oracle Queries. We will go through each one of them one by one.
1. SELECT Query
This query is used when we want to retrieve the data from one or more tables. There is no data manipulation done when we execute this query. Let us understand the syntax with example.
- Expressions: It represents the columns that we want to retrieve. If we want all the columns we can use *
- Tables: Here we provide the name of the table or tables from where we want the data.
- Where Condition: This is optional. It is used when we want data to be retrieved based on certain conditions. If we use where condition then the data will be retrieved only if the condition is satisfied.
SELECT * FROM employees WHERE age > 32;
In this example, we are selecting all fields (as we have used *) where the age is greater than thirty-two (the where condition states that age should be greater than thirty-two).
Now we will see how to select from multiple tables the first example was to select from only one table.
SELECT employees.employeeid, vehicles.name FROM employees INNER JOIN vehicles ON employees.vehicleid = vehicles.vehicleid ORDER BY employeeid;
In the second example, the select statement joins two tables and gives us the employee id from table employees and vehicle names from table vehicles based on the vehicle id.
2. INSERT Query
As the name suggests this query is used to add single or multiple records in the table. It causes data manipulation in the table. Let us understand by syntax and example.
Table (column1, column2, …., columnn1)
VALUES (value 1, value 2, …., value n1)
Table (column1, column2, …., columnn1)
SELECT(value 1, value 2, …., value n1)
FROM source table
- table: Name of the table in which data is to insert
- column1, column2, …., column1: These are the name of columns in which values are to be inserted.
- value 1, value 2, …., value n: Values or expressions to be inserted in the aforementioned columns.
- source table: The table from where data will be inserted
- WHERE condition: Optional clause, It is required if data is to be inserted based on some condition.
Now let us go through some examples.
INSERT INTO employees
(employeeid, name, age)
VALUES(“AB005”, ”Nilanjan”, 27);
In the above example, we are inserting a single record into the already created employe table.
INSERT INTO CUSTOMERS
(customerid, name, age)
SELECT employeeid, name, age
FROM employees WHERE age>25;
In the above example, we are inserting records into the customer’s table from the employee table where age in employees table is greater than 25.
3. UPDATE Query
This query is used to update existing records in a table that is present in the oracle database. We can use this query two ways either directly giving the value to update or using a select statement to get the value and then update. We will understand it further using syntax and examples.
SET column1 = expression1,
column2 = expression2,
column3 = expression3,
columnn1 = expressionn1
Using Select Statement
SET column1 = SELECT expression1,
FROM table2 [where conditions])
- [Column1…columnn1]: It represents the columns whose values we want to update.
- [expression1…..expressionn1]: It represents the values that we want to assign to the respective columns.
- WHERE conditions: It specifies the condition which has to be fulfilled for the update to take place.
In this first example, we are directly providing the value to be updated in the column based on a condition.
SET vehicle= (SELECT name FROM vehicles
WHERE vehicleid =”1254”)
In this second example, we are providing the value by retrieving it from another table using a select query.
4. DELETE Query
This query is used to delete existing records from the table. One important point to keep in mind here is that if you want to delete records or record based on condition then we have to use WHERE clause or else it will delete all the records from the table.
DELETE FROM table
- Table: It is for the name of the table
- [Condition]: The records which satisfy this condition will get deleted.
DELETE FROM employees
DELETE FROM employees;
In the first example, only the record with id ‘AD003’ gets deleted while in the second example the employees’ table would not have any record.
5. TRUNCATE Query
This query is also used to delete records from an existing table. The difference between delete and truncate is DELETE is DML command whereas TRUNCATE is DDL which means TRUNCATE query upon execution cannot be rolled back. It can be rolled back only if it is wrapped in a transaction.
TRUNCATE TABLE table name;
TRUNCATE table employees;
Once we execute the above TRUNCATE query it deletes all records from the existing employees’ table.
This is a guide to Oracle Queries. Here we discuss what is a query in general with respect to database operations. The different types of oracle queries that are present and how to use them. You may also look at the following articles to learn more –