初学者的SQL任务

时间:2013-10-21 06:01:48

标签: sql oracle task

我有3个问题,我无法解决 存在2个表

CREATE TABLE T_DEPARTMENT
(
  ID    NUMBER,
  NAME  VARCHAR2(100 BYTE)

CREATE TABLE T_EMPLOYEE
(
  ID             NUMBER,
  DEPARTMENT_ID  NUMBER,
  CHIEF_ID       NUMBER,
  NAME           VARCHAR2(100 BYTE),
  SALARY         NUMBER
)

问题:

1 - 获取部门名称和员工人数,员工人数超过20人

2 - 按部门显示总预算的消费百分比

3 - 找到比该部门最高工资的员工低10%的首薪。

我尝试这些,都不正确:

1

select id from t_employee group by id having count(*) <= 20

2

select sum (salary) from t_employee group by id

3

select e1.name from t_employee e1, t_employee e2 where e2.id = e1.chief_id and e1.salary < e2.salary*0.1 all is uncorrect

2 个答案:

答案 0 :(得分:0)

  

IN sql server

1&GT;

select a.id,a.name from T_DEPARTMENT a join T_EMPLOYEE b on a.ID=b.DEPARTMENT_ID group by a.id having count(*) >=20

2 - ;

Select salary, (SUM(salary)* 100 / (Select Count(*) from T_EMPLOYEE)) as percentage_sal
    From T_EMPLOYEE
    Group By salary

3&GT;

select e1.name  from T_DEPARTMENT e1, T_EMPLOYEE e2 where e2.id = e1.chief_id and e1.salary < e2.salary*0.1`

答案 1 :(得分:0)

好的第一部分使用此查询:

select 
t1.name,
count(t1.name) as EmployeesCount,
case when count(t1.name)>20 then 'True' else 'False' end as moreThan20 
from 
T_DEPARTMENT t1,
T_EMPLOYEE t2 
where 
t1.id = t2.department_id 
group by t1.name;

第二个:

select 
name,
salary,
(salary * 100) / (select sum(salary) from hatest2)  as percentage
from 
T_EMPLOYEE
group by salary;

并且最后我想你有一个表名为T_CHIEF,这个选择显示你不是主管的人员:

select 
t2.name  
from 
T_CHIEF t1,
T_EMPLOYEE t2 
where 
t1.id = t2.chief_id and 
t2.salary < (select max(salary) * 0.1 from T_EMPLOYEE where CHIEF_ID not in(select id from T_CHIEF)) ;