0% found this document useful (0 votes)
14 views5 pages

LAB5 A

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

Assignment 4

Q1. Display name of employees , department name and job name for each employee

SQL> select deptno, ename, job from emp;

DEPTNO ENAME JOB


---------- ---------- ----------
20 smith clerk
30 allen salesman
20 ward salesman
20 jones manager
30 martin salesman
30 blake manager
10 clark manager
20 scott analyst
10 king president
30 turner salesman
20 adams clerk
30 james clerk
20 ford analyst
10 miller clerk
14 rows selected.
Q2. Display the department name along with no of employees and average salary of that
department

SQL> select deptno, count(*), avg(sal) from emp group by deptno;

DEPTNO COUNT(*) AVG(SAL)


---------- ---------- ----------
30 5 1630
20 6 2308.33333
10 3 2916.66667

Q3. For each department, find out no. of jobs the employees are assigned to.

SQL> select job, count(deptno) from emp group by job;

JOB COUNT(DEPTNO)
---------- -------------
salesman 4 clerk
4
president 1
manager 3
analyst 2
Q4. Check for correctness of the above queries in terms of count, if you want to bring in
all entries, how would you achieve the same?

Q5. Group by the employees based on the first character of employee first name.
Display the results in alphabetic order (descending) of first character.

SQL> select * from emp order by ename desc;

EMPNO ENAME JOB HIREDATE SAL DEPTNO LABEL


---------- ---------- ---------- --------- ---------- ---------- ----------
7521 ward salesman 22-FEB-81 2975 20 senior
7844 turner salesman 08-SEP-81 1500 30 senior
7369 smith clerk 17-DEC-80 800 20 senior
7788 scott analyst 19-APR-87 3000 20 senior
7934 miller clerk 23-JAN-82 1300 10 senior
7654 martin salesman 28-SEP-81 1250 30 senior
7839 king president 17-NOV-81 5000 10 senior
7566 jones manager 02-APR-81 2975 20 senior
7900 james clerk 03-DEC-81 950 30 senior
7902 ford analyst 03-DEC-81 3000 20 senior
7782 clark manager 09-JUN-81 2450 10 senior

EMPNO ENAME JOB HIREDATE SAL DEPTNO LABEL


---------- ---------- ---------- --------- ---------- ---------- ----------
7698 blake manager 01-MAY-81 2850 30 senior
7499 allen salesman 20-FEB-81 1600 30 senior
7876 adams clerk 23-MAY-87 1100 20 senior

14 rows selected.

Q6. Display name of those employees who get a salary more than the average salary

SQL> select * from emp where sal>(select avg(sal) from emp);

EMPNO ENAME JOB HIREDATE SAL DEPTNO LABEL


---------- ---------- ---------- --------- ---------- ---------- ----------
7521 ward salesman 22-FEB-81 2975 20 senior
7566 jones manager 02-APR-81 2975 20 senior
7698 blake manager 01-MAY-81 2850 30 senior
7782 clark manager 09-JUN-81 2450 10 senior
7788 scott analyst 19-APR-87 3000 20 senior
7839 king president 17-NOV-81 5000 10 senior
7902 ford analyst 03-DEC-81 3000 20 senior
Q7. Display name of the all the employees who are ‘stock manager’, except the one
who gets the minimum salary. SQL> select ename from emp where job='manager'
and sal>(select min(sal)from emp where job='manager');
ENAME
----------
jones blake
Q8.
Display
firstname,
lastname,
salary of
those sales
representat
ives who
earns a
higher
salary than
the
minimum
salary a
sales
manager
receives.

SQL> select ename, sal from emp where job='salesman' and sal>(select min(sal) from emp
where job='manager');

ENAME SAL
---------- ---------- ward
2975

Q9.Display the name of the employees/employee who gets the second highest salary.
(sub query)
SQL> select max(sal) from emp where sal<(select max(sal) from emp);

MAX(SAL)
----------
3000

Q10. Come up with the query for previous question using set operators
SQL> select ename from emp where sal in(select max(sal) from emp where sal<(select
max(sal) from emp ));

ENAME
----------
scott ford
Q11. Display the name of the employee (manager) who has the maximum no. of
employees reporting to him.
Q12. Display the name of those employees , who are in the same department as
Timothy Gates and gets an salary more than the average salary of all the employees
.SQL> select ename from emp where deptno=(select deptno from emp where
ename='Timothy Gates') and sal>(select avg(sal) from emp);

no rows selected

Q13. If an employee have spent less than 5 years then he is considered entry level id 5
– 10 then midlevel else a senior employee. Write a query, which will label the
employees in either of the above categories
SQL> select ename, case when(sysdate - hiredate)/365<5 then 'Entry Level' when(sysdate -
hiredate)/365 between 5 and 10 then 'Mid Level' else 'senior' end as employeelevel from
emp;

ENAME EMPLOYEELEV
---------- -----------
smith senior allen
senior ward
senior jones
senior martin
senior blake
senior clark
senior scott
senior king
senior turner
senior
adams senior

ENAME EMPLOYEELEV
---------- -----------
james senior ford
senior
miller senior

14 rows selected.

Q14. Write query to find out any departments that are present in department table but
does not have employees
Q. 15. Write a query which will display job id , which are present in both job and
employee columns
Q. 16. Increase salary of each employee of all the department who draws the minimum
salary by 100$.
update emp set sal=sal+100 where sal = (select min(sal) from emp);
1 row updated. select * from emp;

EMPNO ENAME JOB HIREDATE SAL DEPTNO LABEL


---------- ---------- ---------- --------- ---------- ---------- ----------
7369 smith clerk 17-DEC-80 900 20 senior
7499 allen salesman 20-FEB-81 1600 30 senior
7521 ward salesman 22-FEB-81 2975 20 senior
7566 jones manager 02-APR-81 2975 20 senior
7654 martin salesman 28-SEP-81 1250 30 senior
7698 blake manager 01-MAY-81 2850 30 senior
7782 clark manager 09-JUN-81 2450 10 senior
7788 scott analyst 19-APR-87 3000 20 senior
7839 king president 17-NOV-81 5000 10 senior
7844 turner salesman 08-SEP-81 1500 30 senior
7876 adams clerk 23-MAY-87 1100 20 senior

EMPNO ENAME JOB HIREDATE SAL DEPTNO LABEL


---------- ---------- ---------- --------- ---------- ---------- ----------
7900 james clerk 03-DEC-81 950 30 senior
7902 ford analyst 03-DEC-81 3000 20 senior
7934 miller clerk 23-JAN-82 1300 10 senior

14 rows selected.

You might also like