PSORC-Oracle SQL 2-Queries
PSORC-Oracle SQL 2-Queries
SQL Queries
Muchake Brian
Phone: 0701178573
Email: bmuchake@gmail.com, bmuchake@cis.mak.ac.ug
Objectives
Discuss:
• Oracle Queries
• Oracle Select
• Oracle Insert
• Oracle Insert All
• Oracle Update
• Oracle Delete
• Truncate Table
Oracle Queries
• You can execute many queries in oracle database such as insert, update, delete, alter table, drop, create and
select.
1) Oracle Select Query
• Oracle select query is used to fetch records from database. For example:
• SELECT * from customers;
2) Oracle Insert Query
• Oracle insert query is used to insert records into table. For example:
• insert into customers values(101,'rahul','delhi');
• 3) Oracle Update Query
• Oracle update query is used to update records of a table. For example:
• update customers set name='bob', city='london' where id=101;
Oracle Queries [Cont’d]
4) Oracle Delete Query
•Oracle update query is used to delete records of a table from database. For example:
•delete from customers where id=101;
5) Oracle Truncate Query
•Oracle update query is used to truncate or remove records of a table. It doesn't remove structure.
For example:
•truncate table customers;
6) Oracle Drop Query
•Oracle drop query is used to drop a table or view. It doesn't have structure and data. For example:
drop table customers;
Oracle Queries [Cont’d]
• 7) Oracle Create Query
• Oracle create query is used to create a table, view, sequence, procedure
and function. For example:
• CREATE TABLE customers
• ( id number(10) NOT NULL,
• name varchar2(50) NOT NULL,
• city varchar2(50),
• CONSTRAINT customers_pk PRIMARY KEY (id)
• );
Oracle Queries [Cont’d]
• 8) Oracle Alter Query
• Oracle alter query is used to add, modify, delete or drop colums of a
table. Let's see a query to add column in customers table:
• ALTER TABLE customers
• ADD age varchar2(50);
Oracle SELECT Statement
• The Oracle SELECT statement is used to retrieve data from one or more than one
tables, object tables, views, object views etc.
Syntax
• SELECT expressions
• FROM tables
• WHERE conditions;
• 1) expressions: It specifies the columns or calculations that you want to retrieve.
• 2) tables: This parameter specifies the tables that you want to retrieve records
from. There must be at least one table within the FROM clause.
• 3) conditions: It specifies the conditions that must be followed for selection.
Oracle SELECT Statement [Cont’d]
• Select Example 1: select all fields
• Let's take an example to select all fields from an already created table
named customers
• SELECT * FROM customers;
• Output
Oracle SELECT Statement [Cont’d]
• Select Example 2: select specific fields
• Let's take an example to select specific fields from an already created
table named customers
• SELECT age, address, salary
• FROM customers
• WHERE age < 25
• AND salary > '20000'
• ORDER BY age ASC, salary DESC;
• Output
Oracle SELECT Statement [Cont’d]
• Select Example 3: select fields from multiple tables
• Let's take an example to select fields from multiple tables using joins
• SELECT customers.name, courses.trainer
• FROM courses
• INNER JOIN customers
• ON courses.course_id = course_id
• ORDER BY name;
• Output
Oracle SELECT Statement [Cont’d]
• Output
Oracle INSERT Statement
• In Oracle, INSERT statement is used to add a single record or multiple
records into the table.
Syntax: (Inserting a single record using the Values keyword):
• INSERT INTO table
• (column1, column2, ... column_n )
• VALUES
• (expression1, expression2, ... expression_n );
Oracle INSERT Statement [Cont’d]
Syntax: (Inserting multiple records using a SELECT statement):
• INSERT INTO table
• (column1, column2, ... column_n )
• SELECT expression1, expression2, ... expression_n
• FROM source_table
• WHERE conditions;
Parameters:
• 1) table: The table to insert the records into.
• 2) column1, column2, ... column_n: The columns in the table to insert values.
Oracle INSERT Statement [Cont’d]
• 3) expression1, expression2, ... expression_n: The values to assign to the columns in
the table. So column1 would be assigned the value of expression1, column2 would be
assigned the value of expression2, and so on.
• 4) source_table: The source table when inserting data from another table.
• 5) conditions: The conditions that must be met for the records to be inserted.
1. Oracle Insert Example: By VALUE keyword
• It is the simplest way to insert elements to a database by using VALUE keyword.
• Example:
• Consider here the already created suppliers table. Add a new row where the value of
supplier_id is 23 and supplier_name is Flipkart.
Oracle INSERT Statement [Cont’d]
• INSERT INTO suppliers
• (supplier_id, supplier_name)
• VALUES
• (50, 'Flipkart');
Oracle INSERT Statement [Cont’d]
2. Oracle Insert Example: By SELECT statement
•This method is used for more complicated cases of insertion. In this method insertion is done by
SELECT statement. This method is used to insert multiple elements.
•Example:
•In this method, we insert values to the "suppliers" table from "customers" table. Both tables are
already created with their respective columns.
•INSERT INTO suppliers
•(supplier_id, supplier_name)
•SELECT age, address
•FROM customers
•WHERE age > 20;
Oracle INSERT Statement [Cont’d]
• You can even check the number of rows that you want to insert by
following statement:
• SELECT count(*)
• FROM customers
• WHERE age > 20;
Oracle INSERT ALL statement
• The Oracle INSERT ALL statement is used to insert multiple rows with a
single INSERT statement. You can insert the rows into one table or
multiple tables by using only one SQL command.
Syntax
• INSERT ALL
• INTO table_name (column1, column2, column_n) VALUES (expr1, expr2,
expr_n)
• INTO table_name(column1, column2, column_n) VALUES (expr1, expr2,
expr_n)
• INTO table_name (column1, column2, column_n) VALUES (expr1, expr2,
expr_n)
• SELECT * FROM dual;
Oracle INSERT ALL statement [Cont’d]
Parameters
• 1) table_name: it specifies the table in which you want to insert your records.
• 2) column1, column2, column_n: this specifies the columns in the table to insert values.
• 3) expr1, expr2, expr_n: this specifies the values to assign to the columns in the table.
• Examples: Oracle INSERT ALL Example
• This example specifies how to insert multiple records in one table. Here we insert three rows into the
"suppliers" table.
• INSERT ALL
• INTO suppliers (supplier_id, supplier_name) VALUES (20, 'Google')
• INTO suppliers (supplier_id, supplier_name) VALUES (21, 'Microsoft')
• INTO suppliers (supplier_id, supplier_name) VALUES (22, 'Apple')
• SELECT * FROM dual;
•
Oracle INSERT ALL statement [Cont’d]
• Example: Oracle INSERT ALL Example: (Insert into multiple tables)
• The INSERT ALL statement can also be used to insert multiple rows into more than one table by
one command only.
• In the following example, we are going to insert records into the both "suppliers" and "customers"
tables.
• INSERT ALL
• INTO suppliers (supplier_id, supplier_name) VALUES (30, 'Google')
• INTO suppliers (supplier_id, supplier_name) VALUES (31, 'Microsoft')
• INTO customers (age, name, address) VALUES (29, 'Luca Warsi', 'New York')
• SELECT * FROM dual;
• Here, total 3 rows are inserted, 2 rows are inserted into the suppliers table and one row into the
customers table.
Oracle UPDATE Statement
• In Oracle, UPDATE statement is used to update the existing records in a table. You can update
a table in 2 ways.
• Traditional Update table method
Syntax:
• UPDATE table
• SET column1 = expression1,
• column2 = expression2,
• ...
• column_n = expression_n
• WHERE conditions;
•
Oracle UPDATE Statement [Cont’d]
Selecting records from another table
• Update Table by selecting records from another table
Syntax:
• UPDATE table1
• SET column1 = (SELECT expression1
• FROM table2
• WHERE conditions)
• WHERE conditions;
• Parameters:
• 1) column1, column2, ... column_n: It specifies the columns that you want to update.
• 2) expression1, expression2, ...expression_n: This specifies the values to assign to the column1, column2, ?.
column_n.
• 3) conditions: It specifies the conditions that must be fulfilled for execution of UPDATE statement.
•
Oracle UPDATE Statement [Cont’d]
Oracle Update Example: (Update single column)
• UPDATE suppliers
• SET supplier_name = 'Kingfisher'
• WHERE supplier_id = 2;
• This example will update the supplier_name as "Kingfisher" where "supplier_id" is 2.
Oracle Update Example: (Update multiple columns)
• The following example specifies how to update multiple columns in a table. In this example, two
columns supplier_name and supplier_address is updated by a single statement.
• UPDATE suppliers
• SET supplier_address = 'Agra',
• supplier_name = 'Bata shoes'
• WHERE supplier_id = 1;
Oracle UPDATE Statement [Cont’d]
Oracle Update Example: (By selecting records from another table)
• UPDATE customers
• SET name = (SELECT supplier_name
• FROM suppliers
• WHERE suppliers.supplier_name = customers.name)
• WHERE age < 25;
• Output:
• Here, the customers table is updated by fetching the data from "suppliers" table.
Oracle DELETE Statement
• In Oracle, DELETE statement is used to remove or delete a single record or multiple records from a table.
Syntax
• DELETE FROM table_name
• WHERE conditions;
Parameters
• 1) table_name: It specifies the table which you want to delete.
• 2) conditions: It specifies the conditions that must met for the records to be deleted.
Oracle Delete Example: On one condition
• DELETE FROM customers
• WHERE name = 'Sohan';
• This statement will delete all records from the customer table where name is "Sohan".
Oracle DELETE Statement [Cont’d]
• Oracle Delete Example: On multiple conditions
• DELETE FROM customers
• WHERE last_name = 'Maurya'
• AND customer_id > 2;
• This statement will delete all records from the customers table where
the last_name is "Maurya" and the customer_id is greater than 2.
Oracle Truncate Table
• In Oracle, TRUNCATE TABLE statement is used to remove all records from a table. It works same as
DELETE statement but without specifying a WHERE clause. It is generally used when you don't
have to worry about rolling back
• Once a table is truncated, it can't be rolled back. The TRUNCATE TABLE statement does not affect
any of the table’ s indexes, triggers or dependencies.
Syntax
• TRUNCATE TABLE [schema_name.]table_name
Parameters
• 1) schema_name: This parameter specifies the name of the schema that the table belongs to. It is
optional.
• 2) table_name: It specifies the table that you want to truncate.
Oracle Truncate Table [Cont’d]
Oracle TRUNCATE Table Example
• Consider a table named "customers" and execute the following query to truncate
this
• TRUNCATE TABLE customers;
• Output
• Now check the customers table, you will find that there is no data available in that
table. It is equally similar to DELETE TABLE statement in Oracle.
Oracle Truncate Table [Cont’d]
Oracle DELETE Table Example
• DELETE TABLE customers;
TRUNCATE TABLE vs DELETE TABLE
• Both the statements will remove the data from the "customers" table but
the main difference is that you can roll back the DELETE statement
whereas you can't roll back the TRUNCATE TABLE statement.
Summary
• Oracle Queries
• Oracle Select
• Oracle Insert
• Oracle Insert All
• Oracle Update
• Oracle Delete
• Truncate Table