0% found this document useful (0 votes)
35 views

DBMS Week 4

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)
35 views

DBMS Week 4

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/ 17

Week-4

EMPLOYEE DATABASE

1. EMP(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
2. DEPT (DEPTNO, DNAME, LOC).
3. SALGRADE (GRADE, LOSAL, HISAL).

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


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

DEPT TABLE

DEPTNO DNAME LOC


10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SALGRADE table

GRADE LOSAL HISAL


1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
Create tables:

create table emp (

empno number(4) primary key,

ename varchar(10),

job varchar(9),

mgr number(4),

hiredate date,

sal number(7, 2),

comm number(7, 2),

deptno number(2) references dept(deptno) -- foreign key to dept table

);

create table dept (

deptno number(2) primary key,

dname varchar(14),

loc varchar(13)

);

create table salgrade (

grade number(1) primary key,

losal number(7, 2),

hisal number(7, 2)

);

Inserting values:

insert into dept (deptno, dname, loc) values

(10, 'accounting', 'new york'),

(20, 'research', 'dallas'),


(30, 'sales', 'chicago'),

(40, 'operations', 'boston');

insert into salgrade (grade, losal, hisal) values

(1, 700, 1200),

(2, 1201, 1400),

(3, 1401, 2000),

(4, 2001, 3000),

(5, 3001, 9999);

insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values

(7369, 'smith', 'clerk', 7902, to_date('17-dec-80', 'dd-mon-yy'), 800, null, 20),

(7499, 'allen', 'salesman', 7698, to_date('20-feb-81', 'dd-mon-yy'), 1600, 300, 30),

(7521, 'ward', 'salesman', 7698, to_date('22-feb-81', 'dd-mon-yy'), 1250, 500, 30),

(7566, 'jones', 'manager', 7839, to_date('02-apr-81', 'dd-mon-yy'), 2975, null, 20),

(7654, 'martin', 'sales man', 7698, to_date('28-sep-81', 'dd-mon-yy'), 1250, 1400, 30),

(7698, 'blake', 'manager', 7839, to_date('01-may-81', 'dd-mon-yy'), 2850, null, 30),

(7782, 'clark', 'manager', 7839, to_date('09-jun-81', 'dd-mon-yy'), 2450, null, 10),

(7788, 'scott', 'analyst', 7566, to_date('19-apr-87', 'dd-mon-yy'), 3000, 0, 20),

(7839, 'king', 'president', null, to_date('17-nov-81', 'dd-mon-yy'), 5000, null, 10),

(7844, 'turner', 'sales man', 7698, to_date('08-sep-81', 'dd-mon-yy'), 1500, null, 30),

(7876, 'adams', 'clerk', 7788, to_date('23-may-87', 'dd-mon-yy'), 1100, null, 20),

(7900, 'james', 'clerk', 7698, to_date('03-dec-81', 'dd-mon-yy'), 950, null, 30),

(7902, 'ford', 'analyst', 7566, to_date('03-dec-81', 'dd-mon-yy'), 3000, null, 20),

(7934, 'miller', 'clerk', 7782, to_date('23-jan-82', 'dd-mon-yy'), 1300, null, 10);


Queries:

1. Display all different job types.

SQL> select distinct(job) from emp;

2. List the details of all employees in deptno 10 and 20 in alphabetical order.


SQL> select ename,deptno from emp
2 where deptno = 10 or deptno = 20
3 order by ename;

3. List the names of employees who have “th” or “ll” in their names
SQL> select ename from emp
2 where ename like '%th%' or ename like '%ll%';

4. List the names, jobs and salaries of all employees who have a manger.
SQL> select ename,job,sal from emp
2 where mgr is not null;
5. Give name remuneration of all employees.
SQL> select ename,(sal + nvl(comm,0)) as remuniration from emp;

6. List name and salary increased by 15% of all employees.


SQL> select ename,(sal+(sal*(15/100))) as inr_sal from emp;

7. Find all the employees who were hired during 1982.


2 where hiredate like '%82';

8. Display name, annual salary, commission of all salesmen whose monthly salary is
greater than commission.
SQL> select ename,job,comm from emp
2 where job = 'salesman' and (sal/12 > nvl(comm, 0));

9. Find average salary and average total remuneration of all employees other than salesman.
SQL> select avg(sal), avg(sal + nvl(comm,0)) as avg_rem from emp
2 where job not like 'salesman';
10. Find maximum, minimum and average salaries in each department.
SQL> select deptno, min(sal),max(sal),avg(sal) from emp
2 group by deptno;

11. Find the maximum, minimum and average salaries in each job.
SQL> select job, min(sal),max(sal),avg(sal) from emp
2 group by job;

12. Find the departments which have more than three employees.
SQL> select count(empno), deptno from emp
2 group by deptno
3 having count(empno) > 3;

13. Display employee names and their respective department numbers.


SQL> select ename,deptno from emp;
14. Give the salary grades for all the employees.
SQL> select ename, job, sal
2 from emp e
3 where sal = (select max(sal) from emp where job = e.job);

15. Display the employee names who earn highest salary in each job.
SQL> select ename, job, sal
2 from emp e
3 where sal = (select max(sal) from emp where job = e.job);

16. Find the employee details whose salary is greater than blake’s salary.
SQL> select * from emp where sal > (select sal from emp where ename = 'blake');

17. Find employee details of employees who have the same job and salary as that scott.
SQL> select * from emp where job = (select job from emp where ename = 'scott')
2 and sal = (select sal from emp where ename = 'scott');

18. Display the maximum salaries in accounting and research department.


SQL> select max(sal) as max_salary, deptno
2 from emp
3 where deptno in (10, 20)
4 group by deptno;
19. Display salary grades of all employees except of those employees whose salary grade is 3
and 4.
SQL> select e.ename, e.sal, s.grade
2 from emp e
3 join salgrade s
4 on e.sal between s.losal and s.hisal
5 where s.grade not in (3, 4);

20. Give the names and salaries of the employees whose salary is maximum in their
respective departments.
SQL> select ename, sal, deptno
2 from emp e
3 where sal = (select max(sal) from emp where deptno = e.deptno);

21. List the employees whose salary is greater than the salaries of all employees who are
working as salesman.
SQL> select * from emp
2 where sal > (select max(sal) from emp where job = 'salesman');

22. Write a query which will return the day of the week entered in the format of sysdate.
SQL> select to_char(sysdate, 'day') as day_of_week from dual;
23. Find the difference between highest and lowest salaries.
SQL> select (max(sal) - min(sal)) as salary_difference from emp;

24. Generate the output as smith – clerk.


SQL> select ename || ' – ' || job as emp_details from emp where ename = 'smith';

25. Generate the output as smith(Clerk).


SQL> select ename || '(' || job || ')' as emp_details from emp where ename = 'smith';

26. Give the details of all employees those who r working as manager.
SQL> select * from emp where job = 'manager';

27. List the departments where there are no employees.


SQL> select d.dname
2 from dept d
3 left join emp e
4 on d.deptno = e.deptno
5 where e.deptno is null;

28. Generate the following list: EMPLOYEE NAMME JOB SAL GRADE
SQL> select e.ename, e.job, e.sal, s.grade
2 from emp e
3 join salgrade s
4 on e.sal between s.losal and s.hisal;
29. List the information of those employees in department number 10.
SQL> select * from emp where deptno = 10;

30. Find the department location of james.


SQL> select d.loc
2 from dept d
3 join emp e
4 on d.deptno = e.deptno
5 where e.ename = 'james';

31. Get the manager of jones.


SQL> select e2.ename as manager_name
2 from emp e1
3 join emp e2
4 on e1.mgr = e2.empno
5 where e1.ename = 'jones';

32. Get the sub – ordinates of jones.


SQL> select ename
2 from emp
3 where mgr = (select empno from emp where ename = 'jones');

33. Write a query to calculate the length of time of all employees with the company.
SQL> select ename, round((sysdate - hiredate)/365, 2) as years_with_company
2 from emp;
34. List out name, job, salary, grade, department name of all employees who are not clerks.
SQL> select e.ename, e.job, e.sal, s.grade, d.dname
2 from emp e
3 join salgrade s
4 on e.sal between s.losal and s.hisal
5 join dept d
6 on e.deptno = d.deptno
7 where e.job != 'clerk';

35. List out name, job, salary, grade, department name of all employees who are clerks.
SQL> select e.ename, e.job, e.sal, s.grade, d.dname
2 from emp e
3 join salgrade s
4 on e.sal between s.losal and s.hisal
5 join dept d
6 on e.deptno = d.deptno
7 where e.job = 'clerk';

36. Display each employee with name, hiredate and review date -1 year from now.
SQL> select ename, hiredate, add_months(sysdate, -12) as review_date
2 from emp;
37. List out minimum salary of those employees under manager.
SQL> select min(sal) as min_salary, mgr
2 from emp
3 where mgr is not null
4 group by mgr;

38. Print the employee names and list salary as


i. ‘above target’ if salary is greater than 1500
ii. ‘below target’ if salary is lesser than 1500
iii. ‘on the target’ if salary is exactly 1500.
SQL> select ename,
2 case
3 when sal > 1500 then 'above target'
4 when sal < 1500 then 'below target'
5 else 'on the target'
6 end as salary_status
7 from emp;

39. Display ename, loc and dnames of the employees who earn more than 1500 salary.
SQL> select e.ename, d.loc, d.dname
2 from emp e
3 join dept d
4 on e.deptno = d.deptno
5 where e.sal > 1500;
40. Who are the top 3 earners in the company?
SQL> select ename, sal
2 from emp
3 order by sal desc
4 fetch first 3 rows only;

41. Write a query to display a ‘*’ against the employee who joined most recently.
SQL> select ename,
2 case
3 when hiredate = (select max(hiredate) from emp) then '*'
4 else ''
5 end as recently_joined
6 from emp;

42. Find the most recently joined employee in the company in each department in order of hire
date.
SQL> select ename, deptno, hiredate
2 from emp
3 where (deptno, hiredate) in (select deptno, max(hiredate) from emp group by deptno)
4 order by hiredate desc;
43. Find all the employees who joined before their manager.
SQL> select e1.ename as employee_name, e2.ename as manager_name
2 from emp e1
3 join emp e2
4 on e1.mgr = e2.empno
5 where e1.hiredate < e2.hiredate;

44. List jobs and department names of employees whose names are 5 letters long and must
begin with ‘ALL’ and end with ‘N’.
SQL> select job, d.dname
2 from emp e
3 join dept d
4 on e.deptno = d.deptno
5 where length(ename) = 5
6 and ename like 'all%n';

45. List employees whose commission is greater than 25% of their salary.
SQL> select ename, sal, comm
2 from emp
3 where comm > (sal * 0.25);

46. List all the departments with at least 2 clerks.


SQL> select d.dname
2 from dept d
3 join emp e
4 on d.deptno = e.deptno
5 where e.job = 'clerk'
6 group by d.dname
7 having count(e.empno) >= 2;

47. List employees in dept 30 with job not found in dept 10.
SQL> select ename, job, deptno
2 from emp
3 where deptno = 30
4 and job not in (select distinct job from emp where deptno = 10);

48. Who earns the second highest salary?


SQL> select ename, sal
2 from emp
3 order by sal desc
4 offset 1 row fetch next 1 row only;

49. Display the employee hire date in the format of “Friday, 10-AUG-03”.
SQL> select ename, to_char(hiredate, 'day dd-mon-yy') as formatted_hiredate
2 from emp;

50. Count the people in department 30 who receive both salary and commission.

SQL> select count(*)


2 from emp
3 where deptno = 30
4 and comm is not null;
51. List all the employees who were hired between “1 st January 1981” and “15th April 1985”. In
the order of their seniority.
SQL> select ename, hiredate
2 from emp
3 where hiredate between to_date('01-jan-1981', 'dd-mon-yyyy')
4 and to_date('15-apr-1985', 'dd-mon-yyyy')
5 order by hiredate;

52. Display name and period of service till date.


SQL> select ename, round((sysdate - hiredate)/365, 2) as period_of_service
2 from emp;

53. Write a query to calculate the length of time any employee have been with the company
using define.
SQL> select ename, round((sysdate - hiredate)/365, 2) as years_with_company
2 from emp;
54. Write a query which will return the day of the week for any entered date in the format
DD:MM:YY.
SQL> select to_char(to_date('12-10-24', 'dd-mm-yy'), 'day') as day_of_week
2 from dual;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy