Professional Documents
Culture Documents
COMMIT command
COMMIT command is used to permanently save any transaction into the
database. When we use any DML command like INSERT, UPDATE or DELETE,
the changes made by these commands are not permanent, until the current
session is closed, the changes made by these commands can be rolled
back.To avoid that, we use the COMMIT command to mark the changes as
permanent.
Syntax:- COMMIT;
ROLLBACK command
This command restores the database to last commited state. It is also used
with SAVEPOINT command to jump to a savepoint in an ongoing
transaction.If we have used the UPDATE command to make some changes
into the database, and realise that those changes were not required, then
we can use the ROLLBACK command to rollback those changes, if they were
not commited using the COMMIT command.
Syntax: - 1) rollback ;
2) rollback to savepoint_name;
SAVEPOINT command
SAVEPOINT command is used to temporarily save a transaction so that you
can rollback to that point whenever required.
In short, using this command we can name the different states of our data
in any table and then rollback to that state using the ROLLBACK command
whenever required.
Data Query Language:-
Data query language is used to fetch data from tables based on conditions that we can
easily apply.Data Query Language Contains only one command called “SELECT”.
EMP TABLE
DEPT TABLE:-
Select command:-
The Oracle SELECT statement is used to retrieve data from one or more than
one tables, object tables, views, object views etc.
You can also display all columns in The table by listing all the columns after the
SELECT keyword.
Example:-
1)Display all employee details belongs to deptno=10.
3) Retrieve all salesmen who are paid more than 1500 a month.
The logical operator OR is used to specify that at least one of two conditions must be true.
Example:-
1)List the Empno, Ename, Sal of all emps in the asc order.
2)List the Empno, Ename, Sal of all emps in the descending order.
The SQL GROUP BY clause is used in conjunction with the SELECT statement to
arrange identical data into groups. This clause follows the WHERE clause in a SELECT
statement and precedes the ORDER BY and HAVING clauses (if they exist).
The main purpose of grouping the records of a table based on particular columns is to
perform calculations on these groups. Therefore, The GROUP BY clause is typically
used with aggregate functions such as SUM(), COUNT(), AVG(), MAX(), or MIN() etc.
Syntax:-
SELECT column_name(s)
FROM table_name
GROUP BY column_name(s);
Example:-
SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary
name. Aliases are often used to make column names more readable.An alias
only exists for the duration of that query. An alias is created with
the AS keyword.
Syntax:-
OPERATORS
Arithmetic Operators:
+ - * /
These are used to perform Arithmetic calculations on user's own data and table data.
DUAL table:
It is a system defined table which contains only one column to perform calculations
on users own data.
500
10% of 90000
------------
9000
select sal " Basic Sal", (0.02*sal) " TA" from emp;
Ex: Display employee salaries, 2% as TA, 5% as DA, 10% HRA, 4% as COMM and final
salary?
select Sal " Basic", (0.02*Sal) " TA", (0.05*sal) "DA", (0.10*sal) "HRA",
(0.04*sal) " Comm",
(Sal + (0.02*Sal) + (0.05*sal) + (0.10*sal) + (0.04*sal) " Final Salary"
from emp;
RELATIONAL OPERATORS:-
WHERE clause:
In select query we can write conditions in this clause.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80
96 96 96 96, 9246212143 | www.durgasoft.com Page 31
DURGASOFT Oracle
Syntax:
select cl1, cl2,......,cl-n / *
from table_name
where <conditions>
order by cl1, cl2,...,cln [asc/desc];
select sal " emp sal below 12000" from emp_info where sal < 12000;
Ex: Display employee details who joined before 1st jan 1981?
ASSIGNMENTS:
Consider the below tables with estimated columns and then practise below questions.
CUST_DTLS
CUST_Act_DTLS
ACT_TYPES_INFO
PROD_DTLS
EMP
DEPT
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80
96 96 96 96, 9246212143 | www.durgasoft.com Page 32
DURGASOFT Oracle
SPECIAL OPERATORS:
IS NULL It is used to check the column value is null or not, if it is null display
output
Syntax:-1 BETWEEN
Note: In the above syntax it includes both start value and end value.
BETWEEN Operator supports both Numeric range and Date range
Syntax: IN
Syntax: IS NULL
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80
96 96 96 96, 9246212143 | www.durgasoft.com Page 33
DURGASOFT Oracle
Note: It works on only null values and it is independent of data type of column.
Syntax: LIKE
LIKE:
It uses 2 symbols
select sal from emp where sal between 2000 and 3000 order by sal;
Ex: display employee names and salaries who is getting any one of following salary?
1250,3000,5000
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80
96 96 96 96, 9246212143 | www.durgasoft.com Page 34
DURGASOFT Oracle
22-may-87
02-feb-87
11-oct-87
select * from emp where sal not between 1000 and 2000;
select * from emp where hiredate between '01-jan-81' and '31-dec-81' order by hiredate;
select * from emp where hiredate not between '01-jan-81' and '31-dec-81' order by hiredate;
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80
96 96 96 96, 9246212143 | www.durgasoft.com Page 35
DURGASOFT Oracle
--Display employee names begins with "J" and ends with "S"?
select ename from emp where ename like'J%S';
--Display employee names,salaries, hiredates joined inn the year " 81"?
select ename,sal,hiredate from emp where hiredate like'%81';
NOT BETWEEN
NOT LIKE
NOT IN
IS NOT NULL
select * from emp where hiredate NOT BETWEEN '01-jan-14' and '31-dec-14';
ASSIGNMENTS:
1) Display customer account details whose balance is at least 10000 and at most 100000?
LOGICAL OPERATORS:
Syntax:
SELECT cl1,cl2,....., / *
FROM <table_name>
WHERE <cond-1> [ AND / OR ] <cond-2> [ AND / OR ] <cond-3> [ AND / OR ].......
ORDER BY cl1, cl2,...... [ ASC /DESC];
Ex: Display clerks and salesman details if their salary at least 1000 and atmost 1500?
Assignments:
Ex: Display tablet or mobile information if their cost min 10000 and max 15000?
Ex: Display product details if they were manufactured in current year and min cost 2000
and max cost 10000?
CUST_ACT_DTLS
-------------
ACTNO ACT_TYPE ACT_OPEN_DT ACT_BAL CUST_ID
Ex: Display "male" customers from "texas" and "female" customers from "chicago"?
CUST_DTLS
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80
96 96 96 96, 9246212143 | www.durgasoft.com Page 37
DURGASOFT Oracle
---------
CUST_ID CUST_NAME CUST_CITY CUST_GENDER
CUST_MOBILE
EX: Display employee details joined in 87 year or working under deptno 10?
Ex: Display trading account details having min balance 10000 and savings account
details having min balance 100000?
DML COMMANDS
UPDATE:
It is used to update old values with new values within the table.By default it updates all
values in the column
Syntax: update<table_name>
set colname= value / expression where <condition>;
Note: Without condition update command change all values in the column.
Syntax:
update <table_name>
set colname1= value / expression,
colname2= value / expression,
colname3= value / expression
:
where <condition>;
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80
96 96 96 96, 9246212143 | www.durgasoft.com Page 38
SQL Aggregate Functions
SQL aggregation function is used to perform the calculations on multiple
rows of a single column of a table. It returns a single value. It is also used to
summarize the data. SQL Aggregate functions are
1.sum() 2.count() 3.max() 4.min()5.count()
1.SUM() :-
The SUM() aggregate function takes the name of a column as an argument
and returns the sum of all the value in that column.
Syntax:-
SUM() or SUM( [ALL|DISTINCT] expression )
Example:-
1.Display total salaries of all the employees.
SQL>select sum(sal) from emp;
MAX():-
The MAX() aggregate function takes the name of a column as an argument
and returns the largest value in a column.
Syntax:-
MAX() or MAX( [ALL|DISTINCT] expression )
Example:-
1.Display maximum salary of employee among all.
SQL>select max(sal) from emp ;
Example:-
1.Display minimum salary of employee among all.
SQL>select min(sal) from emp ;
Avg():-
The AVG function is used to calculate the average value of the numeric type. AVG
function returns the average of all non-Null values.