select sal
from emp
where ename = 'JONES';
select ename, sal, deptno from emp
where sal > 2975;
select ename, sal, deptno from emp
where sal > (select sal from emp where ename = 'JONES');
select ename, sal deptno from emp
where sal > (select avg(sal) from emp) ;
select ename, sal, deptno
from emp
where sal = (select min(sal) from emp group by deptno);
select ename, sal, deptno
from emp
where sal in (select min(sal) from emp group by deptno);
select ename, sal, deptno
from emp
where sal
in (950, 800, 1300);
select ename, sal, deptno
from emp
where sal = 950
or sal = 800
or sal = 1300;
select ename, sal, deptno
from emp
where sal > any( select avg(sal) from emp group by deptno);
select ename, sal, deptno
from emp
where sal > 1566
or sal > 2175
or sal > 2916;
select ename, sal, deptno
from emp
where sal > (select min(avg(sal)) from emp group by deptno);
select ename, sal, deptno
from emp
where sal > all(select avg(sal)from emp group by deptno
select ename, sal, deptno
from emp
where sal > 1566
and sal > 2175
and sal > 2916;
select ename, sal, deptno
from emp
where sal > (select max(avg(sal)) from emp group by deptno);
select ename
from emp
where deptno = (select deptno from emp where lower(ename) = 'smith');
select ename
from emp
where deptno = (select deptno from emp where lower(ename) = 'smith')
and lower(ename) != 'smith';
문제2. 사번이 7369인 사원과 직업 같고, 급여는 더 많이 받는 사원의 이름, 직업, 급여
select ename, job, sal
from emp
where sal > (select sal from emp where empno = 7369)
and job = (select job from emp where empno = 7369);
문제3. emp, dept
sales 부서
select ename
from emp
where deptno = (select deptno from dept where upper(dname) = 'SALES')
and hiredate between to_date('1981-01', 'yyyy-mm') and to_date('1981-06', 'yyyy-mm');
select ename, hiredate
from emp
where hiredate
between to_date('19810101', 'yyyymmdd') and to-date('19810630', 'yyyymmdd')
and deptno = (select deptno from dept where upper(dname)= 'SALES');
select e.deptno, d.dname, e.ename, e.sal
from emp e, dept d
where e.deptno = d.deptno
and lower(job) = 'manager';
select last_name
from employees
where employee_id not in (select manager_id from employees where manager_id is not null);
select last_name
from employees
where employee_id not in (select nvl(manager_id, 0) from employees);
select employee_id, manager_id, department_id
from empl_demo
where (manager_id, department_id) in
(select manager_id, department_id
from empl_demo
where first_name = 'John')
and first_name != 'John';
select employee_id, manager_id, department_id
from empl_demo
where manager_id in
(select manager_id
from empl_demo
where first_name = 'John')
and department_id in
(select department_id
from empl_demo
where first_name = 'John')
and first_name <> 'John';
select ename, deptno
from emp
where deptno = (select deptno from emp where lower(ename) = 'smith')
and ename != 'Smith';
select ename, job, sal
from emp
where job = (select job from emp where empno = 7369)
and sal > (select sal from emp where empno = 7369);
select empno, ename, hiredate, sal+comm "income"
from emp
where deptno = (select deptno from dept where dname = 'SALES')
and hiredate between to_date('1981-01', 'yyyy-mm') and to_date('1981-06', 'yyyy-mm')
order by hiredate;
select ename
from emp
where deptno = (select deptno from dept where upper(dname) = 'SALES')
and hiredate between to_date('1981-01', 'yyyy-mm') and to_date('1981-06', 'yyyy-mm');
select e.deptno, d.dname, e.ename, e.sal
from emp e, dept d
where e.deptno = d.deptno
and job = 'MANAGER';
select ename
from emp
where mgr = (select empno from emp where ename = 'KING');
select ename, deptno, sal
from emp
where sal = (select min(sal) from emp);
select ename, sal, job
from emp
where sal > (select avg(sum(sal)) from emp group by ename);
select job, avg(sal)
from emp
group by job
having avg(sal) = (select min(avg(sal))from emp group by job);
select deptno, dname, loc
from dept
where deptno in (select deptno from emp where deptno is not null);
select e.ename, e.sal, d.dname
from emp e
join dept d
on e.deptno = d.deptno
select grade_level from job_grades between lowest_sal and highest_sal);
select *
from salgrade;
select e.ename, e.sal, d.dname
from emp e
join dept d
on e.deptno = d.deptno
and sal between (select losal from salgrade where grade = '5')
and (select hisal from salgrade where grade = '5');
select deptno, dname, loc
from dept
where deptno not in (select deptno from emp where deptno is not null);
select empno, ename
from emp
where empno in (select mgr from emp where mgr is not null);
select empno, ename
from emp
where empno not in (select mgr from emp where mgr is not null);
select empno, ename, deptno, sal
from emp
select e.deptno, d.dname, e.empno, e.ename, e.sal
from emp e
join dept d
on e.deptno = d.deptno
and sal > (select avg(sal)
from emp e1
where e1.deptno=d.deptno )
and job = 'MANAGER';
상관 쿼리
select deptno, dname, loc,
nvl((select count(ename) from emp where deptno = d.deptno group by deptno), '0') as cnt
from dept d
group by deptno, dname, loc;
SELECT d.deptno, d.dname, d.loc, nvl(e.cnt, '0') "cnt"
FROM (SELECT deptno, count(empno) cnt FROM EMP group by deptno) e
right join DEPT D
on E.DEPTNO = D.DEPTNO;
SELECT d.deptno, d.dname, d.loc, nvl(e.cnt, '0') "cnt"
FROM (SELECT deptno, count(empno) cnt FROM EMP group by deptno) e, DEPT D
where E.DEPTNO(+) = D.DEPTNO;
select deptno, dname, loc,
nvl((select count(ename) from emp where deptno = d.deptno group by deptno), '0') as cnt
from dept d;
select d.deptno, d.dname, d.loc,
(select count(*) from emp
where deptno = d.deptno) cnt
from dept d;
select last_name, salary, department_id
from employees outer_table
where salary in
(select min(salary)
from employees inner_table
where inner_table.department_id = outer_table.department_id);
select department_id, employee_id, salary
from employees
where (department_id, salary) in
(select department_id, min(salary)
from employees
group by department_id);
select employee_id, last_name
from employees
where employee_id in (select manager_id from employees) ;
select employee_id, last_name
from employees o
where exists (select manager_id
from employees
where manager_id = o.employee_id) ;
select employee_id, last_name
from employees o
where exists (select 1
from employees
where manager_id = o.employee_id) ;
select employee_id, last_name
from employees o
where not exists (select 1
from employees
where manager_id = o.employee_id) ;
select employee_id, last_name
from employees
where employee_id not in (select manager_id from employees
where manager_id is not null);
select e.empno, e.ename, e.sal, e.deptno, a.avg
from emp e join (select deptno, avg(sal) as avg
from emp
group by deptno) a
on e.deptno = a.deptno
and e.sal > a.avg;
select ename, sal, deptno from emp e
where sal > (select avg(sal) from emp where deptno = e.deptno);
select deptno, sum(sal)
from emp
group by deptno
having sum(sal) > (select avg(sum(sal)) from emp group by deptno);
with emp_sum
as (select deptno, sum(sal) sum from emp group by deptno)
select *
from emp_sum
where sum > (select avg(sum) from emp_sum);
select job_id,
(select count(employee_id) from employees where job_id = j.job_id) "now_cnt",
nvl((select count(employee_id) from job_history where job_id = j.job_id and sysdate > end_date group by job_id), '0') "history_cnt"
from jobs j
group by job_id;
select *
from job_history;
SELECT d.deptno, d.dname, d.loc, nvl(e.cnt, '0') "cnt"
FROM (SELECT deptno, count(empno) cnt FROM EMP group by deptno) e
right join DEPT D
on E.DEPTNO = D.DEPTNO;
SELECT d.deptno, d.dname, d.loc, nvl(e.cnt, '0') "cnt"
FROM (SELECT deptno, count(empno) cnt FROM EMP group by deptno) e, DEPT D
where E.DEPTNO(+) = D.DEPTNO;
select deptno, dname, loc,
nvl((select count(ename) from emp where deptno = d.deptno group by deptno), '0') as cnt
from dept d;
select d.deptno, d.dname, d.loc,
(select count(*) from emp
where deptno = d.deptno) cnt
from dept d;