几个sql多表查询语句,with as
2013-01-04 17:30
441 查看
SQL> create table employees
2 (
3 emp_id number(2),
4 name varchar(30),
5 department_id number(4),
6 job_id varchar(30),
7 salary number(8,2),
8 hire_date date);
insert into employees values (1,'zs',10,'xs',2000,to_date('2011-11-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (2,'ls',20,'mj',12000,to_date('2010-11-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (3,'zs1',10,'xs',5000,to_date('2010-01-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (4,'ls1',20,'mj',6000,to_date('2010-12-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (5,'zs2',10,'xs',11000,to_date('2001-11-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (6,'ls2',20,'mj',18000,to_date('2010-01-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (7,'zs3',10,'xs',50000,to_date('2018-11-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (8,'ls3',20,'mj',80000,to_date('2018-19-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
1、查询1到50部门中 哪些年份入职的员工超过1人.
显示年份,人数,其中年份为4位 ,人数按降序排列
SQL> select substr(to_char(hire_date,'yyyy-mm-dd hh24:mi:ss'),1,4),count(*) from employees group by substr(to_char(hire_date,'yyyy-mm-dd hh24:mi:ss'),1,4) having count(*) >1 order by 2 desc;
SUBS COUNT(*)
---- ----------
2010 4
2018 2
2、那些员工的工资高于该部门的平均工资,显示姓名工资,以工资降序排列
SQL> with t1 as (select department_id,avg(salary) salary from employees group by department_id) select employees.name,employees.salary from employees,t1 where employees.department_id=t1.department_id and employees.salary>t1.salary order by employees.salary desc;
NAME SALARY
------------------------------ ----------
ls3 80000
zs3 50000
2 (
3 emp_id number(2),
4 name varchar(30),
5 department_id number(4),
6 job_id varchar(30),
7 salary number(8,2),
8 hire_date date);
insert into employees values (1,'zs',10,'xs',2000,to_date('2011-11-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (2,'ls',20,'mj',12000,to_date('2010-11-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (3,'zs1',10,'xs',5000,to_date('2010-01-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (4,'ls1',20,'mj',6000,to_date('2010-12-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (5,'zs2',10,'xs',11000,to_date('2001-11-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (6,'ls2',20,'mj',18000,to_date('2010-01-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (7,'zs3',10,'xs',50000,to_date('2018-11-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
insert into employees values (8,'ls3',20,'mj',80000,to_date('2018-19-11 11:11:11','yyyy-mm-dd hh24:mi:ss'));
1、查询1到50部门中 哪些年份入职的员工超过1人.
显示年份,人数,其中年份为4位 ,人数按降序排列
SQL> select substr(to_char(hire_date,'yyyy-mm-dd hh24:mi:ss'),1,4),count(*) from employees group by substr(to_char(hire_date,'yyyy-mm-dd hh24:mi:ss'),1,4) having count(*) >1 order by 2 desc;
SUBS COUNT(*)
---- ----------
2010 4
2018 2
2、那些员工的工资高于该部门的平均工资,显示姓名工资,以工资降序排列
SQL> with t1 as (select department_id,avg(salary) salary from employees group by department_id) select employees.name,employees.salary from employees,t1 where employees.department_id=t1.department_id and employees.salary>t1.salary order by employees.salary desc;
NAME SALARY
------------------------------ ----------
ls3 80000
zs3 50000
相关文章推荐
- 提高数据库SQL语句查询速度的几个方法(转)
- oracle sql语句之多表查询
- sql查询语句时怎么把几个字段拼接成一个字段
- 常用的几个SQL 查询语句
- oracle关于查询空间使用情况的几个sql语句
- SQL语句(十三)多表查询
- SQL语句查询最近几天,几个小时的数据的写法
- 常用的SQL语句<二>几个高级查询运算词
- SQL 多表一起查询的语句总结
- 几个oracle的sql查询语句
- 整理出的数据库中几个常用sql查询语句
- [access查询]将access表导出为TXT文件及将TXT文件导入access表的几个SQL语句
- SQL 语句的多表查询方式
- SQL 多表一起查询的语句总结
- SQL多表查询语句
- SQL 多表一起查询的语句总结
- SQL多表查询优化 高效率SQL语句 11条原则
- 关于学生成绩查询的几个SQL语句
- 【SQL】With as 与row number()实现分页查询的sql语句
- 常用的几个SQL 查询语句