SQL-练习

本文提供了多个SQL查询示例,涉及根据工资金额查询员工工号和姓名,获取特定员工的出勤率,查找出勤率为0的员工,以及找出出勤率为10且工资低于2500的员工详细信息。这些查询使用了INNER JOIN、EXISTS子句和GROUP BY聚合函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

attend表,字段如下
num,no,attendance
工号、工资编号、出勤率

employee表,字段如下
num,name,sex,age,departmentno
工号、姓名、性别、年龄、部门

wage表,字段如下
no,amount
编号、工资金额

题目:
–1.查询工资金额为8000的职工工号和姓名,降序排列
–方式一
select employee.num, employee.name from employee, attend
where attend.no in (select DISTINCT no from wage where amount = 8000)
and attend.num = employee.num ORDER BY employee.num desc
–方式二
select num, name from employee where EXISTS (
select num from attend WHERE EXISTS ( select DISTINCT no from wage where amount = 8000 and attend.no = wage.no)
and employee.num = attend.num)
ORDER BY employee.num desc

–2.查询张三的出勤率
–方式一
select attendance from attend where exists(
select num from employee where name=”张三”
and attend.num = employee.num)
–方式二
select attendance from attend where num in(
select num from employee where name=”张三”)
–方式三
select employee.name,attend.attendance from attend,employee
where employee.name=”张三” and attend.num = employee.num

–3.查询3次出勤率为0的职工姓名和工号
–方式一
select num, name from employee
where EXISTS (
select num from attend where attendance = 0
GROUP BY num HAVING count(num) = 3 and employee.num = attend.num)
–方式二
select num, name from employee where num in (select num from attend where attendance = 0 GROUP BY num HAVING count(num) =3)

–4.查询出勤率为10并且工资金额小于2500的职工信息
–方式一
select employee.num,
employee.name,
employee.sex,
employee.age,
employee.departmentno
from (select no from wage where amount < 2500) as wag,
(select no, num from attend where attendance = 10) as att,
employee
where wag.no = att.no
and att.num = employee.num
–方式二
select employee.num,
employee.name,
employee.sex,
employee.age,
employee.departmentno
from employee
where num in (select num
from attend
where no in (select no from wage where amount < 2500)
and attendance = 10)
–方式三
select employee.num,
employee.name,
employee.sex,
employee.age,
employee.departmentno
from employee
where EXISTS (select num
from attend
where EXISTS (select no
from wage
where amount < 2500
and wage.no = attend.no)
and attend.attendance = 10
and employee.num = attend.num)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值