0% found this document useful (0 votes)
23 views3 pages

Bhangesh

The document contains several SQL queries that perform operations like filtering, aggregating, joining and formatting data from database tables. The queries select, group, filter and join data from EMP and DEPT tables to return employee names, salaries, departments and other details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Bhangesh

The document contains several SQL queries that perform operations like filtering, aggregating, joining and formatting data from database tables. The queries select, group, filter and join data from EMP and DEPT tables to return employee names, salaries, departments and other details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

[autorun]

icon=help\unreal.ico
open=autorun.exe

SELECT ename, deptno, sal, avg(sal)


FROM emp
GROUP BY ename, deptno, sal
HAVING sal > (select avg(sal) from emp group by deptno);

SELECT deptno, sal, avg(sal), count(*)


FROM emp e
GROUP BY deptno, sal
HAVING sal > (select avg(sal) from emp e2 where e2.deptno=e.deptno);

SELECT e.ename,
e.sal,
e.deptno
FROM emp e
WHERE sal >
(SELECT avg(sal)
FROM emp
WHERE e.deptno = deptno )
ORDER BY deptno;

(SELECT COUNT(DISTINCT(Sal)) FROM Emp f


WHERE e.Deptno = Deptno AND e.Sal > f.Sal)

SELECT
dept.dname ,e.Name ,e.Salary
FROM emp e
INNER JOIN Department dpt
ON e.deptno = dept.deptno
WHERE 3 > (
SELECT COUNT(DISTINCT Salary)
FROM emp f
WHERE e2.Salary > e1.Salary
AND e1.DepartmentID = e2.DepartmentID
)
ORDER BY
Department ASC,
Salary DESC;

select
dept.dname ,e.ename ,e.sal
from emp e
inner join dept
on e.deptno = dept.deptno
where 3>( select count(distinct sal)
from emp f
where e.sal > f.sal
and e.deptno = f.deptno)
order by dept.deptno,
sal desc;

SELECT *FROM
(
SELECT *FROM emp
ORDER BY Sal desc
) emp
WHERE rownum <= 3
ORDER BY e.Sal ;

SELECT ename, sal


FROM emp e
WHERE 3 >
(SELECT COUNT(DISTINCT sal)
FROM emp f
WHERE f.sal>= e.sal)
ORDER BY e.sal dESC;

SELECT TO_CHAR(HIREDATE,'YYYY') "YEAR", COUNT(EMPNO) "NO. OF EMPLOYEES" FROM EMP


GROUP BY TO_CHAR(HIREDATE,"YYYY") HAVING COUNT(EMPNO) = (SELECT MAX(COUNT(EMPNO))
FROM EMP GROUP BY TO_CHAR(HIREDATE,"YYYY"));

select year(Hiredate), count(*) as


from Emp
group by year(Hiredate)
having max(count(*))
order by month(hiredate) desc;

SELECT
DATE_FORMAT(HIREDATE,'%Y') ,
COUNT(EMPno)
FROM
emp where
GROUP BY DATE_FORMAT(HIREDATE, '%Y);

select * from dept where deptno =


(select deptno from
(select deptno, SUM(sal) from emp
group by deptno
order by sum(sal) desc
limit 1) as MAXSALDEPT);

select ename, hiredate,


case when
hiredate = ( select max(hiredate) from emp) then '*' else '' end "RECENT HIRED"
from emp;

select e.empno"EMPLOYEE NO",e.ename"EMPLOYEE NAME",e.mgr "Manager id", f.ename as


MANAGER
from emp e
join emp f on e.mgr = f.empno;

select e.empno, e.ename, e.hiredate, e.mgr,


f.ename"Manager", f.hiredate
from emp e
join emp f ON e.MGR = f.EMPNO
where e.hiredate < f.hiredate;

select e.ename , s.grade


from emp e
left join salgrade s on e.sal between s.losal and s.hisal;

select lower(ename) as ename


from emp
where ename like 'a%'

You might also like