SlideShare a Scribd company logo
Basics of Oracle
Priya Goyal
What is Oracle
• Oracle database is a relational database management system. It is
known as Oracle database, OracleDB or simply Oracle. It is produced
and marketed by Oracle Corporation.
• Oracle database is the first database designed for enterprise grid
computing. The enterprise grid computing provides the most flexible
and cost effective way to manage information and applications.
Oracle CREATE TABLE
• CREATE TABLE statement is used to create a new table in the database.
• Syntax:
CREATE TABLE table_name
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
column_n datatype [ NULL | NOT NULL ]
);
*Every column should either be defined as "NULL" or "NOT NULL". In the case, the value is left blank; it is treated as "NULL" as
default.
• Example
CREATE TABLE customers
(
customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
);
**In Oracle, total number of columns cannot be more than 32.
CREATE TABLE Example with primary key
Example 1:
CREATE TABLE customers
(
customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50),
CONSTRAINT customers_pk PRIMARY KEY (customer_id, customer_name)
);
*In the example above there is only ONE PRIMARY KEY (customers_pk).
However, the VALUE of the primary key is made up of TWO COLUMNS
(customer_id + customer_name).
Example 2:
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
*A primary key is a single field or combination of fields that contains a unique record. It must be filled. None of
the field of primary key can contain a null value. A table can have only one primary key.
CREATE TABLE AS Statement
1)copying all columns of another table
CREATE TABLE new_table
AS (SELECT * FROM old_table);
CREATE TABLE newcustomers
AS (SELECT * FROM customers
WHERE customer_id < 5000);
2)copying selected columns of another table
CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n
FROM old_table);
CREATE TABLE newcustomers2
AS (SELECT customer_id, customer_name
FROM customers
WHERE customer_id < 5000);
3)copying selected columns from multiple tables
CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n
FROM old_table_1, old_table_2, ... old_table_n);
Let's take an example: Consider that you have already created two tables “Customer" and “Customers".The
table “Customer" has three columns customer_id, customer_name and customer_city. The second table
“Customers" has also three columns customers_id, customers_name and customers_city.
• In the following example, we will create a table name "newcustomer" form copying
columns from both tables.
Example:
CREATE TABLE newcustomer
AS (SELECT customer.customer_id, customer.customer_city, customers.customers_name
FROM customer, customers
WHERE customer.customer_id =customers.customers_id
AND customer.customer_id < 5000);
ALTER TABLE
• ALTER TABLE statement specifies how to add, modify, drop or delete
columns in a table. It is also used to rename a table.
ALTER TABLE table_name
ADD column_name column-definition;
• Consider that already existing table customers. Now, add a new column
customer_age into the table customers.
ALTER TABLE customers
ADD customer_age varchar2(50);
1)Add multiple columns in the existing
table
ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,
column_n column_definition);
Example
ALTER TABLE customers
ADD (customer_type varchar2(50),
customer_address varchar2(50));
2)Modify column of a table
ALTER TABLE table_name
MODIFY column_name column_type;
Example:
ALTER TABLE customers
MODIFY customer_name varchar2(100) not null;
3)Modify multiple columns of a
table
ALTER TABLE table_name
MODIFY (column_1 column_type,
column_2 column_type,
column_n column_type);
ALTER TABLE customers
MODIFY (customer_name varchar2(100) not null,
city varchar2(100));
4)Drop column of a table
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE customers
DROP COLUMN customer_name;
5)rename column of a table
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
ALTER TABLE customers
RENAME COLUMN customer_name to cname;
6)rename table
ALTER TABLE table_name
RENAME TO new_table_name;
ALTER TABLE customers
RENAME TO retailers;
DROP TABLE Statement
DROP TABLE statement is used to remove or delete a table
from the Oracle database.
DROP [schema_name].TABLE table_name
[ CASCADE CONSTRAINTS ]
[ PURGE ];
Parameters
schema_name: It specifies the name of the schema that owns the table.
table_name: It specifies the name of the table which you want to remove from the
Oracle database.
CASCADE CONSTRAINTS: It is optional. If specified, it will drop all referential
integrity constraints as well.
PURGE: It is also optional. If specified, the table and its dependent objects are
placed in the recycle bin and can’t be recovered.
DROP TABLE customers;
DROP TABLE customers PURGE
This statement will drop the table called customers and issue a PURGE so that the
space associated with the customers table is released and the customers table is
not placed in recycle bin. So, it is not possible to recover that table if required.
SELECT Statement
• The Oracle SELECT statement is used to retrieve data from one or more than one tables, object tables, views,
object views etc.
SELECT expressions
FROM tables
WHERE conditions;
1)select all fields
SELECT *
FROM customers;
2)select specific fields
SELECT age, address, salary
FROM customers
WHERE age < 25
AND salary > '20000'
ORDER BY age ASC, salary DESC;
3)select fields from multiple tables (JOIN)
SELECT customers.name, courses.trainer
FROM courses
INNER JOIN customers
ON courses.course_id = course_id
ORDER BY name;
Insert Statement
1) Inserting a single record using the Values keyword):
INSERT INTO table
(column1, column2, ... column_n )
VALUES
(expression1, expression2, ... expression_n );
INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(50, 'Flipkart');
2) Inserting multiple records using a SELECT statement):
INSERT INTO table
(column1, column2, ... column_n )
SELECT expression1, expression2, ... expression_n
FROM source_table
WHERE conditions;
INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT age, address
FROM customers
WHERE age > 20;
You can even check the number of rows that you want to insert by following statement:
SELECT count(*)
FROM customers
WHERE age > 20;
INSERT statement is used to add a single record or multiple records into the table.
INSERT ALL statement
• INSERT ALL statement is used to insert the rows into one table or multiple tables by using
only one SQL command.
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;
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;
UPDATE Statement
1) Update column
UPDATE table_name
SET column1 = expression1,
column2 = expression2,
...
column_n = expression_n
WHERE conditions;
UPDATE suppliers
SET supplier_name = 'Kingfisher',
supplier_name = 'Bata shoes'
WHERE supplier_id = 2;
2) Update Table by selecting rocords from another table
UPDATE table_name
SET column1 = (SELECT expression1
FROM table2
WHERE conditions)
WHERE conditions;
UPDATE customers
SET name = (SELECT supplier_name
FROM suppliers
WHERE suppliers.supplier_name = customers.name)
WHERE age < 25;
DELETE Statement
In Oracle, DELETE statement is used to remove or delete a single record or multiple records from a
table.
DELETE FROM table_name
WHERE conditions;
1) Delete on one condition
DELETE FROM customers
WHERE name = 'Sohan';
2) Delete On multiple conditions
DELETE FROM customers
WHERE last_name = 'Maurya'
AND customer_id > 2;
TRUNCATE TABLE
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
TRUNCATE TABLE [schema_name.]table_name
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.
TRUNCATE TABLE customers;
DELETE Table
Delete table is used to delete the table
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.
DISTINCT Clause
Oracle DISTINCT clause is used to remove the duplicate records from the result set. It is only used
with SELECT statement.
SELECT DISTINCT expressions
FROM tables
WHERE conditions;
SELECT DISTINCT name, age, salary
FROM customers
WHERE age >= '60';
FROM Clause
• FROM clause is a mandatory clause in SELECT expression. It specifies the tables from which data is
to be retrieved.
Syntax:
FROM table_name...
Expressions...
SELECT *
FROM customers
WHERE salary >= 20000
ORDER BY salary ASC;
FROM Clause Example: (with two tables)
Inner Join example:
Let's take two tables "suppliers" and "order1".
Suppliers:
Order1:
Execute the following query:
SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number
FROM suppliers
INNER JOIN order1
ON suppliers.supplier_id = order1.supplier_id;
ORDER BY Clause
ORDER BY Clause is used to sort or re-arrange the records in the result set.
The ORDER BY clause is only used with SELECT statement.
SELECT expressions
FROM tables
WHERE conditions
ORDER BY expression [ ASC | DESC ];
ASC: optional parameter, used to sort records in ascending
order.
DESC: optional parameter, used to sort records in descending
order.
SELECT *
FROM supplier
ORDER BY last_name;
SELECT *
FROM supplier
ORDER BY last_name DESC;
GROUP BY Clause
GROUP BY clause is used with SELECT statement to collect data from multiple records and group the
results by one or more columns.
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
WHERE conditions
GROUP BY expression1, expression2, ... expression_n;
SELECT item, SUM(sale) AS "Total sales"
FROM salesdepartment
GROUP BY item;
HAVING Clause
• HAVING Clause is used with GROUP BY Clause to restrict the groups of returned rows where
condition is TRUE.
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
WHERE conditions
GROUP BY expression1, expression2, ... expression_n
HAVING having_condition;
• aggregate_function: It specifies the aggregate functions i.e. SUM, COUNT, MIN, MAX or AVG
functions.
• aggregate_expression: It specifies the column or expression on that the aggregate function is
based on.
• having_conditions: It specifies the conditions that are applied only to the aggregated results to
restrict the groups of returned rows.
1) With GROUP BY SUM function 2) With GROUP BY COUNT function
SELECT item, SUM(sale) AS "Total sales"
FROM salesdepartment
GROUP BY item
HAVING SUM(sale) < 1000; SELECT state, COUNT(*) AS "Number of customers"FROM customers
WHERE salary > 10000
GROUP BY state
HAVING COUNT(*) >= 2;
3) with GROUP BY MIN function 4) with GROUP BY MAX function
SELECT department,
MIN(salary) AS "Lowest salary"
FROM employees
GROUP BY department
HAVING MIN(salary) < 15000;
SELECT department,
MAX(salary) AS "Highest salary"
FROM employees
GROUP BY department
HAVING MAX(salary) > 30000;

More Related Content

PDF
Oracle SQL Basics
Dhananjay Goel
 
PPTX
Intro & Applications of Discrete Math
Bilal Khan
 
PPTX
Cursors
Priyanka Yadav
 
PPTX
What Is DevOps?
Soumya De
 
PPTX
Introduction to Oracle Database
puja_dhar
 
DOC
Oracle sql material
prathap kumar
 
PPTX
Packages in PL/SQL
Pooja Dixit
 
PPTX
Introduction to SQL
Ehsan Hamzei
 
Oracle SQL Basics
Dhananjay Goel
 
Intro & Applications of Discrete Math
Bilal Khan
 
What Is DevOps?
Soumya De
 
Introduction to Oracle Database
puja_dhar
 
Oracle sql material
prathap kumar
 
Packages in PL/SQL
Pooja Dixit
 
Introduction to SQL
Ehsan Hamzei
 

What's hot (20)

PPT
Introduction to sql
VARSHAKUMARI49
 
PPTX
Sql commands
Pooja Dixit
 
PPT
SQL Tutorial - Basic Commands
1keydata
 
PPT
1 - Introduction to PL/SQL
rehaniltifat
 
PDF
SQL Queries - DDL Commands
ShubhamBauddh
 
PDF
SQL Overview
Stewart Rogers
 
PPTX
introdution to SQL and SQL functions
farwa waqar
 
PPT
Introduction to structured query language (sql)
Sabana Maharjan
 
PPTX
Oracle database introduction
Mohammad Javad Beheshtian
 
PDF
Sql tutorial
Rumman Ansari
 
PPTX
Sql and Sql commands
Knowledge Center Computer
 
PPTX
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
PPTX
SQL
Shyam Khant
 
PPTX
DDL And DML
pnp @in
 
PPTX
Inner join and outer join
Nargis Ehsan
 
PPT
Mysql
TSUBHASHRI
 
PPT
SQL.ppt
Ranjit273515
 
PPTX
Sql queries presentation
NITISH KUMAR
 
PPTX
SQL Basics
Hammad Rasheed
 
PPT
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
Introduction to sql
VARSHAKUMARI49
 
Sql commands
Pooja Dixit
 
SQL Tutorial - Basic Commands
1keydata
 
1 - Introduction to PL/SQL
rehaniltifat
 
SQL Queries - DDL Commands
ShubhamBauddh
 
SQL Overview
Stewart Rogers
 
introdution to SQL and SQL functions
farwa waqar
 
Introduction to structured query language (sql)
Sabana Maharjan
 
Oracle database introduction
Mohammad Javad Beheshtian
 
Sql tutorial
Rumman Ansari
 
Sql and Sql commands
Knowledge Center Computer
 
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
DDL And DML
pnp @in
 
Inner join and outer join
Nargis Ehsan
 
Mysql
TSUBHASHRI
 
SQL.ppt
Ranjit273515
 
Sql queries presentation
NITISH KUMAR
 
SQL Basics
Hammad Rasheed
 
Restricting and Sorting Data - Oracle Data Base
Salman Memon
 
Ad

Viewers also liked (7)

DOCX
Penentuan kadar ca dan mg serta turbiditas
UIN Alauddin Makassar
 
PPT
Java Script ppt
Priya Goyal
 
PDF
Jsp
Priya Goyal
 
PPTX
Oracle Database | Computer Science
Transweb Global Inc
 
PPTX
Introduction to oracle database (basic concepts)
Bilal Arshad
 
PPT
Dbms
sevtap87
 
PDF
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
Beat Signer
 
Penentuan kadar ca dan mg serta turbiditas
UIN Alauddin Makassar
 
Java Script ppt
Priya Goyal
 
Oracle Database | Computer Science
Transweb Global Inc
 
Introduction to oracle database (basic concepts)
Bilal Arshad
 
Dbms
sevtap87
 
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
Beat Signer
 
Ad

Similar to Oraclesql (20)

PPTX
Creating database using sql commands
Belle Wx
 
DOCX
Query
Raj Devaraj
 
PPT
Les10
arnold 7490
 
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
PDF
Structure query language, database course
yunussufyan2024
 
PDF
full detailled SQL notesquestion bank (1).pdf
yvpachorib23
 
PPTX
SQl data base management and design
franckelsania20
 
PPTX
Introduction to sql new
SANTOSH RATH
 
PPTX
MySQL Essential Training
HudaRaghibKadhim
 
PPTX
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
aidenreji
 
PPTX
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
PPTX
data base programming chapter2 29 slides
nights1988
 
PPTX
SQL command practical power point slides, which help you in learning sql.pptx
macivem311
 
PPTX
Lab
neelam_rawat
 
PDF
Database Management System 1
Swapnali Pawar
 
PPTX
SQL Class Note By Amit Maity PowerPoint Presentation
maitypradip938
 
PDF
SQL Beginners anishurrehman.cloud.pdf
AnishurRehman1
 
Creating database using sql commands
Belle Wx
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
Structure query language, database course
yunussufyan2024
 
full detailled SQL notesquestion bank (1).pdf
yvpachorib23
 
SQl data base management and design
franckelsania20
 
Introduction to sql new
SANTOSH RATH
 
MySQL Essential Training
HudaRaghibKadhim
 
MySqL_n.pptx edshdshfbhjbdhcbjdchdchjcdbbjd
aidenreji
 
SQL DATABASE MANAGAEMENT SYSTEM FOR CLASS 12 CBSE
sdnsdf
 
data base programming chapter2 29 slides
nights1988
 
SQL command practical power point slides, which help you in learning sql.pptx
macivem311
 
Database Management System 1
Swapnali Pawar
 
SQL Class Note By Amit Maity PowerPoint Presentation
maitypradip938
 
SQL Beginners anishurrehman.cloud.pdf
AnishurRehman1
 

Recently uploaded (20)

PDF
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPT
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
AI-Driven IoT-Enabled UAV Inspection Framework for Predictive Maintenance and...
ijcncjournal019
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Packaging Tips for Stainless Steel Tubes and Pipes
heavymetalsandtubes
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Information Retrieval and Extraction - Module 7
premSankar19
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 

Oraclesql

  • 2. What is Oracle • Oracle database is a relational database management system. It is known as Oracle database, OracleDB or simply Oracle. It is produced and marketed by Oracle Corporation. • Oracle database is the first database designed for enterprise grid computing. The enterprise grid computing provides the most flexible and cost effective way to manage information and applications.
  • 3. Oracle CREATE TABLE • CREATE TABLE statement is used to create a new table in the database. • Syntax: CREATE TABLE table_name ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... column_n datatype [ NULL | NOT NULL ] ); *Every column should either be defined as "NULL" or "NOT NULL". In the case, the value is left blank; it is treated as "NULL" as default. • Example CREATE TABLE customers ( customer_id number(10) NOT NULL, customer_name varchar2(50) NOT NULL, city varchar2(50) ); **In Oracle, total number of columns cannot be more than 32.
  • 4. CREATE TABLE Example with primary key Example 1: CREATE TABLE customers ( customer_id number(10) NOT NULL, customer_name varchar2(50) NOT NULL, city varchar2(50), CONSTRAINT customers_pk PRIMARY KEY (customer_id, customer_name) ); *In the example above there is only ONE PRIMARY KEY (customers_pk). However, the VALUE of the primary key is made up of TWO COLUMNS (customer_id + customer_name). Example 2: CREATE TABLE Persons ( P_Id int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) *A primary key is a single field or combination of fields that contains a unique record. It must be filled. None of the field of primary key can contain a null value. A table can have only one primary key.
  • 5. CREATE TABLE AS Statement 1)copying all columns of another table CREATE TABLE new_table AS (SELECT * FROM old_table); CREATE TABLE newcustomers AS (SELECT * FROM customers WHERE customer_id < 5000); 2)copying selected columns of another table CREATE TABLE new_table AS (SELECT column_1, column2, ... column_n FROM old_table); CREATE TABLE newcustomers2 AS (SELECT customer_id, customer_name FROM customers WHERE customer_id < 5000);
  • 6. 3)copying selected columns from multiple tables CREATE TABLE new_table AS (SELECT column_1, column2, ... column_n FROM old_table_1, old_table_2, ... old_table_n); Let's take an example: Consider that you have already created two tables “Customer" and “Customers".The table “Customer" has three columns customer_id, customer_name and customer_city. The second table “Customers" has also three columns customers_id, customers_name and customers_city. • In the following example, we will create a table name "newcustomer" form copying columns from both tables. Example: CREATE TABLE newcustomer AS (SELECT customer.customer_id, customer.customer_city, customers.customers_name FROM customer, customers WHERE customer.customer_id =customers.customers_id AND customer.customer_id < 5000);
  • 7. ALTER TABLE • ALTER TABLE statement specifies how to add, modify, drop or delete columns in a table. It is also used to rename a table. ALTER TABLE table_name ADD column_name column-definition; • Consider that already existing table customers. Now, add a new column customer_age into the table customers. ALTER TABLE customers ADD customer_age varchar2(50);
  • 8. 1)Add multiple columns in the existing table ALTER TABLE table_name ADD (column_1 column-definition, column_2 column-definition, column_n column_definition); Example ALTER TABLE customers ADD (customer_type varchar2(50), customer_address varchar2(50)); 2)Modify column of a table ALTER TABLE table_name MODIFY column_name column_type; Example: ALTER TABLE customers MODIFY customer_name varchar2(100) not null;
  • 9. 3)Modify multiple columns of a table ALTER TABLE table_name MODIFY (column_1 column_type, column_2 column_type, column_n column_type); ALTER TABLE customers MODIFY (customer_name varchar2(100) not null, city varchar2(100)); 4)Drop column of a table ALTER TABLE table_name DROP COLUMN column_name; ALTER TABLE customers DROP COLUMN customer_name;
  • 10. 5)rename column of a table ALTER TABLE table_name RENAME COLUMN old_name to new_name; ALTER TABLE customers RENAME COLUMN customer_name to cname; 6)rename table ALTER TABLE table_name RENAME TO new_table_name; ALTER TABLE customers RENAME TO retailers;
  • 11. DROP TABLE Statement DROP TABLE statement is used to remove or delete a table from the Oracle database. DROP [schema_name].TABLE table_name [ CASCADE CONSTRAINTS ] [ PURGE ]; Parameters schema_name: It specifies the name of the schema that owns the table. table_name: It specifies the name of the table which you want to remove from the Oracle database. CASCADE CONSTRAINTS: It is optional. If specified, it will drop all referential integrity constraints as well. PURGE: It is also optional. If specified, the table and its dependent objects are placed in the recycle bin and can’t be recovered. DROP TABLE customers; DROP TABLE customers PURGE This statement will drop the table called customers and issue a PURGE so that the space associated with the customers table is released and the customers table is not placed in recycle bin. So, it is not possible to recover that table if required.
  • 12. SELECT Statement • The Oracle SELECT statement is used to retrieve data from one or more than one tables, object tables, views, object views etc. SELECT expressions FROM tables WHERE conditions; 1)select all fields SELECT * FROM customers; 2)select specific fields SELECT age, address, salary FROM customers WHERE age < 25 AND salary > '20000' ORDER BY age ASC, salary DESC; 3)select fields from multiple tables (JOIN) SELECT customers.name, courses.trainer FROM courses INNER JOIN customers ON courses.course_id = course_id ORDER BY name;
  • 13. Insert Statement 1) Inserting a single record using the Values keyword): INSERT INTO table (column1, column2, ... column_n ) VALUES (expression1, expression2, ... expression_n ); INSERT INTO suppliers (supplier_id, supplier_name) VALUES (50, 'Flipkart'); 2) Inserting multiple records using a SELECT statement): INSERT INTO table (column1, column2, ... column_n ) SELECT expression1, expression2, ... expression_n FROM source_table WHERE conditions; INSERT INTO suppliers (supplier_id, supplier_name) SELECT age, address FROM customers WHERE age > 20; You can even check the number of rows that you want to insert by following statement: SELECT count(*) FROM customers WHERE age > 20; INSERT statement is used to add a single record or multiple records into the table.
  • 14. INSERT ALL statement • INSERT ALL statement is used to insert the rows into one table or multiple tables by using only one SQL command. 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; 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;
  • 15. UPDATE Statement 1) Update column UPDATE table_name SET column1 = expression1, column2 = expression2, ... column_n = expression_n WHERE conditions; UPDATE suppliers SET supplier_name = 'Kingfisher', supplier_name = 'Bata shoes' WHERE supplier_id = 2; 2) Update Table by selecting rocords from another table UPDATE table_name SET column1 = (SELECT expression1 FROM table2 WHERE conditions) WHERE conditions; UPDATE customers SET name = (SELECT supplier_name FROM suppliers WHERE suppliers.supplier_name = customers.name) WHERE age < 25;
  • 16. DELETE Statement In Oracle, DELETE statement is used to remove or delete a single record or multiple records from a table. DELETE FROM table_name WHERE conditions; 1) Delete on one condition DELETE FROM customers WHERE name = 'Sohan'; 2) Delete On multiple conditions DELETE FROM customers WHERE last_name = 'Maurya' AND customer_id > 2;
  • 17. TRUNCATE TABLE 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 TRUNCATE TABLE [schema_name.]table_name 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. TRUNCATE TABLE customers;
  • 18. DELETE Table Delete table is used to delete the table 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.
  • 19. DISTINCT Clause Oracle DISTINCT clause is used to remove the duplicate records from the result set. It is only used with SELECT statement. SELECT DISTINCT expressions FROM tables WHERE conditions; SELECT DISTINCT name, age, salary FROM customers WHERE age >= '60';
  • 20. FROM Clause • FROM clause is a mandatory clause in SELECT expression. It specifies the tables from which data is to be retrieved. Syntax: FROM table_name... Expressions... SELECT * FROM customers WHERE salary >= 20000 ORDER BY salary ASC;
  • 21. FROM Clause Example: (with two tables) Inner Join example: Let's take two tables "suppliers" and "order1". Suppliers: Order1:
  • 22. Execute the following query: SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number FROM suppliers INNER JOIN order1 ON suppliers.supplier_id = order1.supplier_id;
  • 23. ORDER BY Clause ORDER BY Clause is used to sort or re-arrange the records in the result set. The ORDER BY clause is only used with SELECT statement. SELECT expressions FROM tables WHERE conditions ORDER BY expression [ ASC | DESC ]; ASC: optional parameter, used to sort records in ascending order. DESC: optional parameter, used to sort records in descending order. SELECT * FROM supplier ORDER BY last_name; SELECT * FROM supplier ORDER BY last_name DESC;
  • 24. GROUP BY Clause GROUP BY clause is used with SELECT statement to collect data from multiple records and group the results by one or more columns. SELECT expression1, expression2, ... expression_n, aggregate_function (aggregate_expression) FROM tables WHERE conditions GROUP BY expression1, expression2, ... expression_n; SELECT item, SUM(sale) AS "Total sales" FROM salesdepartment GROUP BY item;
  • 25. HAVING Clause • HAVING Clause is used with GROUP BY Clause to restrict the groups of returned rows where condition is TRUE. SELECT expression1, expression2, ... expression_n, aggregate_function (aggregate_expression) FROM tables WHERE conditions GROUP BY expression1, expression2, ... expression_n HAVING having_condition; • aggregate_function: It specifies the aggregate functions i.e. SUM, COUNT, MIN, MAX or AVG functions. • aggregate_expression: It specifies the column or expression on that the aggregate function is based on. • having_conditions: It specifies the conditions that are applied only to the aggregated results to restrict the groups of returned rows.
  • 26. 1) With GROUP BY SUM function 2) With GROUP BY COUNT function SELECT item, SUM(sale) AS "Total sales" FROM salesdepartment GROUP BY item HAVING SUM(sale) < 1000; SELECT state, COUNT(*) AS "Number of customers"FROM customers WHERE salary > 10000 GROUP BY state HAVING COUNT(*) >= 2;
  • 27. 3) with GROUP BY MIN function 4) with GROUP BY MAX function SELECT department, MIN(salary) AS "Lowest salary" FROM employees GROUP BY department HAVING MIN(salary) < 15000; SELECT department, MAX(salary) AS "Highest salary" FROM employees GROUP BY department HAVING MAX(salary) > 30000;