两个表
dept(dept_id number(2)/*部门编号*/,dep_name varchar2(40) /*部门名称*/)
emp(emp_id number(6)/*员工编号*/,emp_name varchar(20)/*员工名称*/,dep_id number(2) /*部门编号*/)
问题:写出一条sql 展示部门人数在5人以上的员工信息:“员工编号 ,员工姓名,部门名称,部门人数”。
dept(dept_id number(2)/*部门编号*/,dep_name varchar2(40) /*部门名称*/)
emp(emp_id number(6)/*员工编号*/,emp_name varchar(20)/*员工名称*/,dep_id number(2) /*部门编号*/)
问题:写出一条sql 展示部门人数在5人以上的员工信息:“员工编号 ,员工姓名,部门名称,部门人数”。
where dept_id in
(
select dept_id from emp group by dept_id
having count(dept_id) >= 5
)
select emp.emp_id, emp.emp_name, dept.dep_name, sub.cnt
from emp emp, dept dept,
(select a.dep_id dep_id, count(1) cnt from emp a, dept b
where a.dep_id = b.dept_id
group by a.dep_id) sub
where emp.dep_id = dept.dept_id
and emp.dep_id = sub.dep_id
order by emp.dep_id;
from emp,
dep,
(select count(1) num, dept_id
from emp
group by dept_id
having count(1) > 5) s
where emp.dept_id = s.dept_id
and dep.dept_id = s.dept_id
where dept_id in
(
select dept_id from emp group by dept_id
having count(dept_id) >= 5--过滤掉人数在5以下的部门
)
--这个问题也就是一个简单的子查询,楼主遇到类似问题应该先分析一下逻辑顺序,一步一步实现就好了