Oracle SQL 经典查询练手第四篇

您所在的位置:网站首页 收入最高的主播有那些名字 Oracle SQL 经典查询练手第四篇

Oracle SQL 经典查询练手第四篇

2024-07-13 09:30| 来源: 网络整理| 查看: 265

Oracle SQL 经典查询练手第四篇

本文分享的是OracleSQL的经典查询第四篇,仅仅是作者自己的见解,如有问题,希望您给出建议或者方法。同时,欢迎广大读者们补充,如果您有经典的查询方式也可以拿出来我们共同分享,共同成长,共同进步。

本计算机上使用的是Oracle11.2.0版本,使用scott用户登陆。使用的是系统自带的表。

表结构:

describe employees;

describe departments;

describe locations;

select * from employees;(由于空间限制,此表的行和列都没有截取全部,一共107行,11列)

select * from departments;(由于空间限制,此表的列截取了全部,行没有截取全部,一共27行)

select * from locations;

(由于空间限制,此表的行和列都没有截取全部,一共23行,6列)

----hr用户----

1.  各个部门平均、最大、最小工资、人数,按照部门号升序排列。

2.   各个部门中工资大于5000的员工人数。

3.  各个部门平均工资和人数,按照部门名字升序排列。

4. 列出每个部门中有同样工资的员工的统计信息,列出他们的部门号,工资,人数。

5. 列出同部门中工资高于1000 的员工数量超过2 人的部门,显示部门名字、地区名称。

6. 哪些员工的工资,高于整个公司的平均工资,列出员工的名字和工资(降序)。

7. 哪些员工的工资,介于50号 和80号部门平均工资之间。 

8. 所在部门平均工资高于5000 的员工名字。

9. 列出各个部门中工资最高的员工的信息:名字、部门号、工资。

10.最高的部门平均工资是多少。 

 

--1. 各个部门平均、最大、最小工资、人数,按照部门号升序排列。

select department_id ,avg(salary),max(salary),min(salary),count(*) from employees group by department_id order

by department_id asc;

--2. 各个部门中工资大于5000的员工人数。

select department_id,count(*) from employees where salary>5000 group by department_id; 

--3. 各个部门平均工资和人数,按照部门名字升序排列。  

select d.department_name,avg(e.salary),count(*)  from employees e,departments d where e.department_id=d.department_id group by d.department_name order by d.department_name;

--4. 列出每个部门中有同样工资的员工的统计信息,列出他们的部门号,工资,人数。

select e1.department_id,e1.salary,count(*) from employees e1,employees e2 where

e1.department_id=e2.department_id and e1.salary=e2.salary and e1.employee_ide2.employee_id group by

e1.department_id,e1.salary;

(由于空间限制,此表的行和列都没有截取全部,一共23行,3列)

--5. 列出同部门中工资高于1000 的员工数量超过2 人的部门,显示部门名字、地区名称。

(子查询方式)

select * from (select d.department_name,l.city, count(*) cnumber from employees e,departments d ,locations l

where e.department_id=d.department_id and d.location_id=l.location_id and e.salary>100

group by d.department_name,l.city

) where cnumber>2;

(having关键字方式)

select d.department_name,l.city, count(*) from employees e,departments d ,locations l where

e.department_id=d.department_id and d.location_id=l.location_id and e.salary>100

group by d.department_name,l.city having count(*)>2;

--6. 哪些员工的工资,高于整个公司的平均工资,列出员工的名字和工资(降序)。

select e1.first_name||' '||last_name as name ,e1.salary from employees e1 where e1.salary>(select avg(salary) from

employees);

(由于空间限制,此表的行和列都没有截取全部,一共51行,2列)

--7. 哪些员工的工资,介于50号 和80号部门平均工资之间。 

select e1.first_name first_name,e1.last_name last_name,e1.salary from employees e1 where e1.salary between

(select avg(salary) from employees where department_id=50)  and (select avg(salary) from employees where

department_id=80);(由于空间限制,此表的行和列都没有截取全部,一共43行,3列)

--8. 所在部门平均工资高于5000 的员工名字。

select  e.first_name first_name,e.last_name last_name from employees e where department_id in(select

department_id from employees group by department_id having avg(salary)>5000);  (由于空间限制,此表的行和列

都没有截取全部,一共54行,2列)

--9. 列出各个部门中工资最高的员工的信息:名字、部门号、工资。

(子查询方式)

select e1.first_name first_name,e1.last_namelast_name,e1.department_id,e1.salary from employees e1 ,(select

max(salary) maxsalary,department_id from employees e group by department_id) e2 where e1.salary=e2.max

 salary and e1.department_id=e2.department_id;

(in关键字方式)

select e1.first_name first_name,e1.last_namelast_name,e1.department_id,e1.salary  from employees e1 where

(e1.department_id,salary)in (select department_id,max(salary) from employees e group by department_id);

--10. 最高的部门平均工资是多少。 

(子查询方式)

select e.sal from (select avg(salary) sal,department_id from employees group by  department_id order by sal desc)

e where rownum=1;

(聚合函数方式)

select max(e.sal) from (select avg(salary) sal from employees group by department_id) e;

作者水平有限,难免有错误之处,殷切希望广大读者批评指正。

转载请注明出处:http://blog.csdn.net/gcw1024



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3