zoukankan      html  css  js  c++  java
  • 子查询

    --  谁的工资比Abel高?

    select  last_name, salary 
    from employees 
    where  salary>(select salary
                   from employees 
                   where  last_name = 'Abel'); 
    LAST_NAME                     SALARY
    ------------------------- ----------
    King                           24000 
    Kochhar                        17000 
    De Haan                        17000 
    Hartstein                      13000 

    --查询员工为Chen的manager的信息(Chen对应一个manager_id)

    select last_name,salary 
    from employees 
    where employee_id = (select manager_id
                         from employees 
                         where last_name = 'Chen');
    LAST_NAME                     SALARY
    ------------------------- ----------
    Greenberg                      12000 

    -- 返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id和工资

    select last_name, job_id,salary 
    from employees
    where  job_id = (select job_id 
                    from employees 
                    where employee_id=141) and
            salary >(select salary 
                    from employees
                    where employee_id = 143);        
    LAST_NAME                 JOB_ID         SALARY
    ------------------------- ---------- ----------
    Rajs                      ST_CLERK         3500 
    Davies                    ST_CLERK         3100 
    Nayer                     ST_CLERK         3200 
    Mikkilineni               ST_CLERK         2700 

    -- 查询最低工资大于50号部门的最低工资的部门id和其最低工资

    select department_id,min(salary)
    from employees 
    group by department_id
    having min(salary)>(select min(salary)
                         from employees
                         where department_id = 50);
    DEPARTMENT_ID MIN(SALARY)
    ------------- -----------
                         7000 
              100        6900 
               30        2500 

    -- 多行子查询

     --返回其他部门中比job_id为IT_PROG部门任一工资低的员工的员工号,姓名,job_id, salary

    select employee_id,last_name,job_id,salary
    from employees
    where job_id <> 'IT_PROG' AND salary < any (select salary
                                                  from employees
                                                  where job_id = 'IT_PROG');
    EMPLOYEE_ID LAST_NAME                 JOB_ID         SALARY
    ----------- ------------------------- ---------- ----------
            132 Olson                     ST_CLERK         2100 
            128 Markle                    ST_CLERK         2200 
            136 Philtanker                ST_CLERK         2200

    子查询中的空值  空值了不会报错 空了就空了

    练习:

    --1.查询平均工资最低的部门信息

    select * 
    from departments 
    where  department_id = (select department_id
                           from employees
                           having  avg(salary)  = (select min(avg(salary))
                           from employees
                           group by department_id
                           )
    group by department_id);                         
                         
    DEPARTMENT_ID DEPARTMENT_NAME                MANAGER_ID LOCATION_ID
    ------------- ------------------------------ ---------- -----------
               50 Shipping                              124        1500 

    2.从 jobs 表中返回  job_id 的对应项的信息

    select *
    from jobs
    where job_id in(
                     select job_id
                     from employees
                     having avg(salary) = (
                                      select max(avg(salary))
                                      from employees
                                      group by job_id
                                  )
                     group by job_id
    JOB_ID     JOB_TITLE                           MIN_SALARY MAX_SALARY
    ---------- ----------------------------------- ---------- ----------
    AD_PRES    President                                20000      40000 

    --3.查询平均工资高于公司平均工资的部门有哪些

    select department_id,avg(salary)
    from  employees
    group by  department_id
    having avg(salary)>(
                      select avg(salary)
                      from employees
     )order by department_id asc;
    DEPARTMENT_ID AVG(SALARY)
    ------------- -----------
               20        9500 
               40        6500 
               70       10000 
               80 8955.882353 

    -- 4.各个部门中,最高工资中最低的那个部门的最低工资是多少

    select min(salary)
    from employees
    where department_id = (select department_id
                           from employees
                           having max(salary) = (select min(max(salary)) 
                                                   from employees
                                                   group by department_id)
                           group by department_id       
                         );
    
    --各个部门最高工资最低的那个部门工资, 通过那个部门最高的工资 找到department_id,用到分组了最高得group by一下
    select department_id
    from employees
    having max(salary) = (select min(max(salary)) 
                           from employees
                           group by department_id)
    group by department_id;
    --各个部门最高工资最低的那个部门工资
    select min(max(salary)) 
    from employees
    group by department_id;
    MIN(SALARY)
    -----------
           4400 

    -- 5.查询1999年来公司的员工中最高工资的那个员工的信息

    select * 
    from employees
    where salary = (
                    select max(salary)
                    from employees
                    where to_char(hire_date,'yyyy') = '1999'
                    )
    and to_char(hire_date,'yyyy') = '1999';
    EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DATE JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
    ----------- -------------------- ------------------------- ------------------------- -------------------- --------- ---------- ---------- -------------- ---------- -------------
            148 Gerald               Cambrault                 GCAMBRAU                  011.44.1344.619268   15-10月-99 SA_MAN          11000            0.3        100            80 

    -- 6.查询和Zlotkey相同部门的员工姓名和雇用日期

    select last_name,hire_date
    from employees
    where department_id = (
                              select department_id
                              from employees
                            where last_name = 'Zlotkey'
                              )
    and last_name <> 'Zlotkey'
    LAST_NAME                 HIRE_DATE
    ------------------------- ---------
    Abel                      11-5月 -96 
    Taylor                    24-3月 -98 

    --7.查询工资比公司平均工资高的员工的员工号,姓名和工资。

    select last_name,employee_id,salary
    from employees
    where salary > (
                    select avg(salary)
                    from employees);
    LAST_NAME                 EMPLOYEE_ID     SALARY
    ------------------------- ----------- ----------
    King                              100      24000 
    Kochhar                           101      17000 

    -- 8.查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资, 两个salary都是同一个部门 得建立关系

    select employee_id,last_name,salary
    from employees e1
    where salary > (
                       select avg(salary)
                       from employees e2
                       where e1.department_id = e2.department_id
                       group by department_id
                   );
    EMPLOYEE_ID LAST_NAME                     SALARY
    ----------- ------------------------- ----------
            100 King                           24000 
            103 Hunold                          9000 

    --9.查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名

    select employee_id,last_name
    from employees
    where department_id in (
                               select department_id
                               from employees
                               where last_name like '%u%'
                                )
        and last_name not like '%u%';
    EMPLOYEE_ID LAST_NAME               
    ----------- -------------------------
            106 Pataballa                 
            107 Lorentz     

    --10.查询在部门的location_id为1700的部门工作的员工的员工号

    select employee_id
    from employees
    where department_id in (
                           select department_id
                           from departments
                           where location_id = 1700
                           );
    EMPLOYEE_ID
    -----------
            100 
            101 
            102 

    --11.查询管理者是King的员工姓名和工资

    select last_name,salary
    from employees
    where manager_id in (
                       select employee_id
                       from employees
                       where last_name = 'King'
                       )        
    LAST_NAME                     SALARY
    ------------------------- ----------
    Kochhar                        17000 
    De Haan                        17000 
    All that work will definitely pay off
  • 相关阅读:
    HashMap和Hashtable的区别
    java倒计时三种简单实现方式
    AngularJS---基本操作
    AngularJS---认识AngularJS
    Java线程面试题
    JAVA中高访问量高并发的问题怎么解决?
    Java高并发,如何解决,什么方式解决
    Map总结
    Github
    反射
  • 原文地址:https://www.cnblogs.com/afangfang/p/12551370.html
Copyright © 2011-2022 走看看