0% found this document useful (0 votes)
344 views8 pages

RDBMS SEM 3 Practical Solutions

ussenigg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views8 pages

RDBMS SEM 3 Practical Solutions

ussenigg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Downloaded from: www.sucomputersforum.

com Page 1 of 8

B.Com(Computer Applications) CBCS, Semester – III

Subject: Relational Database Management System

Computer Lab-Practical Solutions

Exercise – 1:
1. Create table EMP with columns emp_num, ename, sal and enter 10 records.
SQL>create table emp(emp_num number(3), ename varchar2(10),sal number(7,2));
Table created
Now inserting records into the emp table
SQL> insert into emp values(&emp_num,'&ename',&sal);
Enter value for emp_num : 101
Enter value for ename: waseem
Enter value for sal: 20000
old 1: insert into emp values(&emp_num,'&ename',&sal)
new 1: insert into emp values(101,'Raju',20000)

1 row created.

Press / enter to repeat the above statement to insert another record, repeat it for next 9 times,
after inserting the emp table is

SQL> select * from emp;

EMP_NUM ENAME SAL


--------- ---------- ---------
101 waseem 20000
102 raza 15000
103 shiva 18000
104 gangaram 17000
105 rashmika 1200
106 chandana 16000
107 sindhu 12000
108 bhargav 10000
109 anas 18000
110 sana 12000

10 rows selected.

2. Add columns dname,dept_num,location for emp table.


SQL> alter table emp add(dname varchar2(10),dept_num number(3),location varchar2(15));

Table altered.

After adding columns you need to update data for added columns to existing records in
emp table as shown below.
-Prepared by Mohammed Waseem Raza
Downloaded from: www.sucomputersforum.com Page 2 of 8

SQL> update employee set dname='ACCOUNTS',dept_num=10,location='HYD' where


emp_num=101;

1 row updated.

By using above syntax format update remaining 9 records with distinct values.

After updating emp table is

SQL > Select * from emp;

EMP_NUM ENAME SAL DNAME DEPT_NUM Location


--------- ---------- --------- ----------------- --------------- ------------------
101 waseem 20000 ACCOUNTS 10 HYD
102 raza 15000 SALES 20 PUNE
103 shiva 18000 HR 30 KNR
104 gangaram 17000 ACCOUNTS 10 HYD
105 rashmika 1200 SALES 20 PUNE
106 chandana 16000 MARKETING 40 JGL
107 sindhu 12000 ACCOUNTS 10 HYD
108 bhargav 10000 SALES 20 PUNE
109 anas 18000 MARKETING 40 JGL
110 sana 12000 HR 30 KNR

10 Rows Selected.

3. Rename the Emp table with Employee and modify the ename column size as 20.
SQL> rename emp to employee;
Table renamed.

SQL> alter table employee modify ename varchar2(20);

Table altered.
4. Display all the records from employee of dept_num 30
SQL>select * from employee where dept_num=30;

EMP_NUM ENAME SAL DNAME DEPT_NUM Location


--------- ---------- --------- ----------------- --------------- ------------------
103 shiva 18000 HR 30 KNR
110 sana 12000 HR 30 KNR

5. Display the employee details whose have 2A’s in their name.


SQL>select * from employee where ename like ‘%a%a%’;

-Prepared by Mohammed Waseem Raza


Downloaded from: www.sucomputersforum.com Page 3 of 8

EMP_NUM ENAME SAL DNAME DEPT_NUM Location


--------- ---------- --------- ----------------- --------------- ------------------
102 raza 15000 SALES 20 PUNE
103 shiva 18000 HR 30 KNR
104 gangaram 17000 ACCOUNTS 10 HYD
105 rashmika 1200 SALES 20 PUNE
106 chandana 16000 MARKETING 40 JGL
108 bhargav 10000 SALES 20 PUNE
109 anas 18000 MARKETING 40 JGL
110 sana 12000 HR 30 KNR

6. Drop the column dname and display details of employees whose salary greater than 15000.
SQL>alter table employee drop column dname;
Note: This query is not executing in our version.

SQL> select from employee where sal>15000;

EMP_NUM ENAME SAL DNAME DEPT_NUM Location


--------- ---------- --------- ----------------- --------------- ------------------
101 waseem 20000 ACCOUNTS 10 HYD
103 shiva 18000 HR 30 KNR
104 gangaram 17000 ACCOUNTS 10 HYD
106 chandana 16000 MARKETING 40 JGL
109 anas 18000 MARKETING 40 JGL
Exercise – 2:
1. Display the details of employees whose join date is 01/11/2020.
SQL> alter table employee add doj date;
Table altered.
Update the doj column values to ten records, after updating employee table is

SQL> select * from employee;

EMP_NUM ENAME SAL DNAME DEPT_NUM Location DOJ


--------- ---------- --------- ----------------- --------------- ------------------ ------------------
101 waseem 20000 ACCOUNTS 10 HYD 01-MAR-20
102 raza 15000 SALES 20 PUNE 05-AUG-15
103 shiva 18000 HR 30 KNR 25-DEC-19
104 gangaram 17000 ACCOUNTS 10 HYD 01-APR-20
105 rashmika 1200 SALES 20 PUNE 01-NOV-20
106 chandana 16000 MARKETING 40 JGL 20-NOV-20
107 sindhu 12000 ACCOUNTS 10 HYD 01-JAN-21
108 bhargav 10000 SALES 20 PUNE 05-FEB-21
109 anas 18000 MARKETING 40 JGL 01-MAR-21
110 sana 12000 HR 30 KNR 11-JAN-21
10 Rows Selected.

-Prepared by Mohammed Waseem Raza


Downloaded from: www.sucomputersforum.com Page 4 of 8

SQL>select * from employee where doj=’01-nov-2020’;

EMP_NUM ENAME SAL DNAME DEPT_NUM Location DOJ


--------- ---------- --------- ----------------- --------------- ------------------ ------------------
105 rashmika 1200 SALES 20 PUNE 01-NOV-20
2. Add column job to the employee table and list the clerks in the deptno of 10.
SQL>alter table employee add job varchar2(10);
Table altered.
And update job column values to all the records and table is
SQL> update employee set job='MANAGER' where dname='HR';
2 rows updated.
SQL> update employee set job='CLERK' where dname='ACCOUNTS';
3 rows updated.
SQL> update employee set job='SOFTWARE' where dname='SALES';
3 rows updated.
SQL> update employee set job='ADMIN' where dname='MARKETING';
2 rows updated.
SQL> select *from empoloyee;
EMP_NUM ENAME SAL DNAME DEPT_NUMLocation DOJ JOB
--------- ---------- --------- ----------------- --------------- ------------------ ------------------ ------------------
101 waseem 20000 ACCOUNTS 10 HYD 01-MAR-20 CLERK
102 raza 15000 SALES 20 PUNE 05-AUG-15 SOFTWARE
103 shiva 18000 HR 30 KNR 25-DEC-19 MANAGER
104 gangaram 17000 ACCOUNTS 10 HYD 01-APR-20 CLERK
105 rashmika 1200 SALES 20 PUNE 01-NOV-20 SOFTWARE
106 chandana 16000 MARKETING 40 JGL 20-NOV-20 ADMIN
107 sindhu 12000 ACCOUNTS 10 HYD 01-JAN-21 CLERK
108 bhargav 10000 SALES 20 PUNE 05-FEB-21 SOFTWARE
109 anas 18000 MARKETING 40 JGL 01-MAR-21 ADMIN
110 sana 12000 HR 30 KNR 11-JAN-21 MANAGER
10 Rows Selected.

SQL> select *from empoloyee where job=’clerk’ and dept_num=10;


EMP_NUM ENAME SAL DNAME DEPT_NUMLocation DOJ JOB
--------- ---------- --------- ----------------- --------------- ------------------ ------------------ ------------------
101 waseem 20000 ACCOUNTS 10 HYD 01-MAR-20 CLERK
104 gangaram 17000 ACCOUNTS 10 HYD 01-APR-20 CLERK
107 sindhu 12000 ACCOUNTS 10 HYD 01-JAN-21 CLERK

3. Display the details of employee whose salary is less than 10000

SQL>select * from employee where sal<10000;


EMP_NUM ENAME SAL DNAME DEPT_NUMLocation DOJ JOB
--------- ---------- --------- ----------------- --------------- ------------------ ------------------ ------------------
105 rashmika 1200 SALES 20 PUNE 01-NOV-20 SOFTWARE

4. Display the details of employee salaries in descending order.

SQL>select ename, sal from employee order by sal desc;

-Prepared by Mohammed Waseem Raza


Downloaded from: www.sucomputersforum.com Page 5 of 8

ENAME SAL
---------- ---------
waseem 20000
anas 18000
shiva 18000
gangaram 17000
chandana 16000
raza 15000
sindhu 12000
sana 12000
bhargav 10000
rashmika 1200
5.
6. Display the names of employee in upper case.

SQL>select upper(ename) from employee;

Upper(ENAME)
-----------------------------
WASEEM
RAZA
SHIVA
GANGARAM
RASHMIKA
CHANDANA
SINDHU
BHARGAV
ANAS
SANA

7. Display the names of the employees in lower case.

SQL>select lower(ename) from employee;

lower(ENAME)
-----------------------------
waseem
raza
shiva
gangaram
rashmika
chandana
sindhu
bhargav
anas
sana

-Prepared by Mohammed Waseem Raza


Downloaded from: www.sucomputersforum.com Page 6 of 8

Exercise – 3:
1. Find the Dept which has maximum number of employee.

SQL>select dname,count(*) from employee group by dname having count(*)=(select


max(count(*)) from employee group by dept_num);

2. List the year in which maximum number of employee was recruited.

SQL> select to_char(doj,'yyyy') year from employee group by to_char(doj,'yyyy') having


count(*)=(select max(count(*)) from employee group by to_char(doj,'yyyy'));

YEAR
-------
2017

3. Display the details of employees who are working for deptno 10 and 20.
SQL> select * from employee where dept_num in(10,20);
EMP_NUM ENAME SAL DNAME DEPT_NUMLocation DOJ JOB
--------- ---------- --------- ----------------- --------------- ------------------ ------------------ ------------------
101 waseem 20000 ACCOUNTS 10 HYD 01-MAR-20 CLERK
102 raza 15000 SALES 20 PUNE 05-AUG-15 SOFTWARE
104 gangaram 17000 ACCOUNTS 10 HYD 01-APR-20 CLERK
105 rashmika 1200 SALES 20 PUNE 01-NOV-20 SOFTWARE
107 sindhu 12000 ACCOUNTS 10 HYD 01-JAN-21 CLERK
108 bhargav 10000 SALES 20 PUNE 05-FEB-21 SOFTWARE
4. Update the HRA=15%, DA=10%, TA=10% for all the Employees whose is experience more
than 10 years.

Before update HRA,DA and TA we have to add three columns and update those values to
employee table.
Adding three columns:

SQL> alter table employee add(HRA number(7,2),DA number(7,2),TA number(7,2));

Table altered.
SQL> update employee set HRA=sal*0.15,DA=sal*0.1,TA=sal*0.1 where
(months_between(sysdate,doj)/12)>10;
No rows selected
5. Write a query to delete duplicate records from emp.

SQL> delete from employee where emp_num=(select emp_num from employee group by emp_num
having count(*)>1);

0 rows deleted.
-Prepared by Mohammed Waseem Raza
Downloaded from: www.sucomputersforum.com Page 7 of 8

6. Display the sum of salaries in department wise.


SQL> select dept_num,sum(sal) from employee group by dept_num;

DEPT_NUM SUM(SAL)
--------- ---------
10 49000
20 26200
30 30000
40 34000

Exercise – 4:
1. Make the duplicate table as emp12 on emp

SQL> create table emp12 as select * from employee;

Table created.

2. Add Constraint Primary Key for emp_num and dept_num columns for emp table

SQL> alter table employee add constraint con1 primary key(emp_num,dept_num);

Table altered.

3. Remove the referential integrity from employee table and dept table.

SQL> alter table employee drop constraint con1;

Table altered.

4. Display the name of Employees who earn the Highest salary in their respective
departments.

SQL> select ename from employee where (dept_num,sal) in(select dept_num,max(sal) from
employee group by dept_num);

ENAME
--------------
waseem
raza
shiva
anas

5. Display the employees whose job as manager.


SQL> select emp_num,ename,job from employee where job='MANAGER';

EMP_NUM ENAME JOB


--------- -------------------- ----------
103 shiva MANAGER
110 sana MANAGER

-Prepared by Mohammed Waseem Raza


Downloaded from: www.sucomputersforum.com Page 8 of 8

6. Display the details of employees whose name is ALLEN.


SQL> select emp_num,ename,sal from employee where ename='ALLEN';

no rows selected

Exercise – 5:
1. Display all rows from Emp Table. The System wait after every Screen full of information.
SQL> set pause on;
SQL> select emp_num,ename,sal from employee;

If you want to see the output of selected columns from employees press enter key then it display
selected columns
EMP_NUM ENAME SAL
--------- ---------- ---------
101 waseem 20000
102 raza 15000
103 shiva 18000
104 gangaram 17000
105 rashmika 1200
106 chandana 16000
107 sindhu 12000
108 bhargav 10000
109 anas 18000
110 sana 12000

2. Create view for emp table.


SQL> create view v1 as select * from employee;

View created.
3. Create a view for emp table where deptno=10.
SQL> create view v2 as select * from employee where dept_num=10;

View created.
4. Drop table the view of emp table.
SQL> drop view v1;

View dropped.
5. Delete all the records from the emp where the deptname is NULL.
SQL> delete from employee where dname is null;

0 rows deleted.
6. Delete the rows of employees whose experience is less than 5 year.
SQL> delete from employee where ((months_between(sysdate,doj))/12)<5;

10 rows deleted.

-Prepared by Mohammed Waseem Raza

You might also like