emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno所属部门编号) dept部门表(deptno部门编号/d

本文通过创建部门和员工两个表格,演示了基本的SQL操作,包括插入数据、查询、更新等。并提供了多个实战查询案例,如按条件筛选记录、计算统计数据等。

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

create table dept(
deptno int primary key auto_increment not null,
dname varchar(20),
ioc varchar(50)
);
create table emp(
empno varchar(20) primary key not null,
ename varchar(20),
job varchar(20),
mgr varchar(20),
hiredate datetime,
sal double,
comm double,
deptno int,
foreign key(deptno) references dept(deptno)
);
insert into dept(dname,ioc)values('开发','郑州');
insert into dept(dname,ioc)values('游戏','上海');
insert into dept(dname,ioc)values('办公','米国');
insert into emp values('11011','小明','职业打假','11','2021-11-11 12:12:12',2000,300,1);
insert into emp values('11012','小李','游戏主播','11','2021-11-11 12:12:12',4000,3000,2);
insert into emp values('11013','张三','职业律师','12','2021-12-12 12:12:12',5000,30,2);
insert into emp values('11014','李四','职业背锅','12','2021-10-13 12:12:12',4000,4000,2);
insert into emp values('11015','王五','职业聊天','13','2021-11-14 12:12:12',1000,3000,3);
insert into emp values('11016','赵六','职业作假','13','2021-11-20 12:12:12',4000,500,3);
-- 1、在emp表中查询出所有记录的姓名、部门编号、薪水,并且列名要显示为中文。
select ename 姓名,deptno 部门号,sal 薪水 from emp
-- 2、在emp表中查询出薪水大于1500的记录,并且按照薪水的降序排列。
select * from emp where sal>1500 group by sal
-- 3、在emp表中查询出comm字段为空值的记录。
select * from emp where comm is null
-- 4、查询出emp表中含有几个部门的记录。(用DISTINCT去除重复记录)
select DISTINCT count(deptno) from emp GROUP BY deptno 
-- 5、在emp表中查询出部门编号为10或20的记录(要求使用IN关键字)
select * from emp where deptno in (1,2)
-- 6、在emp表中查询出姓名的第二个字母为A的记录。
select * from emp where ename like '_A%'
-- --7、查询出emp表中总共有多少条记录。
select count(*) from emp 
-- --8、查询emp表中出每个部门的部门代码、薪水之和、平均薪水。
select deptno as 部门编号,sum(sal) as 部门薪水之和,avg(sal) as 平均薪资 from emp group by sal
-- 练习二:
-- 1.列出至少有一个员工的所有部门。 
select deptno from emp group by deptno having count(deptno)>1
-- 2.列出薪金比“SMITH”多的所有员工。 
select * from emp where sal>(select sal from emp where ename = '李四')
-- 3.列出所有员工的姓名及其直接上级的姓名。 
select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno
-- 4.列出受雇日期早于其直接上级的所有员工。
select * from emp where hiredate>'2021-11-10 12:12:12'
-- 5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门。 
select * from dept d left outer join emp e on d.deptno = e.deptno
-- 6.列出所有“CLERK”(办事员)的姓名及其部门名称。 
select e.ename,d.dname from emp e,dept d where job = '职业打假' and  e.deptno = d.deptno
-- 7.列出最低薪金大于1500的各种工作。 
select job from emp where sal>1500
-- --8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
select e.ename from emp e,dept d where d.deptno = e.deptno and d.dname = '游戏'
-- --9.列出薪金高于公司平均薪金的所有员工。 
select * from emp where sal>(select avg(sal) from emp)
-- --10.列出与“SCOTT”从事相同工作的所有员工。
select * from emp where job=(select job from emp where ename = '李四')
-- --12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
select * from emp where deptno = 1
-- --13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
select count(deptno),avg(sal) from emp group by deptno
-- --14.列出所有员工的姓名、部门名称和工资。
select e.ename , d.dname, sal+comm from emp e ,dept d where e.deptno = d.deptno
-- --15.列出所有部门的详细信息和部门人数。 
select d.*,count(e.deptno) from emp e,dept d where d.deptno = e.deptno
-- --16.列出各种工作的最低工资。 
select job,min(sal) from emp group by job 
-- --17.列出薪金最低的MANAGER的基本信息。 
select * from emp order by sal asc limit 0,1
-- --18.列出所有员工的年工资,按年薪从低到高排序。
select (sal+comm)*12 as 年工资 from emp order by sal*12 desc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值