select distinct staffNo, staff.fName, staff.lName, staff.salary
from privateowner join propertyforrent using (ownerNo) join staff using (staffNo)
where privateowner.telNo is not null
order by staff.salary desc;
查询所有担任助理职务员工的员工号和收入,查询结果按收入降序排列。
select staffNo, salary from staff where position = "assistant" order by salary desc;
统计员工总人数。
select count(*) as total_staff from staff;
统计担任经理职务员工的人数。
select count(*) as total_manager from staff where position = "manager";
计算所有担任助理职务员工的总收入之和。
select sum(salary) as total_salary_assistant from staff where position = "assistant";
统计每个职务的员工人数,列出职务名称和人数。
select position, count(*) as total_staff from staff group by position;
查询每种职务的员工人数和平均收入。
select position, count(*) as total_staff, avg(salary) as avg_salary from staff group by position;
查询与 Ann beech 担任同一个职务的职工的职工号。
select staffNo from staff where position = (
select position from staff where fname = "Ann" and lname = "beech"
);
将职工号为“SA9”的职工的收入改为 15000。
update staff set salary = 15000 where staffNo = "SA9";
删除所有收入低于 10000 的员工的记录。
SET FOREIGN_KEY_CHECKS = 0;
delete from staff where salary < 10000;
with branch_rent(branchNo, daily_rent) as
(SELECT branchNo, sum(rent)/30 FROM propertyforrent group by branchNo)
select branch.branchNo,
(case
when daily_rent is not null then daily_rent
else 0
end) as daily_rent
from branch natural left outer join branch_rent
order by branchNo;
给出每个分支机构持有的每一类房产的总数。
select branchNo, type, count(*) as total_property
from propertyforrent
group by branchNo, type;
给出看过两类(含)房产以上且由两个(含)分公司的员工带看过的客 户的名字和电话号码。
select fName, lName, telNo from client
where 2 <= (
select count(distinct type) from viewing natural join propertyforrent
where client.clientNo = viewing.clientNo
);
给出为提供多处房产的业主提供服务的员工的名字、薪水和所在分支机 构
with ownerwithmultiproperty(ownerNo)
as (select ownerNo from propertyforrent
group by ownerNo
having count(propertyNo) > 1),
staffsatisfied(staffNo)
as (select staffNo from propertyforrent
where ownerNo in (select ownerNo from ownerwithmultiproperty))
select fName, lName, salary, branchNo from staff
where staffNo in (select staffNo from staffsatisfied);