DBMS Week 4
DBMS Week 4
EMPLOYEE DATABASE
1. EMP(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
2. DEPT (DEPTNO, DNAME, LOC).
3. SALGRADE (GRADE, LOSAL, HISAL).
DEPT TABLE
ename varchar(10),
job varchar(9),
mgr number(4),
hiredate date,
);
dname varchar(14),
loc varchar(13)
);
hisal number(7, 2)
);
Inserting values:
insert into emp (empno, ename, job, mgr, hiredate, sal, comm, deptno) values
(7654, 'martin', 'sales man', 7698, to_date('28-sep-81', 'dd-mon-yy'), 1250, 1400, 30),
(7844, 'turner', 'sales man', 7698, to_date('08-sep-81', 'dd-mon-yy'), 1500, null, 30),
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;
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;
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');
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;
26. Give the details of all employees those who r working as manager.
SQL> select * from emp where job = 'manager';
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;
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;
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);
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);
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.
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;