You are on page 1of 23

Transaction Control Language(TCL):-

Transaction Control Language(TCL) commands are used to manage


transactions in the database. These are used to manage the changes made
to the data in a table by DML statements. It also allows statements to be
grouped together into logical transactions.
TCL commands are 1)commit 2)rollback 3)savepoint.

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.

Syntax:- SAVEPOINT savepoint_name;

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.

Selection: A select operation selects a subset of rows (records) in a


table (relation) that satisfy a selection condition. The ability to select
rows from out of complete result set is called Selection. It involves
conditional filtering and data staging. The subset can range from no
rows, if none of the rows satisfy the selection condition, to all rows in
a table.
Projection: A projection operation selects only certain columns
(fields) from a table. The result table has a subset of the available
columns and can include anything from a single column to all available
columns..
Join: You can use the join capability in SQL to bring together data
that is stored in different tables by creating a link through a column
that both the tables share..
Syntax :-
Selecting All Columns, All Rows
We can display all columns of data in a table by following the SELECT keyword
with an asterisk (*).

SQL>select *from dept;

You can also display all columns in The table by listing all the columns after the
SELECT keyword.

SQL>SELECT deptno, dname, loc FROM dept;

Selecting Specific Columns, All Rows(projection):-


Projection refers to a subset of columns. Projections are made by specifying
the columns to be returned by a SELECT query.
You can use the SELECT statement to display specific columns of the table by
specifying the column names, separated by commas.

SQL>SELECT deptno, ename FROM emp;


SQL SELECT WHERE Clause

A SELECT statement can have an optional WHERE clause.The WHERE clause


allows us to fetch records from a database table that matches specified
condition(s).
Syntax:-

SELECT column1, column2, ...FROM table_name WHERE condition;


You can specify a condition using comparison or logical operators like
>,<,=,LIKE,NOT etc.

Example:-
1)Display all employee details belongs to deptno=10.

SQL>select * from emp where deptno=10;


2) List the emps who joined before 1981.

SQL>select * from emp where hiredate < (‟01-jan-81‟);

3) Retrieve all salesmen who are paid more than 1500 a month.

SQL>SELECT EMPNO, ENAME, JOB, SAL FROM EMP WHERE


JOB = 'SALESMAN' AND sal>1500;
The logical operator AND is used to specify that two conditions must both be
true. When a WHERE clause has more than one condition, this is called a
compound condition.

4) Find employees who are based in either department 10 or department 20.

SQL> SELECT EMPNO, ENAME, DEPTNO FROM EMP


WHERE DEPTNO = 10 OR DEPTNO = 20;

The logical operator OR is used to specify that at least one of two conditions must be true.

Order by clause in select statement:-

The ORDER BY keyword is used to sort the result-set in ascending or descending


order.The ORDER BY keyword sorts the records in ascending order by default. To
sort the records in descending order, use the DESC keyword.

SELECT column1, column2, ...FROM table_name ORDER BY

column1, column2, ... ASC|DESC;

Example:-

1)List the Empno, Ename, Sal of all emps in the asc order.

SQL>select empno,ename,sal from emp order by sal asc;

2)List the Empno, Ename, Sal of all emps in the descending order.

SQL>select empno,ename,sal from emp order by sal desc;


3) The following query will sort employees into department number
order, and within that, into employee name order.

SQL> SELECT EMPNO, ENAME, DEPTNO FROM EMP

ORDER BY DEPTNO, ENAME;

4) lists all salesman employees in ascending order of salary.

SQL>SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE JOB = „SALESMAN‟


ORDER BY SAL;

GROUP BY Clause in select statement:-

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:-

1.Display Maximum salary of employee in each department.

SQL>select deptno,max(sal) from emp group by deptno;


2.Display average salary of employee in each department.

SQL>select deptno,avg(sal) from emp group by deptno;

3.Display No.of Employees in each department.


SQL>select deptno,count(*) from emp group by deptno;

“having” clause in select statement:-


The SQL HAVING clause is similar to the WHERE clause; both are used to filter rows
in a table based on specified criteria. However, the HAVING clause is used to filter
grouped rows instead of single rows. These rows are grouped together by the GROUP
BY clause, so, the HAVING clause must always be followed by the GROUP BY clause.
Moreover, the HAVING clause can be used with aggregate functions such as COUNT(),
SUM(), AVG(), etc., whereas the WHERE clause cannot be used with them.
Syntax

SELECT column1, column2, aggregate_function(column) FROM table_name


GROUP BY column1, column2 HAVING condition;
Example:-

DISTINCT KEYWORD IN SELECT :-


The SQL DISTINCT keyword is used in conjunction with the SELECT statement to fetch
unique records from a table.We use DISTINCT keyword with the SELECT statetment
when there is a need to avoid duplicate values present in any specific columns/tables.
When we use DISTINCT keyword, SELECT statement returns only the unique records
available in the table.
Syntax

SELECT DISTINCT column1, column2,.....columnN FROM table_name;


Example:-
1)Display unique Jobs from EMP table?
SQL> select distinct job from emp;
(0r)
SQL>select unique job from emp;
2)Display unique deptno,job from EMP.

SQL>select distinct deptno,job from emp;

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.

Alias Column Syntax:-

SELECT column_name AS alias_name FROM table_name;

Alias table Syntax

SELECT column_name(s)FROM table_name AS alias_name;

Syntax:-

select colname "title", colname "title"... from table;


Example:-
SQL>select ename "Emp Name", sal “SALARY” from emp;
DURGASOFT Oracle

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.

Arithmetic Calculations On Users data:

Ex: select 200+300 from dual;

500

Ex: select (90000*10)/100 "10% of 90000" from dual;

10% of 90000
------------
9000

Ex: select 2000+(0.10*5000)-300 " After calculation" from dual;


2200

Arithmetic Calculations On Table data:

Ex: Display emp salaries and 2% of salary as TA?

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:-

These are used to compare values by specifying conditions on the columns.

< > = <= >=

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];

Ex: display salaries below 12000?

select sal " emp sal below 12000" from emp_info where sal < 12000;

Ex: Display employee details who is getting above 12000 salary?

select * from emp_info where sal> 12000;

Ex: display the details of accounting dept?

select * from dept where dname='ACCOUNTING';

Ex: display the details of managers?

select * from emp where job='MANAGER';

Ex: display employee name and sal of empno 7788?

select ename,sal from emp where empno=7788;

Ex: Display employee details who joined before 1st jan 1981?

select * from emp where hiredate < '01-jan-81';

Ex: select act_type,act_open_dt, count(actno) from cust_act_dtls


group by act_open_dt;

Ex: select cityid,count(custid) " No. of accounts"


from cust_act_dtls
group by cityid;

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

1) Fetch all clerks information


2) Display all departments information located at CHICAGO?
3) Display product details manufactured in the current year only?
4) Get the details of cutomers accounts who opened the accounts before this year?
5) Get all SALARY account details?
6) Display customer names and mobile numbers from the city 'Texas'?

select cname,mobile from cust_dtls where city='Texas';

7) Get the information of Trading account?

8) Display only Expired product details?


select * from prod_dtls where exp<sysdate;

SPECIAL OPERATORS:

BETWEEN It supports specific range of values.

IN It supports specific list of values

IS NULL It is used to check the column value is null or not, if it is null display
output

LIKE It is used to represents sequence of chars / specific strings

Syntax:-1 BETWEEN

select cl1, cl2,......,cl-n / *


from table_name
where <ColumnName> BETWEEN <start_value> AND <end_value>
order by cl1, cl2,...,cln [asc/desc];

Note: In the above syntax it includes both start value and end value.
BETWEEN Operator supports both Numeric range and Date range

Syntax: IN

select cl1, cl2,......,cl-n / *


from table_name
where <ColumnName> IN(val1, val2,val3,.....)
order by cl1, cl2,...,cln [asc/desc];

Note: IN operator works on Numeric, string and Date type data.

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

select cl1, cl2,......,cl-n / *


from table_name
where <ColumnName> IS NULL
order by cl1, cl2,...,cln [asc/desc];

Note: It works on only null values and it is independent of data type of column.

Syntax: LIKE

select cl1, cl2,......,cl-n / *


from table_name
where <ColumnName> LIKE'string'
order by cl1, cl2,...,cln [asc/desc];

LIKE:

It uses 2 symbols

_ (underscore)represents anyone char

% represents any number of chars

Ex: display salaries between 2000 and 3000 in ascending order?

select sal from emp where sal between 2000 and 3000 order by sal;

Ex: display employee details who is joined in 1981?

select * from emp where hiredate between '01-jan-81' and '31-dec-81';


or
select * from emp where hiredate like'%81';

Ex: display emplyees working like clerks and managers?

select * from emp where job IN('CLERK','MANAGER');

Ex: display employee names and salaries who is getting any one of following salary?

1250,3000,5000

select ename,sal from emp where sal in(1250,3000,5000);

Ex: display employee id,name,sal,comm who is not getting comission?

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

select empno,ename,sal,comm from emp


where comm is null;

Ex: dispaly 3 digit salaries?

select sal from emp where sal like'___';

Ex: display names of emps begins s?

select ename from emp where ename like'S%';

Ex: display employees joined in 87?

select * from emp where hiredate like'%87';

22-may-87
02-feb-87
11-oct-87

select * from emp;

select * from emp where sal between 1000 and 2000;

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;

select * from emp where job in('CLERK','SALESMAN');

select * from emp where job not in('CLERK','SALESMAN');

select * from emp where deptno in(10,20);

select * from emp where hiredate in('19-apr-87','23-jan-82') ;

create table sample as select * from emp;


select * from sample;

update sample set deptno=null where empno


in(7499,7566,7698,7788,7876,79007902,7934);

--Display 3-digit salaries


select sal from emp where sal like'___';

--Display salaries begining with digit "2"?


select sal from emp where sal like'2%';

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 4-char length employee names?


select ename from emp where ename like'____';

--Display 4-char length employee names ends with "D"?


select ename from emp where ename like'___D';

--Display employee names,salaries, hiredates joined inn the year " 81"?
select ename,sal,hiredate from emp where hiredate like'%81';

RELATION NEGATION OPERATORS:

!= (or) <> (or) ^= (NOT EQUAL TO)

NOT BETWEEN

NOT LIKE

NOT IN

IS NOT NULL

Ex: Display all emps details except SALESMAN?

select * from emp where job<>'SALESMAN';

Ex: Display employee details not joined in the last year?

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?

2) Display unknown account details?

3) Display customer details whose gender is unknown?

4) Display customers from the citites 'TEXAS ' and 'CHICAGO'?

5) Display Product details manufactured in january of this year?

6) Display product details whose warrenty is finished in the last year?

7) Display customer names having a char 'K'?

8) Display customer details who is living in 6 char length cities?


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 36
DURGASOFT Oracle

LOGICAL OPERATORS:

These are used to specify Multiple conditions in the where clause.

AND Display output if all conditions are true.


If any one condition was failed then it will not display output.

OR Display output if anyone condition is true.


If all conditions are false then it will not display output.

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 manager details getting above 2500 sal?

select * from emp where job='MANAGER' and sal>2500;

Ex: Display clerks and salesman details if their salary at least 1000 and atmost 1500?

select ename,sal,job from emp


where job in('CLERK','SALESMAN') AND sal between 1000 and 1500;

Ex: Display salary account details having below 100000 balance?

select * from cust_act_dtls where act_type='sal' and bal <100000;

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

select * from cust_dtls


where (gender='male' and city='texas')
or
( gender='female' and city='chicago');

EX: Display employee details joined in 87 year or working under deptno 10?

select * from emp where hiredate like'%87' or 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

Updating single column value:

Syntax: update<table_name>
set colname= value / expression where <condition>;

Note: Without condition update command change all values in the column.

Updating multiple column values:

Syntax:
update <table_name>
set colname1= value / expression,
colname2= value / expression,
colname3= value / expression
:
where <condition>;

Ex: update the commission of 7369 as 500?

update emp set comm=500 where empno=7369;

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;

2.Display total salary of employees belongs to deptno=10.


SQL>select sum(sal) from emp where deptno=10;

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 ;

2. Display maximum salary of employee of deptno=10.


SQL>select max(sal) from emp where deptno=10;
Min():-
The Min() aggregate function takes the name of a column as an argument
and returns the smallest value in a column.
Syntax:-
MIN() or MIN( [ALL|DISTINCT] expression )

Example:-
1.Display minimum salary of employee among all.
SQL>select min(sal) from emp ;

2. Display maximum salary of employee of deptno=10.


SQL>select min(sal) from emp where deptno=10;

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.

Syntax:- AVG() or AVG( [ALL|DISTINCT] expression )

1.Display average salary of all the employees.


SQL>select avg(sal) “Average salary” from emp;

2.Display average salary of employees belongs to deptno=10.

SQL>select deptno,avg(sal) from emp where deptno=10 group by


deptno;
COUNT():-
The COUNT() aggregate function returns the total number of rows that match
the specified criteria. For instance, to find the total number of employees who
have less than 5 years of experience, the given query can be used.

Note: A column name of the table can also be used instead of *.


Unlike COUNT(*), this variation COUNT(column) will not count NULL values in that
column.
Syntax:- count(*) or COUNT( [ALL|DISTINCT] expression )

1.Display total no.of employees in emp table.


SQL>select count(*) from emp;

2.Display total no.of employees who are getting commission.


SQL>select count(comm) from emp;

You might also like