Sqlplus
Username : connect as sysdba
Password: oracle123
Create user abhi identified by abhishek;
GRANT CONNECT TO abhi;
GRANT RESOURCE TO abhi;
GRANT SELECT, UPDATE ON emp_info TO abhi;
1. GRANT ALL PRIVILEGES to abhi;
Larry Ellison and his two friends and former co-workers, Bob Miner and Ed Oates, started a
consultancy called Software Development Laboratories (SDL) in 1977. SDL developed the
original version of the Oracle software.
Current release: Oracle Database 21c
Oracle holding #1 RDBMS market share worldwide based on the revenue
1. Data Definition Language (DDL)
o CREATE
o ALTER
o DROP
o TRUNCATE
The ‘CREATE TABLE’ Statement
This statement is used to create a table.
Syntax
CREATE TABLE TableName (
Column1 datatype,
Column2 datatype,
Column3 datatype,
....
ColumnN datatype
);
CREATE TABLE Emp_Info
(
EmpID Number(2),
EmpName varchar(7),
FName varchar(7),
PhoneNo Number(10),
Address varchar(12),
City varchar(10),
Country varchar(7)
);
INSERT INTO
This statement is used to insert new records into the table.
Syntax
INSERT INTO TableName (Column1, Column2, Column3, ...,ColumnN)
VALUES (value1, value2, value3, ...);
--If you don't want to mention the column names then use the below
syntax
INSERT INTO TableName
VALUES (Value1, Value2, Value3, ...);
INSERT INTO Emp_Info
VALUES ('01', 'Sanjana','Jagan', '9921321141', 'Camel Street', 'Chennai', 'India');
INSERT INTO Emp_Info
VALUES ('02', 'Sanu','Praveen', '9934567654', 'Nice Road', 'Pune', 'India');
INSERT INTO Emp_Info
VALUES ('03', 'Abhi','Vijay', '8299005088', 'Rajapur', 'Allahabad', 'India');
INSERT INTO Emp_Info
VALUES ('04', 'Pawan','Ram', '9415026341', 'gomti nagar', 'Lucknow', 'India');
ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:
To add a new column in the table
1. ALTER TABLE table_name ADD column_name COLUMN-definition;
To modify existing column in the table:
1. ALTER TABLE MODIFY(COLUMN DEFINITION....);
EXAMPLE
1. ALTER TABLE emp_info ADD(Pincode VARCHAR(6
));
2. ALTER TABLE emp_info MODIFY (empname VARC
HAR(20));
3. ALTER TABLE emp_info
DROP COLUMN country;
The ‘DROP TABLE’ Statement
This statement is used to drop an existing table. When you use this statement, complete
information present in the table will be lost.
Syntax
DROP TABLE TableName;
DROP Table Employee_Info;
TRUNCATE: It is used to delete all the rows from the table and free the space containing
the table.
Syntax:
1. TRUNCATE TABLE table_name;
Example:
1. TRUNCATE TABLE emp_info;
Data Manipulation Language
DML commands are used to modify the database.
o INSERT
o UPDATE
o DELETE
UPDATE: This command is used to update or modify the value of a column in the table.
Syntax:
1. UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WH
ERE CONDITION]
For example:
UPDATE emp_info
SET empName = 'pappu'
WHERE empId = '03';
UPDATE emp_info
SET empName = 'rahul', fname = ‘rajiv’
WHERE empId = '03';
c. DELETE: It is used to remove one or more row from a table.
Syntax:
1. DELETE FROM table_name [WHERE condition];
For example:
DELETE FROM emp_info
WHERE empname=’rahul’;
Data Control Language
DCL commands are used to grant and take back authority from any database user.
o Grant
o Revoke
a. Grant: It is used to give user access privileges to a database.
Example
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
b. Revoke: It is used to take back permissions from the user.
Example
1. REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
Transaction Control Language
TCL commands can only use with DML commands like INSERT, DELETE and UPDATE only.
o COMMIT
o ROLLBACK
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
1. COMMIT;
Example:
DELETE FROM emp_info
WHERE empid = 01;
COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not already been
saved to the database.
Syntax:
1. ROLLBACK;
Example:
DELETE FROM emp_info
WHERE empid = 01;
ROLLBACK;
Data Query Language
DQL is used to fetch the data from the database.
It uses only one command:
o SELECT
Arithmatic Operators:
CREATE TABLE Emp_Sal
(
EmpID Number(2),
EmpName varchar(7),
Basic Number(5),
HRA Number(5),
DA Number(5)
);
Insert into emp_sal values (1,’Abhi’,10000,2000,3000);
Insert into emp_sal values (2,’Ram’,20000,3000,4000);
Insert into emp_sal values (3,’Shyam’,30000,4000,5000);
Insert into emp_sal values (4,’Mohan’,40000,5000,6000);
Insert into emp_sal values (5,’Sohan’,50000,6000,7000);
Insert into emp_sal values (6,’Rohan’,50000,5500,7700);
Addition:
SELECT empid, empname, basic, hra, da, basic+hra+da
AS "Salary" FROM emp_sal;
Substraction:
SELECT empid, empname, basic, hra, da, basic-da
AS "Example_Sub" FROM emp_sal;
Multiplication:
SELECT empid, empname, basic, hra, da, basic*da
AS "Example_Mul" FROM emp_sal;
Division:
SELECT empid, empname, basic, hra, da, basic/da
AS "Example_Div" FROM emp_sal;
Describe Command:
Desc Emp_sal;
SQL Comparison Operators
Operator Description
= Equal to
SELECT * FROM emp_sal
WHERE empid = 1;
> Greater than
SELECT * FROM emp_sal
WHERE basic > 10000;
< Less than
SELECT * FROM emp_sal
WHERE basic < 30000;
>= Greater than or equal to
SELECT * FROM emp_sal
WHERE basic >= 30000;
<= Less than or equal to
SELECT * FROM emp_sal
WHERE basic <= 30000;
<> Not equal to
SELECT * FROM emp_sal
WHERE basic <> 30000;
SQL Logical Operators
Operator Description
AND TRUE if all the conditions separated by AND is TRUE
select * from emp_sal
where basic = 50000 and empid = 5;
BETWEEN TRUE if the operand is within the range of comparisons
SELECT * FROM emp_sal
WHERE basic BETWEEN 10000 AND 30000;
IN TRUE if the operand is equal to one of a list of expressions
SELECT * FROM emp_sal
WHERE basic IN (10000,30000);
LIKE TRUE if the operand matches a pattern
SELECT * FROM emp_sal
WHERE empname LIKE 'Ab%';
NOT Displays a record if the condition(s) is NOT TRUE
SELECT * FROM emp_sal
WHERE empname not LIKE 'Ab%';
OR TRUE if any of the conditions separated by OR is TRUE
select * from emp_sal
where basic = 50000 or empid = 1;
LOGICAL OPERATOR HAS PRECEDENCE AND HERE IS THE RANKING:
Order by Clause:
select * from emp_sal order by empname;
order by sorts the values in ascending order
select * from emp_sal order by empname desc;
Single Row functions - Single row functions are the one who work on single row and
return one output per row. For example, length and case conversion functions are
single row functions.
Multiple Row functions - Multiple row functions work upon group of rows and return
one result for the complete set of rows. They are also known as Group Functions.
Case Conversion functions - Accepts character input and returns a character
value. Functions under the category are UPPER, LOWER and INITCAP.
o UPPER function converts a string to upper case.
o LOWER function converts a string to lower case.
o INITCAP function converts only the initial alphabets of a string to upper
case.
Character functions - Accepts character input and returns number or character
value. Functions under the category are CONCAT, LENGTH, SUBSTR, INSTR.
o CONCAT function concatenates two string values.
o LENGTH function returns the length of the input string.
o SUBSTR function returns a portion of a string from a given start point to
an end point.
o INSTR function returns numeric position of a character or a string in a
given string.
Number functions - Accepts numeric input and returns numeric values.
Functions under the category are ROUND, TRUNC, and MOD.
o ROUND and TRUNC functions are used to round and truncate the
number value.
o MOD is used to return the remainder of the division operation between
two numbers.
SELECT UPPER (empname)FROM emp_info;
SELECT LOWER (empname)FROM emp_info;
SELECT INITCAP (empname)FROM emp_info;
SELECT CONCAT (City, Country)
FROM emp_info;
SELECT LENGTH(EMPNAME)
FROM emp_info;
Create table employee (
fname varchar(10),
mname varchar (10),
lname varchar (10));
insert into employee values ('Abhishek','Kumar','Jaiswal');
insert into employee values ('Ashutosh','','Pandey');
insert into employee values ('Ram','','');
insert into employee values ('Mohan','Kumar','Sharma');
The SELECT query below demonstrates the use of SUBSTR and INSTR functions.
SUBSTR function returns the portion of input string from 1st position to 5th position.
INSTR function returns the numeric position of character 'a' in the first name.
SELECT SUBSTR (fname,1,5) from employee;
SELECT INSTR (fname,'h')FROM employee;
Number functions
The SELECT query below demonstrates the use of ROUND and TRUNC functions.
SELECT ROUND (HRA,1)
FROM emp_sal where empid = ‘7’;
SELECT TRUNC (DA,-2)FROM emp_sal;
MOD() function
SQL MOD() function is used to get the remainder from a division.
SELECT MOD (DA,HRA) from emp_sal;
MULTI ROW FUNCTIONS :–
1. These functions are also calld “Aggregate functions” (Or) Group
functions.
2. All these multi row functions will process multiple records.
3. Applied on group of records and returns one value from the
entire group.
MAX ( ) Function
Select max(sal),from emp;
Min ( ) Function
Select min(sal) from emp
Sum Function:
Select sum(sal) from emp
avg ( ) Function
Select avg (sal) from emp
Count ( ) Function
Select count(empno) from emp
count * ( ) Function
Select count (*) from emp
Group by Function:
The GROUP BY statement is often used with aggregate functions (COUNT, MAX,
MIN, SUM, AVG) to group the result-set by one or more columns.
select count(empname), basic from emp_sal group by basic;
Having Clause:
select count(empname), basic from emp_sal group by basic having
count(empname)>1;
To_Char
TO_CHAR function converts a datetime value (DATE, TIMESTAMP data types i.e.) to a string using the
specified format.
The DUAL table is a special one-row, one-column table present by default in Oracle
SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD') FROM dual;
SELECT TO_CHAR(SYSDATE, 'month dd, yyyy') FROM dual;
NVL Function
select empid,empname,nvl(basic,0) from emp_sal;
Second Highest Salary
select max(Salary) as Second_Max_DA from salary where Salary <
(select max(Salary) from Salary);
select max(Salary) as Second_Max_DA from salary where Salary not in
(select max(Salary) from Salary);
Nth Highest salary
Select id,salary from salary s1
Where 2 = (select count (Distinct Salary)
From salary s2
Where s2.salary >s1.salary)