1 LAB Practical File
Sno. Lab no Page No
1. LAB I 3-6
2. LAB II 7-9
3. LAB III 10-11
4. LAB IV 12-17
5. LAB V 18-22
6. LAB VI (String Functions) 23-24
MS-239: Database Management System
2 LAB Practical File
LAB I : 8 August 2013
1. Write SQL query to display all Employee who are clerks and salesman.
SQL> SELECT * FROM Emp WHERE Job='CLERKS' OR Job='SALESMAN';
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
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
2. Write SQL query to display all the employees who are managers and earning
greater than 2500.
SQL> SELECT * from emp where job='MANAGER' and 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
3. Write SQL query to display all employees who were hired in year 1981.
SELECT * from Emp Where hiredate like '%81';
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
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
MS-239: Database Management System
3 LAB Practical File
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
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 30
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
10 rows selected.
4. Write SQL query to display the details of employees who are not working in
department no 30 and 40.
SQL> select * from emp where deptno not in (30,40);
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
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
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
8 rows selected.
5. Write SQL query to display details of employees in which s comes in their name.
SQL> select * from emp where ename like '%S%';
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
MS-239: Database Management System
4 LAB Practical File
7369 SMITH CLERK 7902 17-DEC-80 800 20
7566 JONES MANAGER 7839 02-APR-81 2975 20
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
5 rows selected.
6. Write SQL query to Find the names of the employees in each department having
highest salary?
SQL> select ename from emp where sal in(select max(sal) from
emp); ENAME
30 BLAKE
7. Write SQL query to Display all the employees who are managers and salesman and
earning salary>2500.
SQL> select * from emp where job = 'MANAGER' or job = 'SALESMAN' and sal>2500;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
CONTACTNO ADDRESS
7369 SMITH MANAGER 7902 17-DEC-80 5000 30
7566 JONES MANAGER 7839 02-APR-81 2356.2 20
7698 BLAKE MANAGER 7839 01-MAY-81 2257.2
30
7782 CLARK MANAGER 7839 09-JUN-81 2134.44
10
MS-239: Database Management System
5 LAB Practical File
8. Write SQL query to Display all the employees who are managers and earning salary
>2500 as well as all the salesman
SQL> select * from emp where job ='MANAGER' and sal>2500 or job= 'SALESMAN
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
CONTACTNO ADDRESS
7369 SMITH MANAGER 7902 17-DEC-80 5000 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1584 300
30
7521 WARD SALESMAN 7698 22-FEB-81 1237.5 500
30
MS-239: Database Management System
6 LAB Practical File
LAB II: 12 August 2013
1. Write SQL query to find out total salary of all the employees where job type is
MANAGER
SQL> select job,sum(sal) from emp where job='MANAGER' group by
job; JOB SUM(SAL)
MANAGER 8275
2. Write SQL query to find out total salary of all the employees where job type is
either Manager of Clerk.
SQL> select job,sum(sal) from emp where job='MANAGER' or job='CLERK' group by
job; JOB SUM(SAL)
CLERK 4150
MANAGER 8275
3. Write SQL query to Find the number of each job types in each department number
SQL> select job,count(job) from emp group by job;
JOB COUNT(JOB)
CLERK 4
SALESMAN 4
PRESIDENT 1
MANAGER 3
ANALYST 2
MS-239: Database Management System
7 LAB Practical File
4. Write SQL Query to find the average salary of each department.
SQL> select deptno,avg(sal) from emp where ename!='A%' group by deptno order by
deptno asc;
DEPTNO AVG(SAL)
10 2916.66667
20 2175
30 1566.66667
5. Find average salary for each department number and display only those
department numbers whose average salary > 2500
SQL> select job,sum(sal) from emp group by job having sum(sal)
>2500; JOB SUM(SAL)
CLERK 4150
SALESMAN 5600
PRESIDENT 5000
MANAGER 8275
ANALYST 6000a
6. Write SQL Query to find out the maximum salary in each department. Also
display the name of the employee.
SQL> select ename,deptno,sal from emp where sal in (select max(sal) from emp
gro up by deptno) ;
ENAME DEPTNO SAL
BLAKE 30 2850
MS-239: Database Management System
8 LAB Practical File
SCOTT 20 3000
KING 10 5000
FORD 20 3000
7. Write SQL query to find the average sal of employees of each department where no
of employee is greater than 3.
SQL> select job,avg(sal) from emp group by job having
count(*)>3; JOB AVG(SAL)
CLERK 1037.5
SALESMAN 1400
8. Write SQL query to count the no.of employee in each department.
SQL> select deptno,count(job) from emp group by deptno;
DEPTNO COUNT(JOB)
30 6
20 5
10 3
MS-239: Database Management System
9 LAB Practical File
LAB III: 26 August 2013 (Create, Insert and Delete)
# Creating Table CLASS
SQL> create table class (classno number(2) primary key,classname varchar2(25),block
varchar2(5));
Table created.
# Inserting into Table CLASS
SQL> insert into class values(10,'Finance','D');
1 row created.
SQL> insert into class values(11,'Marketing','D');
1 row created.
SQL> insert into class values(12,'IT','C');
1 row created.
# Creating Table Student
SQL> create table student(rollno number(10) primary key,SName varchar2(35)
unique, classno number(2) references class(classno));
Table created.
# Inserting into Table Student
SQL> insert into student values(123,'Aman',10);
1 row created.
SQL> insert into student values(124,'Harpreet',10);
1 row created.
SQL> insert into student values(125,'Anchal',11);
1 row created.
MS-239: Database Management System
10 LAB Practical File
# Deleting from table Student
SQL> delete from student where classno=10;
2 rows deleted.
SQL> delete from class where classno=10;
1 row deleted.
MS-239: Database Management System
11 LAB Practical File
LAB IV: 2 September 2013
1. Write SQL Query to Add the attribute „contactno‟ to the table emp.
SQL> alter table emp add(contactno number(20));
Table altered.
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
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 CONTACTNO
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 CONTACTNO
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 CONTACTNO
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
MS-239: Database Management System
12 LAB Practical File
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 CONTACTNO
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
2. Write SQL Query to Change the width of the job attribute to 40 characters.
SQL> alter table emp modify(JOB char(40));
Table altered.
3. Write SQL Query to Add a constraint unique on the attribute contact no.
SQL> alter table emp add constraint un unique(contactno);
Table altered.
4. Write SQL Quert to Drop the attribute „contact no.‟
SQL> alter table emp drop column contactno;
Table altered.
5. Write SQL Query to Change the designation of all salesmen to
„Marketing Executives‟ and give them a 20% salary hike.
SQL> update emp set job='marketing executive',sal=1.2*sal where
job='salesman'; 4 rows updated.
MS-239: Database Management System
13 LAB Practical File
6. Write SQL Query to Decrease the salaries of all managers by 5%.
SQL> update emp set sal=0.95*sal where job='manager';
SQL> SELECT * FROM EMP;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN MARKETING EXECUTIVE 7698 20-FEB-81 1920 300
30
7521 WARD MARKETING EXECUTIVE 7698 22-FEB-81 1500 500
30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
7566 JONES MANAGER 7839 02-APR-81 2826.25 20
7654 MARTIN MARKETING EXECUTIVE 7698 28-SEP-81 1500 1400
30
7698 BLAKE MANAGER 7839 01-MAY-81 2707.5 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO CONTACTNO
7782 CLARK MANAGER 7839 09-JUN-81 2327.5 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 CONTACTNO
7844 TURNER MARKETING EXECUTIVE 7698 08-SEP-81 1800
0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
MS-239: Database Management System
14 LAB Practical File
7900 JAMES CLERK 7698 03-DEC-81 950 30
14 rows selected.
7. Write SQL Query to Change the designation of Scott to manager and a give him
a 20% salary hike.
Ans. SQL> update emp set job='manager',sal=1.2*sal where ename like 'scott';
1 row updated.
8. Write SQL Query to display empno, dname and the location of each
employee working.
SQL> select empno,ename,loc,emp.deptno from emp,dept where emp.deptno=dept.deptno;
EMPNO ENAME LOC DEPTNO
7782 CLARK NEW YORK 10
7839 KING NEW YORK 10
7934 MILLER NEW YORK 10
7566 JONES DALLAS 20
7902 FORD DALLAS 20
7876 ADAMS DALLAS 20
7369 SMITH DALLAS 20
7788 SCOTT DALLAS 20
7521 WARD CHICAGO 30
7844 TURNER CHICAGO 30
7499 ALLEN CHICAGO 30
EMPNO ENAME LOC DEPTNO
7900 JAMES CHICAGO 30
MS-239: Database Management System
15 LAB Practical File
7698 BLAKE CHICAGO 30
7654 MARTIN CHICAGO 30
14 rows selected.
9. Write SQL Query to display empno, salary and depttno for the employee
WARD. SQL> select empno, sal,dname from emp,dept where emp.deptno=dept.deptno
and enam e='WARD';
EMPNO SAL DNAME
7521 1250 SALES
10. Write SQL Query to display department name,empno, and salary for
employees who are either Manager or Salesman.
SQL> select dname,empno,sal from emp,dept where dept.deptno=emp.deptno and (job=
'MANAGER'or JOB='SALESMAN');
DNAME EMPNO SAL
ACCOUNTING 7782 2450
RESEARCH 7566 2975
SALES 7844 1500
SALES 7698 2850
SALES 7521 1250
SALES 7499 1600
SALES 7654 1250
7 rows selected.
MS-239: Database Management System
16 LAB Practical File
11. Write SQL Query to Display empno. Deptno and location of employee whose
name is either SMITH or JONES.
SQL> select ename,emp.deptno,loc from emp,dept where emp.deptno=dept.deptno and
(ename='SMITH' or ename='JONES');
ENAME DEPTNO LOC
SMITH 20 DALLAS
JONES 20 DALLAS
MS-239: Database Management System
17 LAB Practical File
LAB V: 12 September 2013
1. Write SQL Query to Display name, hire date and review date of employees
of department 10 where review date is hire date + 90 days.
SQL> select ename,hiredate,hiredate+90 from emp where
deptno=10; ENAME HIREDATE HIREDATE+
CLARK 09-JUN-81 07-SEP-81
KING 17-NOV-81 15-FEB-82
MILLER 23-JAN-82 23-APR-82
2. Write SQL Query to Display name, hire date and review date of employees
of department 20 where review date is hire date + 6 months.
SQL> select ename,hiredate,add_months(hiredate,6) from emp where
deptno=20; ENAME HIREDATE ADD_MONTH
SMITH 17-DEC-80 17-JUN-81
WARD 22-FEB-81 22-AUG-81
JONES 02-APR-81 02-OCT-81
SCOTT 19-APR-87 19-OCT-87
ADAMS 23-MAY-87 23-NOV-87
FORD 03-DEC-81 03-JUN-82
6 rows selected.
3. Write SQL Query to Display name, number of weeks employed for employees
of department 30.
SQL> select ename,hiredate, (sysdate-hiredate)/7 from emp where deptno=30;
MS-239: Database Management System
18 LAB Practical File
ENAME HIREDATE (SYSDATE-HIREDATE)/7
ALLEN 20-FEB-81 1698.92869
MARTIN 28-SEP-81 1667.50012
BLAKE 01-MAY-81 1688.92869
TURNER 08-SEP-81 1670.35726
JAMES 03-DEC-81 1658.07155
4. Write SQL Query to display the hiredate for all the employees of department 20
in the format “15th August 2013”
SQL> select ename,hiredate,To_char((hiredate),'DD "of" MONTH,YYYY') from emp where
deptno=20;
ENAME HIREDATE TO_CHAR((HIREDATE),'
SMITH 17-DEC-80 17 of DECEMBER ,1980
WARD 22-FEB-81 22 of FEBRUARY ,1981
JONES 02-APR-81 02 of APRIL ,1981
SCOTT 19-APR-87 19 of APRIL ,1987
ADAMS 23-MAY-87 23 of MAY ,1987
FORD 03-DEC-81 03 of DECEMBER ,1981
6 rows selected.
5. Write SQL Query to Produce the hiredate announcement for all employees in
the format „employee hired on May 20th, 1992 at 16:27‟
SQL> select ename,hiredate,To_char((hiredate),'"Employee hired on" Month DDTH,YY
YY "at" HH:MI') from emp;
MS-239: Database Management System
19 LAB Practical File
ENAME HIREDATE
TO_CHAR((HIREDATE),'"EMPLOYEEHIREDON"MONTHDDTH
SMITH 17-DEC-80 Employee hired on December 17TH,1980 at 12:00
ALLEN 20-FEB-81 Employee hired on February 20TH,1981 at 12:00
WARD 22-FEB-81 Employee hired on February 22ND,1981 at 12:00
JONES 02-APR-81 Employee hired on April 02ND,1981 at 12:00
MARTIN 28-SEP-81 Employee hired on September 28TH,1981 at 12:00
BLAKE 01-MAY-81 Employee hired on May 01ST,1981 at 12:00
CLARK 09-JUN-81 Employee hired on June 09TH,1981 at 12:00
SCOTT 19-APR-87 Employee hired on April 19TH,1987 at 12:00
KING 17-NOV-81 Employee hired on November 17TH,1981 at 12:00
TURNER 08-SEP-81 Employee hired on September 08TH,1981 at 12:00
ADAMS 23-MAY-87 Employee hired on May 23RD,1987 at 12:00
ENAME HIREDATE
TO_CHAR((HIREDATE),'"EMPLOYEEHIREDON"MONTHDDTH
JAMES 03-DEC-81 Employee hired on December 03RD,1981 at 12:00
FORD 03-DEC-81 Employee hired on December 03RD,1981 at 12:00
MILLER 23-JAN-82 Employee hired on January 23RD,1982 at 12:00
14 rows selected.
6. Write a SQL Query to display the jobs of those employees who have salary 3500 as
„top management‟ and rest employees as „junior management‟.
SQL> select ename,sal,decode(sal,1300,'Junior Manager',1800,'Middle Manager',300
0,'Senior Manager') from emp;
MS-239: Database Management System
20 LAB Practical File
ENAME SAL DECODE(SAL,130
SMITH 175.76
ALLEN 1920
WARD 3500
JONES 39.57
MARTIN 1500
BLAKE 379.05
CLARK 325.85
SCOTT 3000 Senior Manager
KING 5000
TURNER 1800 Middle Manager
ADAMS 1100
ENAME SAL DECODE(SAL,130
JAMES 159.6
FORD 3000 Senior Manager
MILLER 1300 Junior Manager
14 rows selected.
7. Write a SQL Query to convert a name character string into next character
string. SQL> select ename, translate
(ename,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','BCDEFGHIJKLMNOPQ
RSTUVWXYZA')from emp;
MS-239: Database Management System
21 LAB Practical File
ENAME TRANSLATE(
SMITH TNJUI
ALLEN BMMFO
WARD XBSE
JONES KPOFT
MARTIN NBSUJO
BLAKE CMBLF
CLARK DMBSL
SCOTT TDPUU
KING LJOH
TURNER UVSOFS
ADAMS BEBNT
ENAME TRANSLATE(
JAMES KBNFT
FORD GPSE
MILLER NJMMFS
14 rows selected.
MS-239: Database Management System
22 LAB Practical File
LAB VI: 16 September 2013 (String Functions)
1. Write SQL Query to Create a table „Magazine‟ with attributes name, author‟s
name, head office and phone number.
Ans. SQL> SELECT * FROM MAGAZINE;
NAME ANAME HO PH
FORBES CHARLES, JOHN NEW YORK 011-2334567
OUTLOOK RISHI, SHARMA NEW DELHI 044-12234566
TIME RAGHURAM, RAJAN JAIPUR 022-345686946
2. Write SQL Query to Display the name before the comma in the author‟s name.
Ans. SQL> SELECT SUBSTR (ANAME,1,INSTR(ANAME,',',1,1)-1)"SUBSTRING"
FROM MAGAZINE;
SUBSTRING
CHARLES
RISHI
RAGHURAM
3. Write SQL Query to Display the rows which have head office in places that
sound like „Jaypur‟
Ans. SQL> SELECT* FROM MAGAZINE WHERE
SOUNDEX(HO)=SOUNDEX('JAYPUR');
NAME ANAME HO PH
TIME RAGHURAM, RAJAN JAIPUR 022-345686946
MS-239: Database Management System
23 LAB Practical File
4. Write a query to do right trimming on the author‟s name in the Magazine table.
Ans. SQL> SELECT RTRIM(ANAME,' ') FROM
MAGAZINE; RTRIM(ANAME,'')
CHARLES, JOHN
RISHI, SHARMA
RAGHURAM, RAJAN
5. Write a query to do left trimming on the author‟s name in the Magazine table.
SQL> SELECT LTRIM(ANAME,' ') FROM
MAGAZINE; LTRIM(ANAME,'')
CHARLES, JOHN
RISHI, SHARMA
RAGHURAM, RAJAN
6. Write SQL Query to to right pad the magazine name with „.‟ up to 30 characters.
Ans. SQL> SELECT RPAD(NAME,30,'.') FROM
MAGAZINE; RPAD(NAME,30,'.')
FORBES .....
OUTLOOK .....
MS-239: Database Management System