You are on page 1of 42

NAME: WRITAM MALLIK

SEC : A ROLL : 04
DATABASE MANAGEMENT LAB (CS691)
(FULL ASSIGNMENT SET)(LAB GROUP C1)

ASSIGNMENT 1

1.Make your own student table with certain necessary facts, like your id, name,
branch.Fill up the table with the records of at least 10 of your friends.

ID NAME BRANCH
---------- -------------------- --------------------
1 Writam CSE

2 Rupam

3 Ipshita CSE

ID NAME BRANCH
---------- -------------------- --------------------

4 Salman CSE

5 Sumita BCA

6 Rai BCA

ID NAME BRANCH
---------- -------------------- --------------------

7 Somodyuti CSE

8 Tithy CSE

9 Lionel FCB
ID NAME BRANCH
---------- -------------------- --------------------

10 Sourav KKR

2. It sounds good if you say roll instead of id. so, change it.

SQL> alter table writam rename column id to roll;


Table altered.
SQL> select * from writam;

ROLL NAME BRANCH


---------- -------------------- --------------------
1 Writam CSE

2 Rupam

3 Ipshita CSE

ROLL NAME BRANCH


---------- -------------------- --------------------

4 Salman CSE

5 Sumita BCA

6 Rai BCA

ROLL NAME BRANCH


---------- -------------------- --------------------

7 Somodyuti CSE

8 Tithy CSE

9 Lionel FCB
ROLL NAME BRANCH
---------- -------------------- --------------------

10 Sourav KKR

3.Here, I think age and address could also be added. So, append it with default
address of all students as Kolkata.

SQL> alter table writam add address varchar2(20) default 'kolkata';


Table altered.
SQL> select * from writam;

ROLL NAME BRANCH ADDRESS


---------- -------------------- -------------------- ------------------
1 Writam CSE Baranagar

2 Rupam Baranagar

3 Ipshita CSE Howrah

ROLL NAME BRANCH ADDRESS


---------- -------------------- -------------------- -------------------

4 Salman CSE Beckbagan

5 Sumita BCA Kuthighat

6 Rai BCA Dumdum

ROLL NAME BRANCH ADDRESS


---------- -------------------- -------------------- -------------------

7 Somodyuti CSE Cooch Behar

8 Tithy CSE Duttapukur

9 Lionel FCB Rosario


ROLL NAME BRANCH ADDRESS
---------- -------------------- -------------- ------------------

10 Sourav KKR Behala

4.Fill up the records with individual student's age.

SQL>update writam set age=20 where roll=2;

1 row updated.

SQL>update writam set age=22 where roll=3;

1 row updated.

SQL>update writam set age=22 where roll=3;

1 row updated.

SQL>update writam set age=22 where roll=4;

1 row updated.

SQL>update writam set age=32 where roll=5;

1 row updated.

SQL>update writam set age=26 where roll=6;

1 row updated.

SQL>update writam set age=25 where roll=7;

1 row updated.

SQL>update writam set age=24 where roll=8;

1 row updated.

SQL>update writam set age=24 where roll=9;

1 row updated.
SQL>update writam set age=24 where roll=10;

1 row updated.

SQL> select * from writam;

5.How do I identify each student uniquely? So make roll number as your primary
key.
SQL> alter table writam modify roll primary key;

Table altered.

6.Add marks column in the table and add values.

SQL> alter table writam add marks number;

Table altered.

SQL>update writam set marks=23 where roll=1;

1 row updated.

SQL>update writam set marks=33 where roll=2;

1 row updated.

SQL>update writam set marks=34 where roll=10;

1 row updated.

SQL>update writam set marks=34 where roll=9;

1 row updated.

SQL>update writam set marks=32 where roll=8;

1 row updated.

SQL>update writam set marks=78 where roll=7;

1 row updated.

SQL>update writam set marks=56 where roll=6;

1 row updated.
SQL>update writam set marks=56 where roll=5;

1 row updated.

SQL>update writam set marks=67 where roll=4;

1 row updated.

SQL>update writam set marks=67 where roll=3;

1 row updated.

SQL> select * from writam;

7.Identify the students who have passed the exam. Cut off marks is 50%.

SQL> select * from writam where marks>=50;

8.If any student fails, discard his record from the database.

SQL> delete from writam where marks<=30;

1 rows deleted.
9.Remove the address field from your table.
SQL>alter table writam drop address;
Table altered.

10.Copy the contents from emp table to a new table.


Show the employee records from your new table.

SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO EXPERIENCE
---------- ---------- ------------ ---------- --------- ---------- ---------- --
-------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800
20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30
7566 JONES MANAGER 7839 02-APR-81 2975
20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7788 SCOTT ANALYST 7566 19-APR-87 3000
20
7839 KING PRESIDENT 17-NOV-81 5000
10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30
7876 ADAMS CLERK 7788 23-MAY-87 1100
20
7900 JAMES CLERK 7698 03-DEC-81 950
30
7902 FORD ANALYST 7566 03-DEC-81 3000
20
7934 MILLER CLERK 7782 23-JAN-82 1300
10

14 rows selected.
SQL> create table writam2 as select empno,ename,job,sal from emp;

Table created.

SQL> select * from writam2;

EMPNO ENAME JOB SAL


---------- ---------- ------------ ----------
7369 SMITH CLERK 800
7499 ALLEN SALESMAN 1600
7521 WARD SALESMAN 1250
7566 JONES MANAGER 2975
7654 MARTIN SALESMAN 1250
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7788 SCOTT ANALYST 3000
7839 KING PRESIDENT 5000
7844 TURNER SALESMAN 1500
7876 ADAMS CLERK 1100
7900 JAMES CLERK 950
7902 FORD ANALYST 3000
7934 MILLER CLERK 1300

14 rows selected.

11.Show salary statement along with name of all employees whose salary>1000.

SQL> select * from writam2 where sal>1000;


EMPNO ENAME JOB SAL
---------- ---------- ------------ ----------
7499 ALLEN SALESMAN 1600
7521 WARD SALESMAN 1250
7566 JONES MANAGER 2975
7654 MARTIN SALESMAN 1250
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7788 SCOTT ANALYST 3000
7839 KING PRESIDENT 5000
7844 TURNER SALESMAN 1500
7876 ADAMS CLERK 1100
7902 FORD ANALYST 3000
7934 LER CLERK 1300

11 rows selected.

12.How many such employees are there whose salary is within 1000 to 3000
range?
SQL> select * from writam2 where sal>1000 and sal<3000;

EMPNO ENAME JOB SAL


---------- ---------- ------------ ----------
7499 ALLEN SALESMAN 1600
7521 WARD SALESMAN 1250
7566 JONES MANAGER 2975
7654 MARTIN SALESMAN 1250
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7844 TURNER SALESMAN 1500
7876 ADAMS CLERK 1100
7934 MILLER CLERK 1300

9 rows selected.

SQL>
13.Give a pay hike to the employees whose salary is 1250 and 950.
SQL> alter table writam2 add payhike number;

Table altered.

SQL> update writam2 set payhike=100 where sal=1250 or sal=950;

2 rows updated.

SQL> select * from writam2;


EMPNO ENAME JOB SAL PAYHIKE
---------- ---------- ------------ ---------- ----------
7369 SMITH CLERK 800
7499 ALLEN SALESMAN 1600
7521 WARD SALESMAN 1250 100
7566 JONES MANAGER 2975
7654 MARTIN SALESMAN 1250 100
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7788 SCOTT ANALYST 3000
7839 KING PRESIDENT 5000
7844 TURNER SALESMAN 1500
7876 ADAMS CLERK 1100
7900 JAMES CLERK 950 100
7902 FORD ANALYST 3000
7934 MILLER CLERK 1300

14 rows selected.
14.Give a salary hike of 15% to the employees who have joined the company
before 31st Dec 1981

SQL> create table writam 3 as select empno,ename,job,sal,hiredate,comm from emp;


Table created.
SQL> select * from writam3;

EMPNO ENAME JOB SAL HIREDATE COMM


---------- ---------- ------------ ---------- --------- ----------
7369 SMITH CLERK 800 17-DEC-80
7499 ALLEN SALESMAN 1600 20-FEB-81 300
7521 WARD SALESMAN 1250 22-FEB-81 500
7566 JONES MANAGER 2975 02-APR-81
7654 MARTIN SALESMAN 1250 28-SEP-81 1400
7698 BLAKE MANAGER 2850 01-MAY-81
7782 CLARK MANAGER 2450 09-JUN-81
7788 SCOTT ANALYST 3000 19-APR-87
7839 KING PRESIDENT 5000 17-NOV-81
7844 TURNER SALESMAN 1500 08-SEP-81 0
7876 ADAMS CLERK 1100 23-MAY-87
7900 JAMES CLERK 950 03-DEC-81
7902 FORD ANALYST 3000 03-DEC-81
7934 MILLER CLERK 1300 23-JAN-82

14 rows selected.

SQL> alter table writam3 add payhike number;


Table altered.

SQL> update writam3 set payhike=500 where hiredate<31-DEC-81;


update writam3 set payhike=500 where hiredate<31-DEC-81
*
ERROR at line 1:
ORA-00904: "DEC": invalid identifier

SQL> update writam3 set payhike=500 where hiredate<'31-DEC-81';

11 rows updated.

ASSIGNMENT 2:

1. write query to select all the columns of emp table


SQL> select * from emp;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300


30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7566 JONES MANAGER 7839 02-APR-81 2975
20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400


30

7698 BLAKE MANAGER 7839 01-MAY-81 2850


30
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-JUN-81 2450
10

7788 SCOTT ANALYST 7566 19-APR-87 3000


20

7839 KING PRESIDENT 17-NOV-81 5000


10

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30

7876 ADAMS CLERK 7788 23-MAY-87 1100


20

7900 JAMES CLERK 7698 03-DEC-81 950


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7902 FORD ANALYST 7566 03-DEC-81 3000
20

7934 MILLER CLERK 7782 23-JAN-82 1300


10

14 rows selected.

2. write query to select only Employees name, id and Job


SQL> select empno,ename,job from emp;

EMPNO ENAME JOB


---------- ---------- ---------
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7566 JONES MANAGER
7654 MARTIN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
7788 SCOTT ANALYST
7839 KING PRESIDENT
7844 TURNER SALESMAN
7876 ADAMS CLERK

EMPNO ENAME JOB


---------- ---------- ---------
7900 JAMES CLERK
7902 FORD ANALYST
7934 MILLER CLERK

14 rows selected.

3. write query to select unique Jobs


SQL> select distinct job from emp;

JOB
---------
CLERK
SALESMAN
PRESIDENT
MANAGER
ANALYST
4. write query to select only those employees who are salesman
SQL> select ename from emp where job='SALESMAN';

ENAME
----------
ALLEN
WARD
MARTIN
TURNER
5. select employee name , grade and salary , in the order of their salary
SQL> select ename,sal from emp order by sal;

ENAME SAL
---------- ----------
SMITH 800
JAMES 950
ADAMS 1100
WARD 1250
MARTIN 1250
MILLER 1300
TURNER 1500
ALLEN 1600
CLARK 2450
BLAKE 2850
JONES 2975

ENAME SAL
---------- ----------
SCOTT 3000
FORD 3000
KING 5000

14 rows selected.

6. Mgmt. is considering a pay raise, however they want to find out, if they give a
flat 200/- increment to all, then what % each person is getting. So in your result
display, ename , salary and pctincr

SQL> select ename,sal,(sal+200) inc from emp;

ENAME SAL INC


---------- ---------- ----------
SMITH 800 1000
ALLEN 1600 1800
WARD 1250 1450
JONES 2975 3175
MARTIN 1250 1450
BLAKE 2850 3050
CLARK 2450 2650
SCOTT 3000 3200
KING 5000 5200
TURNER 1500 1700
ADAMS 1100 1300

ENAME SAL INC


---------- ---------- ----------
JAMES 950 1150
FORD 3000 3200
MILLER 1300 1500

14 rows selected.
7.Express work experience of each of the employees by using sysdate and
hiredate in terms of no of years. Hints : you would need to use cast

SQL> select ename,round(months_between(sysdate,hiredate)/12,0) as work from emp;


ENAME WORK
---------- ----------
SMITH 39
ALLEN 39
WARD 39
JONES 39
MARTIN 38
BLAKE 39
CLARK 39
SCOTT 33
KING 38
TURNER 38
ADAMS 33

JAMES 38
FORD 38
MILLER 38
14 rows selected.
8. Select only those employees who are a clerk and a manager. Use all of ‘or’
condition , ‘IN’ and ‘NOT IN’ clause Comment on the case sensitivity of the string
literal within single quote
SQL> select * from emp where job='CLERK' or job='MANAGER';

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20

7566 JONES MANAGER 7839 02-APR-81 2975


20

7698 BLAKE MANAGER 7839 01-MAY-81 2850


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-JUN-81 2450
10
7876 ADAMS CLERK 7788 23-MAY-87 1100
20

7900 JAMES CLERK 7698 03-DEC-81 950


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7934 MILLER CLERK 7782 23-JAN-82 1300
10
7 rows selected.
SQL> select * from emp where job in('CLERK','MANAGER');

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20

7566 JONES MANAGER 7839 02-APR-81 2975


20

7698 BLAKE MANAGER 7839 01-MAY-81 2850


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-JUN-81 2450
10

7876 ADAMS CLERK 7788 23-MAY-87 1100


20

7900 JAMES CLERK 7698 03-DEC-81 950


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7934 MILLER CLERK 7782 23-JAN-82 1300
10

7 rows selected.

SQL> select * from emp where job not in('CLERK','MANAGER');

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500


30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7788 SCOTT ANALYST 7566 19-APR-87 3000
20

7839 KING PRESIDENT 17-NOV-81 5000


10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7902 FORD ANALYST 7566 03-DEC-81 3000
20

7 rows selected.
SQL>

14 rows selected.

9. Use emp table and use different columns and string concatenation to display a
message like below for each of the employees Output Example: JAMES is a CLERK
and is working in the company for last 32 Years.
SQL> select ename||'is a '||job||'and is working in the company for'||round(mont
hs_between(sysdate,hiredate)/12,0)||'years' from emp;
ENAME||'ISA'||JOB||'ANDISWORKINGINTHECOMPANYFOR'||ROUND(MONTHS_BETWEEN(
SYSDATE,H
--------------------------------------------------------------------------------
SMITHis a CLERKand is working in the company for39years
ALLENis a SALESMANand is working in the company for39years

WARDis a SALESMANand is working in the company for39years


JONESis a MANAGERand is working in the company for39years
MARTINis a SALESMANand is working in the company for38years
BLAKEis a MANAGERand is working in the company for39years
CLARKis a MANAGERand is working in the company for39years
SCOTTis a ANALYSTand is working in the company for33years
KINGis a PRESIDENTand is working in the company for38years
TURNERis a SALESMANand is working in the company for38years
ADAMSis a CLERKand is working in the company for33years
ENAME||'ISA'||JOB||'ANDISWORKINGINTHECOMPANYFOR'||ROUND(MONTHS_BETWEEN(
SYSDATE,H
--------------------------------------------------------------------------------
JAMESis a CLERKand is working in the company for38years
FORDis a ANALYSTand is working in the company for38years
MILLERis a CLERKand is working in the company for38years
14 rows selected.

10. Use emp table to display only those employees who have joined in the year 80
and 81. Comment on if between clauses is inclusive or exclusive.
SQL> select * from emp where hiredate>'01-JAN-80'and hiredate<'31-DEC-81';

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800
20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300


30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7566 JONES MANAGER 7839 02-APR-81 2975
20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400


30

7698 BLAKE MANAGER 7839 01-MAY-81 2850


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7782 CLARK MANAGER 7839 09-JUN-81 2450
10

7839 KING PRESIDENT 17-NOV-81 5000


10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0


30

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7900 JAMES CLERK 7698 03-DEC-81 950
30

7902 FORD ANALYST 7566 03-DEC-81 3000


20

11 rows selected.
11.Use like statement to display name of the employees which start with ‘A’ Write
your remarks on use of wildcards with like statement

SQL> select ename from emp where ename like 'A%';

ENAME
----------
ALLEN
ADAMS
12. Select those employees , who has joined on or before 31st December 1982 and
is either a clerk or having a salary greater than 2500
SQL> select * from emp where hiredate='31-DEC-82' and job='CLERK' or sal>2500;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7566 JONES MANAGER 7839 02-APR-81 2975
20

7698 BLAKE MANAGER 7839 01-MAY-81 2850


30

7788 SCOTT ANALYST 7566 19-APR-87 3000


20

EMPNO ENAME JOB MGR HIREDATE SAL COMM


---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7839 KING PRESIDENT 17-NOV-81 5000
10

7902 FORD ANALYST 7566 03-DEC-81 3000


20

13. List down no of employees, minimum salary , maximum salary for each
department

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


COUNT(ENAME) MIN(SAL) MAX(SAL)
------------ ---------- ----------
6 950 2850
5 800 3000
3 1300 5000
14. Apart from ‘Delete’ a ‘Truncate’ statement can also be used for deleting the
rows. Comment on their difference.

TRUNCATE is the DDL statement whereas DELETE is a DML statement.TRUNCATE always


removes all the rows from a table, leaving the table empty and the table structure intact
whereas DELETE may remove conditionally if the where clause is used.

15. Display a department id wise count of employees getting salary more than
5000
SQL> select department_id,count(*) from employees where salary>5000 group by dep
artment_id;
DEPARTMENT_ID COUNT(*)
------------- ----------
100 6
30 1
1
20 2
70 1
90 3
110 2
40 1
50 5
80 34
60 2
11 rows selected.
16. Apart from the above condition, select only those departments which has an
average salary in excess of 6500
SQL> select department_id,count(*) from employees having avg(salary)<6500 group
by department_id;
DEPARTMENT_ID COUNT(*)
------------- ----------
30 6
50 45
10 1
60 5

17. Explain how two levels of filtering is happening based on firstly where clause
secondly having clause based on this particular scenario
 A WHERE clause is used is filter records from a result. The filter occurs before any
groupings are made.
 A HAVING clause is used to filter values from a group.
18. You want to add a new row in the employees table with employee id 10000,
First Name = ‘Scott’ , Last Name = ‘Tiger’ , Email = Stiger, Hire Date , 01/02/2014,
Job id PR_Prsdnt ( Title ‘Company President’ ) Department_id 280 (
Department_Name ‘Database’ ) Salary 50000
SQL>insert into emp(empno,ename,email-
id,hiredate,job,sal)values(1000,’scot’,’stiger’,TO__DATE(‘01/02/2014’,’DD/MM/YYYY’),’PRpr
edent’,50000);
19. Issue necessary insert statements.
SQL>update emp set email-id=’uxev345@gmail.com’ where empno=7369;
SQL>update emp set email-id=’abc 345@gmail.com’ where empno=7499;
Table altered.
20. After the update is over in the email column, use instr and substr to display
email id and domain information separately.
select substr(’uxev345@gmail.com’,1,8)substring_result from emp where emp no=7369;
22.Display day, month and year of the hire date of the employees
SQL> select ename,hiredate, to_char(hiredate,'dd'),to_char(hiredate,'mm'),to_cha
r(hiredate,'yyyy') "YYYY" from emp;
ENAME HIREDATE TO TO YYYY
---------- --------- -- -- ----
SMITH 17-DEC-80 17 12 1980
ALLEN 20-FEB-81 20 02 1981
WARD 22-FEB-81 22 02 1981
JONES 02-APR-81 02 04 1981
MARTIN 28-SEP-81 28 09 1981
BLAKE 01-MAY-81 01 05 1981
CLARK 09-JUN-81 09 06 1981
SCOTT 19-APR-87 19 04 1987
KING 17-NOV-81 17 11 1981
TURNER 08-SEP-81 08 09 1981
ADAMS 23-MAY-87 23 05 1987
JAMES 03-DEC-81 03 12 1981
FORD 03-DEC-81 03 12 1981
MILLER 23-JAN-82 23 01 1982
14 ows selected.

24.Select an appropriate query which displays the first name and last name of all
the employees , however if the firstname is not available display it as “Unknown”

SQL> select nvl (first_name,'unknown')last_name from employees;


LAST_NAME
--------------------
Ellen
Sundar
Mozhe
David
Hermann
Shelli
Amit
Elizabeth
Sarah
David
Laura
Harrison
Alexis
Anthony
Gerald
Nanette
John
Kelly
Karen
Curtis
Lex
Julia

Jennifer
Louise
Bruce
Alberto
Britney
Daniel
Pat
Kevin
Jean
Table emp1:
SQL> create table emp1( emp_id number, dept_id number, name varchar2(20), gender
varchar2(20), salary number, branch varchar2(20),doj date);

Table created.
SQL> insert into emp1 values(1,10,'harshita','female',10000,'kolkata',' 04-Nov-2
018');
1 row created.
SQL> insert into emp1 values(2,20,'akash','male',10000,'mumbai','18-Mar-2018');
1 row created.
SQL> insert into emp1 values(3,10,'Ishani','female',20000,'Kolkata','14-Nov-2015
');
1 row created.
SQL> insert into emp1 values(4,30,'Koushik','male',15000,'Delhi','28-Feb-2016');
1 row created.
SQL> insert into emp1 values(5,10,'Ishan','male',15000,'Kolkata','04-Nov-2018');
1 row created.
SQL> insert into emp1 values(6,20,'Mayank','male',20000,'Mumbai','04-Apr-2015');
1 row created.
SQL> insert into emp1 values(7,30,'Kitty','female',15000,'Delhi','09-Jan-2016');
1 row created.
SQL> select * from emp1;
EMP_ID DEPT_ID NAME GENDER SALARY BRANC
H DOJ
---------- ---------- -------------------- -------------------- ---------- -----
--------------- ---------
1 10 harshita female 10000 kolka
ta 04-NOV-18
2 20 akash male 10000 mumba
i 18-MAR-18
3 10 Ishani female 20000 Kolka
ta 14-NOV-15
4 30 Koushik male 15000 Delhi
28-FEB-16
5 10 Ishan male 15000 Kolka
ta 04-NOV-18

6 20 Mayank male 20000 Mumba


i 04-APR-15
7 30 Kitty female 15000 Delhi
09-JAN-16
7 rows selected.
28.How many employees are getting 10,000 salary?
SQL> select count(name) from emp1 where salary=10000;
COUNT(NAME)
-----------
2

ASSIGNMENT 3:
JOIN QUERIES:

1. Display name of employees, department name and job name for each
employee
SQL> select * from emp inner join dept on emp.deptno=dept.deptno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM D
EPTNO EMAILID DEPTNO DNAME LOC
---------- ---------- --------- ---------- --------- ---------- ---------- -----
----- -------------------- ---------- -------------- -------------
7782 CLARK MANAGER 7839 09-JUN-81 2450
10 10 ACCOUNTING NEW YORK
7839 KING PRESIDENT 17-NOV-81 5000
10 10 ACCOUNTING NEW YORK
7934 MILLER CLERK 7782 23-JAN-82 1300
10 10 ACCOUNTING NEW YORK
7566 JONES MANAGER 7839 02-APR-81 2975
20 20 RESEARCH DALLAS
7902 FORD ANALYST 7566 03-DEC-81 3000
20 20 RESEARCH DALLAS
7876 ADAMS CLERK 7788 23-MAY-87 1100
20 20 RESEARCH DALLAS
7369 SMITH CLERK 7902 17-DEC-80 800
20 20 RESEARCH DALLAS
7788 SCOTT ANALYST 7566 19-APR-87 3000
20 20 RESEARCH DALLAS
7521 WARD SALESMAN 7698 22-FEB-81 1250 500
30 30 SALES CHICAGO
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0
30 30 SALES CHICAGO
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300
30 30 SALES CHICAGO
7900 JAMES CLERK 7698 03-DEC-81 950
30 30 SALES CHICAGO
7698 BLAKE MANAGER 7839 01-MAY-81 2850
30 30 SALES CHICAGO
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400
30 30 SALES CHICAGO
14 rows selected.

2. Display the department name along with no of employees and average


salary of that department
SQL> select dname,count(ename),avg(sal) from emp, dept where
emp.deptno=dept.dep
tno group by dname;
DNAME COUNT(ENAME) AVG(SAL)
-------------- ------------ ----------
ACCOUNTING 3 2916.66667
RESEARCH 5 2175
SALES 6 1566.66667

3. For each department find out number of jobs the employees are assigned
to
SQL> select dname,count(ename) from emp, dept where emp.deptno=dept.deptno
group
by dname;
DNAME COUNT(ENAME)
-------------- ------------
ACCOUNTING 3
RESEARCH 5
SALES 6
6. Display name of those employees who get salary more than the average salary.
SQL> select ename,sal,average from(select avg(sal) as average from emp),empwher
e sal>average;
ENAME SAL AVERAGE
---------- ---------- ----------
JONES 2975 2073.21429
BLAKE 2850 2073.21429
CLARK 2450 2073.21429
SCOTT 3000 2073.21429
KING 5000 2073.21429
FORD 3000 2073.21429
6 rows selected.
SQL>

Assignment 4:

1. Find the names of sailors who have reserved boat 103.


SNAME
--------------------
Dustin
Lubber
Horatio

2. . Find the names of sailors who have reserved a red boat

SQL> select distinct sname from boats natural join reserves natural join sailors
where color='red';

SNAME
--------------------
Lubber
Dustin
Horatio

3. Find the colours of boats reserved by Lubber


SQL> select color from boats natural join reserves natural join sailors where sn
ame='Lubber';
COLOR
-----
red
red
green

4. Find the names of sailors who have reserved at least one boat
SQL> select snamefrom( select distinct sid,sname from sailors natural join rese
rves);

SNAME
--------------------
Dustin
Lubber
Horatio
Horatio

5. How many sailors are there within the range of 40-60 who have reserved a red
boat?

SQL> select count (*) from (select distinct sname from ( selectsid,sname from s
ailors where age between 40 and 60) natural join ( select bid, sid from reserves
) natural join ( select bid from boats where color='red'));

COUNT(*)
----------
2

6. Find the names of sailors who have reserved a red or a green boat
SNAME
--------------------
Dustin
Lubber
Horatio
Horatio

7.Find the name of sailors who have reserved a red and a green boat
SQL> select sname from (select distinct sid,sname from boats natural join reserv
es natural join sailors where color in('red','green'));
SQL> select sname from (select distinct sid,sname from sailors natural join rese
rves natural join boats where color='red') natural join (select distinct sid,sna
me from sailors natural join reserves natural join boats where color='green') ;

SNAME
--------------------
Dustin
Lubber

8.Find the name of sailors who have reserved at least two boats

SQL> select sname from (select sid,sname from sailors) natural join (select sid,
count(*) as cnt from reserves group by sid) where cnt>=2;

SNAME
--------------------
Dustin
Lubber
Horatio

9.On which date, boat 102 was reserved by Dustin?


SQL> select day from (select sid,sname from sailors where sname='Dustin') natura
l join reserves where bid=102;

DAY
---------
10-OCT-98

10. How many sailors are there who have same rating?

SQL> select count(*),rating from sailors group by rating;

COUNT(*) RATING
---------- ----------
1 1
3 8
2 7
1 3
2 10
1 9

6 rows selected.

11.Find the sids of sailors with age over 20 who have not reserved a red boat

SQL> select sid from (select sid from sailors where age > 20) natural join ( se
ectsid,bid from reserves) natural join (select bid from boats where color not
n 'red');

SID
----------
22
22
31
64
74

ASSIGNMENT 5
1. DISPLAY YOUR NAME 5 TIMES USING FOR LOOP.

DECLARE
name VARCHAR2(10) := 'writam';
BEGIN
FOR i IN 1 .. 5 LOOP
DBMS_OUTPUT.put_line(name);
END LOOP;
END;

output:
writam
writam
writam
writam
writam

2. WRITE A PL/SQL BLOCK OF CODE TO INVERT A NUMBER 12345 TO


54321.
declare

num varchar2(5):='12345';
lennumber(2);
revnum varchar2(5);
begin

len := length(num);

for i in reverse 1..len


loop

revnum := revnum || substr(num,i,1);


end loop;

dbms_output.put_line('given number ='|| num);


dbms_output.put_line('reverse number ='|| revnum);
end;
output:
given number =12345
reverse number =54321

3.WRITE A PL/SQL CODE BLOCK TO CALCULATE THE AREA OF CIRCLE FOR A


VALUE OF RADIUS VARYING FROM 3 TO 7. STORE THE RADIUS & THE
CORRESPONDING VALUES
create table areas(radius number(5),area number(14,2));

DECLARE
pi constant number(4,2):=3.14;
radius number(5);
area number(14,2);
BEGIN
radius:=3;
while(radius<=7)
LOOP
area:=pi*power(radius,2);
Insert into areas values(radius,area);
radius:=radius+1;
END LOOP;
dbms_output.put_line('Records are successfully inserted');
END;
/

set serveroutput on;

select * from areas;

RADIUS AREA
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86
4. CREATE A SIMPLE LOOP SUCH THAT A MESSAGE IS DISPLAYED WHEN A
LOOP EXCEEDS A PARTICULAR VALUE(WHILE LOOP).

DECLARE
a number(2) := 10;
BEGIN
WHILE a < 20 LOOP
dbms_output.put_line('value of a: ' || a);
a := a + 1;
END LOOP;
dbms_output.put_line('value of a exceeds 20');
END;
OUTPUT:

Statement processed
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a exceeds 20

5.WRITE A PL/SQL BLOCK CODEnto THAT WILL ACCEPT AN ACCOUNT


NUMBER FROM THE USER, CHECK IF THE USER'S BALANCE IS LESS THAN THE
MINIMUM BALANCE,
ONLY THEN DEDUCT RS. 100/- FROM THE AVAILABLE BALANCE. THE
PROCESS IS FIRED ON THE ACCOUNTS TABLE(Hint: create a table ACCOUNTS with
necessary attributes,
create table acct_master1(acct_nonumber(5) primary key,acct_name
varchar2(10),balance number(10));

insert into acct_master1 values(1,'aBI',1000);


insert into acct_master1 values(2,'sri',100);
insert into acct_master1 values(3,'riya',1100);
insert into acct_master1 values(4,'de',700);
insert into acct_master1 values(5,'ef',1700);
DECLARE
xacct_nonumber(5):=4;
xmin_balnumber(5):=1000;
xbalancenumber(5);
BEGIN
select balance into xbalance from acct_master where acct_no=xacct_no;

IF(xbalance<xmin_bal) THEN
update acct_master set balance=balance-100 where acct_no=xacct_no;
xbalance:=xbalance-100;
dbms_output.put_line('Rs 100 is deducted and current balance is'||xbalance);

ELSE
dbms_output.put_line('Current balance is'||xbalance);
END IF;

END;
output:

Table created.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

Statement processed.
Rs 100 is deducted and current balance is600

6.. BANK DECLARES 8% INTEREST ON CAPITAL. SO, UPDATE ALL ACCOUNTS


USING PL/SQL CODE BLOCK.

create table acc_master(acct_no number(5) primary key, acct_name varchar2(10),balance


number(10));

insert into acc_mastervalues(1, 'Alice', 1000);


insert into acc_mastervalues(2, 'alex', 100);
insert into acc_mastervalues(3, 'Raj', 1100);
insert into acc_mastervalues(4, 'harish', 400);
insert into acc_mastervalues(5, 'sri', 1700);
alter table acc_master add newbalancenumber(10);

DECLARE
a_nonumber(5);
new_balnumber(5);
balnumber(5);
inumber(1);
BEGIN
FOR i in 1 .. 5 LOOP
a_no:=i;
select balance into bal from acc_master where acct_no=a_no;
update acc_master set newbalance=balance*1.08 where acct_no=a_no;
new_bal:=bal*1.08;
dbms_output.put_line('Current balance is '||bal||' and new balance is '||new_bal);
END LOOP;
END;
/

output:
1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

Table altered.

Statement processed.
Current balance is 1000 and new balance is 1080
Current balance is 100 and new balance is 108
Current balance is 1100 and new balance is 1188
Current balance is 400 and new balance is 432
Current balance is 1700 and new balance is 1836

Assignment 6:

1.Write a PL/SQL block to find the second highest salary from the customer table
CREATE TABLE CUSTOMER1(ID NUMBER,NAME VARCHAR2(20),SALARY NUMBER);

INSERT INTO CUSTOMER1 VALUES(1,'SRI',1200);

INSERT INTO CUSTOMER1 VALUES (2,'RANI',500);

INSERT INTO CUSTOMER1 VALUES(3,'HKH',800);


INSERT INTO CUSTOMER1 VALUES(4,'SIKH',700);

INSERT INTO CUSTOMER1 VALUES(5,'SIhgKH',3400);

SELECT * FROM CUSTOMER1;

CREATE OR REPLACE PACKAGE CUSTOMERPACKAGE AS

FUNCTION SECOND
RETURN NUMBER;

END CUSTOMERPACKAGE;

CREATE OR REPLACE PACKAGE BODY CUSTOMERPACKAGE AS

FUNCTION SECOND

RETURN NUMBER IS

SEC NUMBER(4) := 0;

BEGIN

SELECT MAX(SALARY) INTO SEC FROM (SELECT SALARY FROM CUSTOMER1 WHERE SALARY
<(SELECT MAX(SALARY) FROM CUSTOMER1));

RETURN SEC;

END;

END CUSTOMERPACKAGE;

DECLARE

C NUMBER(5);

BEGIN
C := CUSTOMERPACKAGE.SECOND();

DBMS_OUTPUT.PUT_LINE('SECOND HIGHEST SALARY : ' || C);

END;

output:
Table created.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

Result Set 1
ID NAME SALARY
1 SRI 1200
2 RANI 500
3 HKH 800
4 SIKH 700
5 SIhgKH 3400

5 rows selected.

Package created.

Package Body created.

Statement processed.
SECOND HIGHEST SALARY : 1200

2.Write a function which will accept a four digit year and will print whether the
year is leap year or not.
create or replace function IS_LEAP_YEAR (nYear in number) return number
is
a number;
b number;
c number;
BEGIN
a := MOD(nYear,4);
b := MOD(nYear,100);
c := MOD(nYear,400);
IF ((a = 0 AND b <>0 ) OR c = 0) THEN
DBMS_OUTPUT.PUT_LINE(nYear || ' is a leap year');
return 1;
ELSE
DBMS_OUTPUT.PUT_LINE (nYear || ' is not a leap year');
return 0;
END IF;
END IS_LEAP_YEAR;
/
SELECT IS_LEAP_YEAR(1600) FROM DUAL;
/
Function created.
IS_LEAP_YEAR(1600)
1
1600 is a leap year
3.Write a procedure that will accept EMPNO and PROJNO and will display whether
the employee has worked in the given project. Also, write a program to call the
procedure.
(Sample output: Enter empno: 1
Enter projno: 2
No, Asha has not worked on the DBMS project
Students can create their own database as well to solve this)

create table employees (empnonumber,ename varchar2(20));


insert into employees values(1,'Tokyo');
insert into employees values(2,'Professor');
insert into employees values(3,'Nairobi');
insert into employees values(4,'Raquel');
insert into employees values(5,'Berlin');
select * from employees;
create table proj (projname varchar2(20),projno number);
insert into proj values('a',2);
insert into proj values('b',5);
insert into proj values('c',7);
select * from proj;
create table projemp (empnonumber,projnonumber,worked number);
insert into projempvalues(2,5,67);
insert into projempvalues(1,7,78);
insert into projempvalues(3,2,45);
select * from projemp;
create or replace procedure findfun(vempno in varchar2,vprojno in varchar2) is
venameemployees.ename%type;
vprojnameproj.projname%type;
vcntnumber(2);
begin
select count(*) into vcnt from projemp where empno=vempno and projno=vprojno;
if (vcnt>0) then
select ename into vename from employees where empno=vempno;
select projname into vprojname from proj where projno=vprojno;
DBMS_OUTPUT.PUT_LINE('yes '||vename||' worked on'||vprojname||' project');
else
DBMS_OUTPUT.PUT_LINE('no ' ||vename || ' doesnot work on '||vprojname||' project');
end if;
end findfun;
/
EXEC findfun(1,2);
output:
Procedure created.
Statement processed.
yes Tokyo worked on c project.

ASSIGNMENT 7:

1.Write a PL/SQL block of code that first withdraws an amount of Rs. 1000. Then
deposits
an amount of Rs. 1,40,000. Update the current balance. Then check to see that the
current
balance of ALL the accounts in the bank does not exceed Rs. 2,00,000. If the
balance exceeds,
then undo the deposit just made.(Hint: create EMP_MSTR table before writing this
block)

create table trans_mstr


(
trans_no varchar2(5),
acct_no varchar2(5),
dt date,
type varchar2(2),
particular varchar2(15),
dr_cr varchar2(2),
amtnumber(8),
balance number(9)
);
create table acct_mstr(acct_no varchar2(5),curbal number(10));
insert into acct_mstr values('ca10',8000);
insert into acct_mstr values('ca11',80000);

DECLARE
mbalnumber(8,2);
BEGIN
insert into trans_mstr values('t100','ca10','04-jul-2004','c','telephone','w',1000,31000);
update acct_mstr set curbal=curbal-1000 where acct_no='ca10';

savepointno_update;
insert into trans_mstr values('t101','ca10','04-jul-2004','c','deposite','s',140000,171000);
update acct_mstr set curbal=curbal+140000 where acct_no='ca10';
select sum(curbal) into mbal from acct_mstr;

if mbal>200000 then
rollback to savepointno_update;
end if;
commit;
END;
/
select * from trans_mstr;
select * from acct_mstr;
/
output:
Table created.

Table created.

1 row(s) inserted.

1 row(s) inserted.

Statement processed.

TRANS_NO ACCT_NO DT TYPE PARTICULAR DR_CR AMT BALANCE


t100 ca10 04-JUL-04 c telephone w 1000 31000

ACCT_NO CURBAL
ca10 7000
ca11 80000

2 rows selected.
2. The bank manager has decided to transfer employees across branches. Write a
PL/SQL block
to accept an employee number and the branch number followed by updating the
branch number of that
employee to which he belongs appropriately. Display an appropriate message
using SQL%FOUND based on
the existence of the record in the EMP_MSTR table. Otherwise, display message
using SQL%NOTFOUND based on the
non-existence of the record.

create table EMP_MSTR1 ( EMP_NO number, BRANCH_NO number);


insert into EMP_MSTR1 values(1200,89);
insert into EMP_MSTR1 values(1211,67);
insert into EMP_MSTR1 values(1870,80);
select * from EMP_MSTR1;

DECLARE
total_rowsnumber(2);
BEGIN
UPDATE EMP_MSTR1 SET BRANCH_NO = 80 WHERE EMP_NO = 1200;

IF SQL%FOUND THEN

total_rows := sql%rowcount;
dbms_output.put_line('Employee Successfully Transferred');
ELSE
dbms_output.put_line('Employee Number does not Exist');
END IF;

END;
/
output:
Table created.

1 row(s) inserted.

1 row(s) inserted.

1 row(s) inserted.

EMP_NO BRANCH_NO
1200 89
1211 67
1870 80
3 rows selected.

Statement processed.
Employee Successfully Transferred

EMP_NO BRANCH_NO
1200 80
1211 67
1870 80
3 rows selected.
.4.The bank manager has decided to mark all those accounts as inactive(I) on
which there are no transactions
performed in last 365 days. Whenever any such update takes place, a record for
the same is maintained in the
INACTV table comprising of the account number, the opening date and the type of
account.
Write a PL/SQL block to do the same.
Hint: create the table INACTV first.

CREATE TABLE INACTV_ACCT_MSTR (ACCT_NO VARCHAR2(10), OPNDT DATE);


create table ACCT_MSTR(ACCT_NO varchar2(20), STATUS varchar2(20), OPNDT DATE);
insert into ACCT_MSTR values('ca10','S','25-08-2016');
insert into ACCT_MSTR values('ca11','I','26-09-2017');
create table TRANS_MSTR(trans_no varchar2(5),acct_no varchar2(5),dtdate,type
varchar2(2),particular varchar2(15),dr_cr varchar2(2),amt number(8),balance number(9));
insert into trans_mstr values('t100','ca10','04-jul-2004','c','telephone','w',1000,31000);
DECLARE
CURSOR Crsr_NoTrans IS
SELECT ACCT_NO, STATUS, OPNDT FROM ACCT_MSTR WHERE ACCT_NO IN (SELECT
ACCT_NO FROM TRANS_MSTR GROUP BY ACCT_NO HAVING MAX(SYSDATE - DT) >365);
str_ACCT_NOACCT_MSTR.ACCT_NO%type;
str_STATUSACCT_MSTR.STATUS%type;
dt_OPNDTACCT_MSTR.OPNDT%type;
BEGIN
OPEN Crsr_NoTrans;
IF Crsr_NoTrans%ISOPEN THEN
LOOP
FETCH Crsr_NoTrans INTO str_ACCT_NO, str_STATUS, dt_OPNDT;
EXIT WHEN Crsr_NoTrans%NOTFOUND;
IF Crsr_NoTrans%FOUND THEN
UPDATE ACCT_MSTR SET STATUS = 'S' WHERE ACCT_NO = str_ACCT_NO;
INSERT INTO INACTV_ACCT_MSTR VALUES(str_ACCT_NO, dt_OPNDT);
END IF;
END LOOP;
COMMIT;
ELSE
dbms_output.put_line ('Unable to open Cursor');
END IF;
CLOSE Crsr_NoTrans;
END;
/
select * from INACTV_ACCT_MSTR;
/

Output:
Table created. Table created. 1 row(s) inserted. Statement processed.

Assignment 8:

2.Write a trigger which will not allow to delete records from emp_bkup.
(Hint: use if inserting/updating/deleting then......)

CREATE TABLE emp( enonumber,ename varchar2(20));


CREATE TABLE emp_bckupAS(SELECT * FROM emp);
CREATE OR REPLACE TRIGGER emp_trigger BEFORE DELETE
ON emp FOR EACH ROW
BEGIN

INSERT INTO emp_bckup VALUES (:old.eno,:old.ename);

END;
/
insert into emp values(1,'Shahrukh');
insert into emp values(2,'Salman');
select * from emp;
SELECT * FROM emp_bckup;
DELETE FROM emp;
SELECT * FROM emp_bckup;
/
output:
Table created.

Table created.

Trigger created.

1 row(s) inserted.
1 row(s) inserted.

ENO ENAME
1 Shahrukh
2 Salman

2 rows selected.

no data found

2 row(s) deleted.

ENO ENAME
1 Shahrukh
2 Salman

3.Write a trigger which will create a primary key automatically into a table.
create table test (id number, testdata varchar2(255));
insert into test values(1,'a');
insert into test values(2,'b');
insert into test values(3,'c');
create sequence test_seq
start with 1
increment by 1
nomaxvalue;
create trigger test_trigger
before insert on test
for each row
begin
select test_seq.nextval into :new.id from dual;
end;
/
commit;
select * from test;

Table created.

1 row(s) inserted.

1 row(s) inserted.
1 row(s) inserted.

Sequence created.

Trigger created.
Statement processed.

Result Set 1
ID TESTDATA
1 a
2 b
3 c
Download CSV
3 rows selected.
2 rows selected

You might also like