Oracle经典查询练手第一篇

--1.	哪些部门的人数比90 号部门的人数多。  
select department_id ,count(*)
from employees
group by department_id
having count(*)>(select count(*) from employees where department_id=90);

--2.	Den(FIRST_NAME)、Raphaely(LAST_NAME)的领导是谁(非关联子查询)。  
select  first_name||' '||last_name from employees where employee_id = (
  select manager_id from employees where first_name='Den' and last_name='Raphaely');

--3.	Den(FIRST_NAME)、Raphaely(LAST_NAME) 领导谁(非关联子查询)。  
select  first_name||' '||last_name from employees where manager_id = (
  select employee_id from employees where first_name='Den' and last_name='Raphaely');

--4.	Den(FIRST_NAME)、Raphaely(LAST_NAME) 的领导是谁(关联子查询)。  
select first_name||' '||last_name 
from employees a
where exists
  (select 1 from employees b
    where a.employee_id=b.manager_id
      and b.first_name='Den' 
      and b.last_name='Raphaely');
      
--5.	Den(FIRST_NAME)、Raphaely(LAST_NAME) 领导谁(关联子查询)。 
select first_name||' '||last_name 
from employees a
where exists
  (select 1 from employees b
    where a.manager_id=b.employee_id
      and b.first_name='Den' 
      and b.last_name='Raphaely');
      
--6.	列出在同一部门共事,入职日期晚但工资高于其他同事的员工:名字、工资、入职日期(关联子查询)。  
select first_name||' '||last_name 
from employees a
where exists
  (select 1 from employees b
    where a.department_id=b.department_id
      and a.hire_date>b.hire_date
      and a.salary>b.salary );


--7.	哪些员工跟Den(FIRST_NAME)、Raphaely(LAST_NAME)不在同一个部门(非关联子查询)。 
select first_name||' '||last_name 
from employees 
where department_id <>
  (select department_id from employees 
    where first_name='Den' 
      and last_name='Raphaely');

--8.	哪些员工跟Den(FIRST_NAME)、Raphaely(LAST_NAME)不在同一个部门(关联子查询)。 
select first_name||' '||last_name 
from employees a
where exists
  (select 1 from employees b
    where a.department_id<>b.department_id
      and b.first_name='Den' 
      and b.last_name='Raphaely');
      
      
--9.	Finance部门有哪些职位(非关联子查询)。 
select distinct job_id
from employees 
where department_id =
  (select department_id from departments 
    where department_name='Finance');
    
    
--10.	Finance部门有哪些职位(关联子查询)。
select distinct job_id
from employees a
where exists
  (select 1 from departments b
    where department_name='Finance'
    and a.department_id=b.department_id);

你可能感兴趣的:(Oracle经典查询练手第一篇)