0
点赞
收藏
分享

微信扫一扫

sql练习

1.查看公司的平均奖金率

select SUM(commisstion_pct)/count(ifnull(commisstion,0))
from employees;

2.查看部门ID为10,20,30,40这四个部门中最高工资比10000高的部门信息
方式一


select department_id,MAX(salary)
from employees
where department_id in(10,20,30,40)
group by department_id
having MAX(salary)>10000;

方式二

select department_id,MAX(salary)
from employees
group by department_id
having MAX(salary)>10000 and department_id in(10,20,30,40);

方式一执行效率高于方式二

结论:

当过滤条件中有聚合函数时,则此过滤函数必须声明在having中。

当过滤条件中没有聚合函数时,则此过滤条件声明在where或者having中都可以。但是建议声明在where中。

3.查找谁的工资比Abel高?

select Last_name,salary
from employees
where salary>(select salary
             from employees
             where last_name='Abel');

4、查找job_id与141员工相同,salary比143号员工多的员工姓名,job_id和工资

select last_name,job_id,salary
from employees
where job_id=(select job_id from employees
             where job_id=141)
and salary>(select salary from employees
            where job_id=143)

5.返回公司工资最少的员工的last_name,job_id,salary

select last_name,job_id,salary 
from employees
where salary=(select min(salary) 
              from employees
              );

6.查询与141号员工的manager_id和department_id相同的其他员工的employee_id,manager_id,department_id

select employee_id,manager_id,department_id
from employees
where manager_id=(select manager_id 
                  from employees

                 where employee_id=141 )
and department_id=(select department_id 
                  from employees
                  where employee_id=141 )
and employee_id  <>141;

7.查询最低工资大于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
);

8.显示员工的employee_id,last_name和location。其中员工的department_id 与location_id为1800的department_id 相同,则location为‘Canada,其余为‘USA’
9.查找其他job_id中比job_id为‘IT-PROG’部门任一工资低的姓名,员工号,和工资

select last_name,job_id,salary
from employees
where job_id<>'IT_PROG'
and salary < ANY (
              select salary 
              from departments
              where job_id='IT_PROG'
               );

9.1查找其他job_id中比job_id为‘IT-PROG’部门任一工资低的姓名,员工号,和工资

select last_name,job_id,salary
from employees
where job_id<>'IT_PROG'
and salary < all (
              select salary 
              from departments
              where job_id='IT_PROG'
               );

10.查询平均工资最低的部门id
方式1

oracel版

select department_id
from employees
group by department_id
having avg(salary)=(
                     select min(avg(salary)) 
                     from EMPLOYEES
                     group by department_id;
                   )

mysql版:聚合函数不支持嵌套

select department_id
from employees
group by department_id
having avg(salary)=(
                    select min(avg_sal) 
                    from (select avg(salary) avg_sal
						  from employees 
						  group by department_id
                          ));

方式2

--查所有部门的平均工资,小于等于平均工资  即  最低平均工资
select department_id
from employees
group by department_id
having avg(salary)<=(
                     select avg(salary) avg_sal
                     from employees
                     group by department_id
                     );

举报

相关推荐

SQL练习

Sql练习

sql在线练习

sql练习2

SQL语言练习

SQL查询练习

Hive SQL 练习

SQL语句练习

0 条评论