use student
create table bumen
(
bcode int primary key,
bname varchar(50),
bzhi varchar(50)
)
go
create table ren
(
code int identity(1,1),
name varchar(50),
age int,
sex char(10),
cid varchar(50),
bumen int
)
go
insert into bumen values(1001,‘人事部‘,‘负责人员面试考核‘)
insert into bumen values(1002,‘市场部‘,‘负责开拓市场‘)
insert into bumen values(1003,‘销售部‘,‘负责产品销售‘)
insert into bumen values(1004,‘生产部‘,‘负责产品生产‘)
insert into bumen values(1005,‘质检部‘,‘负责产品质量检验‘)
go
insert into ren values(‘张三‘,24,‘男‘,‘370303199903044440‘,1002)
insert into ren values(‘李四‘,25,‘女‘,‘370303199905044440‘,1001)
insert into ren values(‘王五‘,26,‘男‘,‘370303199907044440‘,1002)
insert into ren values(‘赵六‘,27,‘女‘,‘370303199908044440‘,1001)
insert into ren values(‘冯七‘,28,‘男‘,‘370303199909044440‘,1003)
insert into ren values(‘钱大‘,29,‘女‘,‘370303199901044440‘,1001)
insert into ren values(‘赵二‘,22,‘男‘,‘370303199911044440‘,1003)
insert into ren values(‘陈赫‘,31,‘男‘,‘370303199912044440‘,1001)
insert into ren values(‘王祖蓝‘,32,‘女‘,‘370303199904044440‘,1004)
insert into ren values(‘宝贝儿‘,33,‘男‘,‘370303199903034440‘,1004)
insert into ren values(‘李晨‘,34,‘女‘,‘370303199903224440‘,1004)
insert into ren values(‘范爷‘,35,‘男‘,‘370303199903234440‘,1005)
insert into ren values(‘任泉‘,36,‘男‘,‘370303199903254440‘,1002)
insert into ren values(‘李冰冰‘,37,‘男‘,‘370303199903264440‘,1003)
insert into ren values(‘冯小刚‘,38,‘女‘,‘370303199903274440‘,1005)
insert into ren values(‘郭德纲‘,39,‘男‘,‘370303199903284440‘,1004)
insert into ren values(‘赵本山‘,40,‘女‘,‘370303199903294440‘,1004)
1、子查询
--查看销售部有哪些人
select*from ren where bumen=(select bcode from bumen where bname=‘销售部‘)
--查看张三所属的部门职责
select bzhi from bumen where bcode=(select bumen from ren where name=‘张三‘)
--查询部门人数最多的部门里面年龄最大的人员的所有信息
select top 1*from ren where bumen=(select top 1 bumen from ren group by bumen order by COUNT(*)desc) order by age
--按照年龄排序,从小到大,不要前五个人的
select *from ren where code not in (select top 5 code from ren order by age )
--找男职员里面年纪最大的全部信息
select *from ren where age=(select top 1 age from ren where sex=‘男‘ order by age desc)
select *from ren where age=(select MAX(age) from ren where sex=‘男‘ )
--将人员表内部门的编码转换成部门名称查询出来
select ren.code,name,age,sex,cid,bumen.bname from ren,bumen where ren.bumen=bumen.bcode
--查询销售部里的年龄大于30岁的人的所有信息
select *from ren where bumen=(select bcode from bumen where bname=‘销售部‘)and age>30
--查看所有人员的生日
select name,SUBSTRING(cid,7,4)+‘年‘+SUBSTRING(cid,10,2)+‘月‘+SUBSTRING(cid,12,2)+‘日‘from ren
--查看编号为5的人的生日
select name ,SUBSTRING(cid,7,4)+‘年‘+SUBSTRING(cid,10,2)+‘月‘+SUBSTRING(cid,12,2)+‘日‘from ren where code=5
--查年龄是25,26,27所有人的数据
select*from ren where age in(25,26,27)
--查看有没有跟2,3,4一样大的人员
select *from ren where age in(select age from ren where code in(2,3,4))
2、分页查询
--前五条
select top 5*from ren
--第6-10条
select top 5*from ren where code not in (select top 5 * from ren )
--想在一开始就想知道能分成几页(五条一页)
select CEILING(COUNT(*)/5.0)from ren
3、连接查询
--join on 连接查询
select ren.name,sex,age,cid,bumen.bname from ren join bumen on ren.bumen=bumen.bcode
--在上面的基础上,将每个部门的部门职责放在后面显示
select ren.name,sex,age,cid,bumen.bname,bzhi from ren join bumen on ren.bumen=bumen.bcode
--在表中插入两条不完整的数据
insert into bumen values(6,‘科研‘,null)
insert into ren values(‘奥巴马‘,67,null,null,null)
--全部查询 full
--信息不完整时还想查到所有数据,可以在join前面加上full
select ren.name,sex,age,cid,bumen.bname,bzhi from ren full join bumen on ren.bumen=bumen.bcode
--左查询 left
--只管第一个表的所有数据全部显示,不管第二个表的不完整数据
select ren.name,sex,age,cid,bumen.bname,bzhi from ren left join bumen on ren.bumen=bumen.bcode
--右查询 right
--只管第二个表的所有数据全部显示,不管第一个表的不完整数据
select ren.name,sex,age,cid,bumen.bname,bzhi from ren right join bumen on ren.bumen=bumen.bcode
--union 上下拼接
--注意:所拼接的列的数据类型要一致
--查询code值为1 和code值为2 的两个人的所有信息
select*from ren where code =2
union
select*from ren where code=1
--人员表的code列和name列拼接上部门表的bcode列和bname列
select code ,name from ren
union
select bcode,bname from bumen
--给宣传部加上职能的信息
update bumen set bzhi=‘宣传企业文化‘ from bumen where bname=‘科研‘
--人员表的code列和name列sex拼接上部门表的bcode列和bname列bzhi列
select code ,name ,sex from ren
union
select bcode,bname ,bzhi from bumen
--年龄大于37和年龄小于27的员工信息
select*from ren where age>37or age<27