数据检索
Top Distinct
Select top 5 sName,sAgefrom student
Select top 30 percentsName,sAgefrom student 非四舍五入,返回最大整数2.1返回3
聚合函数
selectcount(*) from student;
selectmax(english) fromscore;
selectmin(english) fromscore;
selectavg(english) fromscore
selectsum(english) fromscore;
selectcount(*) from student
wheresSex=‘女’;
带条件的查询
select studentIdfrom score whereenglish< 60
select sName,sAge,sSexfrom student wheresAge>=20 andsAge<=30 andsSex='男'
select sName,sAge,sSexfrom student wheresAgebetween 20 and 30 andsSex='男'
数据分组
select 语句中可以使用group by 子句将行划分成较小的组,然后,使用聚组函数返回每一个组的汇总信息
可以使用having子句限制返回的结果集。group by 子句可以将查询结果分组,并返回行的汇总信息. 按照group by 子句中指定的表达式的值分组查询结果。
--group by
--每个班有多少人
select * from student
select sClassId,count(*)from student group bysClassId
use MySchool
--简单查询
select * from student
select sId, sName, sAge, sNo, sBirthday, sClassId, sSex, sIntime from student
select sName from student
--给列改名
select sName as '姓名',sAge as '年龄' from student
select sName '姓名',sAge '年龄' from student
select '姓名'=sName,'年龄'=sAge from student
select sName as '姓名',sAge as '年龄',sSex as '性别'
from student where sSEx='女'
select sName as '姓名',sAge as '年龄',sSex as '性别'
from student where sAge > 20
select 2*3
select getdate()
--Top Distinct
select top 2 sName,sAge,sSex from student
order by sAge
--百分之10个学生 如果有小数位数,直接进1
select top 10 percent sName,sAge,sSex
from student
select distinct * from student
select distinct sName from student
select distinct sName,sSex,sAge from student
--聚合函数max min avg sum count
select max(english) from score
select min(english) from score
--null值不参加avg的运算
select avg(english) from score
select sum(english)/count(*) from score
select sum(english) from score
select count(sId) from student
select count(*) from student where sSex='女'
select max(english) as '最大值',min(english) as '最小值',avg(english) as '平均值'
from score
select max(sBirthday),min(sBirthday) from student
select * from student
select * from score
--带条件查询
select studentId from score where english >= 60
select sName,sAge,sSex from student
where sAge >=20 and sAge<=30 and sSex='男'
--between....and ....
select sName,sAge,sSex from student
where sAge between 20 and 30 and sSex='男'
select * from student
where sBirthday between '1988-1-1' and '1989-12-31'
select * from student
select * from score where english between 80 and 90
--in (1,2,3)
select * from student
where sClassId = 1 or sClassId=4 or sClassId = 3
select * from student
where sClassId in (1,2,4)
--模糊查询
select left('123',1)
select * from student where left(sName,1)='张'
select * from student where sName like '张%'
select * from student where sName like '%亮%'
select * from student where sName like '张__'
select * from student where sName like '张[飞亮]%'
alter table student
add constraint CK_Student_sPhone check (sPhone like '[0-9][0-9][0-9][0-9][0-9][0-9]')
insert into Student (sClassId,sName,sAge,sSex,sNo,sBirthday,sPhone) values (4,'诸葛亮',20,'男',22315678801234561,'1989-8-8','123456')
--null
select null + 123
select * from student where sPhone is null
select * from score where english is null
select * from score where english is not null
--order by
select top 2 * from student
order by sAge desc
select * from student
order by sName desc
select * from score
order by english asc,math desc
select * from student
where sSex='男'
order by sAge desc
--group by sClassId
--分组就是把sClassId相同值的那些行,合并为一行
--当有“每个”的含义时候使用分组
--每个班有多少个学生
--第一个问题 使用group by后,select之后的列必须出现在group by子句中或者聚合函数中
select count(*),sClassId from student
group by sClassId
select count(sId) as '个数',sClassId from student
where sSex='男'
group by sClassId
select count(sId),sClassId,avg(sAge)
from student
group by sClassId
--聚合函数不能出现在where子句中
--错误
select count(sId),sClassId,avg(sAge)
from student
where avg(sAge) > 20
group by sClassId
--求平均年龄超过20岁的那些班
--having 对分组过后的数据进行筛选(和where的作用不用)
select sClassId,avg(sAge)
from student
group by sClassId
having avg(sAge) > 20
--求人数超过5个的那些班
select sClassId,count(sId)
from student
group by sClassId
having count(sId) > 5
--分组练习
--求男生和女生分别有多少人
select count(sId),sSex from student
group by sSex
--求每个班有多少男生
select count(sId),sClassId from student
where sSex='男'
group by sClassid
--每个班中的男同学的平均年龄
select count(sId),sClassId,avg(sAge) from student
where sSex='男'
group by sClassid
--求平均年龄小于22的那些班
select count(sId),sClassId,avg(sAge) from student
group by sClassid
having avg(sAge) < 22
--union
--列的个数一致,类型一致
--排序 去除重复数据
select tName,tSex,-1 from teacher union
select sName,sSex,sClassId from student
--union all
--直接连接两个结果集
select tName,tSex from teacher union all
select sName,sSex from student
--要求在一个表格中查询出学生的英语最高成绩、最低成绩、平均成绩
select '最高成绩',max(english) from score union all
select '最低成绩',min(english) from score union all
select '平均成绩',avg(english) from score
--查询每位老师的信息,包括姓名、工资,并且在最后加上平均工资和最高工资
select tName,tSalary from teacher union all
select '平均工资',avg(tSalary) from teacher union all
select '最高工资',max(tSalary) from teacher
--一次插入多条数据
insert into Score(studentId,english,math)
select 1,80,100 union
select 2,60,80 union
select 3,50,59 union
select 4,66,89 union
select 5,59,100
select * from score
--把现有表中的数据插入到新表中(newStudent表不能存在)
--去除表中重复数据
select * into newStudent from student
select * from newStudent
--把现有表的数据复制到一个已存在的表
truncate table newStudent
insert into newStudent select sName, sAge, sNo, sBirthday, sClassId, sSex, sIntime, sPhone from student
--简单查询
select * from student
select sId, sName, sAge, sNo, sBirthday, sClassId, sSex, sIntime from student
select sName from student
--给列改名
select sName as '姓名',sAge as '年龄' from student
select sName '姓名',sAge '年龄' from student
select '姓名'=sName,'年龄'=sAge from student
select sName as '姓名',sAge as '年龄',sSex as '性别'
from student where sSEx='女'
select sName as '姓名',sAge as '年龄',sSex as '性别'
from student where sAge > 20
select 2*3
select getdate()
--Top Distinct
select top 2 sName,sAge,sSex from student
order by sAge
--百分之10个学生 如果有小数位数,直接进1
select top 10 percent sName,sAge,sSex
from student
select distinct * from student
select distinct sName from student
select distinct sName,sSex,sAge from student
--聚合函数max min avg sum count
select max(english) from score
select min(english) from score
--null值不参加avg的运算
select avg(english) from score
select sum(english)/count(*) from score
select sum(english) from score
select count(sId) from student
select count(*) from student where sSex='女'
select max(english) as '最大值',min(english) as '最小值',avg(english) as '平均值'
from score
select max(sBirthday),min(sBirthday) from student
select * from student
select * from score
--带条件查询
select studentId from score where english >= 60
select sName,sAge,sSex from student
where sAge >=20 and sAge<=30 and sSex='男'
--between....and ....
select sName,sAge,sSex from student
where sAge between 20 and 30 and sSex='男'
select * from student
where sBirthday between '1988-1-1' and '1989-12-31'
select * from student
select * from score where english between 80 and 90
--in (1,2,3)
select * from student
where sClassId = 1 or sClassId=4 or sClassId = 3
select * from student
where sClassId in (1,2,4)
--模糊查询
select left('123',1)
select * from student where left(sName,1)='张'
select * from student where sName like '张%'
select * from student where sName like '%亮%'
select * from student where sName like '张__'
select * from student where sName like '张[飞亮]%'
alter table student
add constraint CK_Student_sPhone check (sPhone like '[0-9][0-9][0-9][0-9][0-9][0-9]')
insert into Student (sClassId,sName,sAge,sSex,sNo,sBirthday,sPhone) values (4,'诸葛亮',20,'男',22315678801234561,'1989-8-8','123456')
--null
select null + 123
select * from student where sPhone is null
select * from score where english is null
select * from score where english is not null
--order by
select top 2 * from student
order by sAge desc
select * from student
order by sName desc
select * from score
order by english asc,math desc
select * from student
where sSex='男'
order by sAge desc
--group by sClassId
--分组就是把sClassId相同值的那些行,合并为一行
--当有“每个”的含义时候使用分组
--每个班有多少个学生
--第一个问题 使用group by后,select之后的列必须出现在group by子句中或者聚合函数中
select count(*),sClassId from student
group by sClassId
select count(sId) as '个数',sClassId from student
where sSex='男'
group by sClassId
select count(sId),sClassId,avg(sAge)
from student
group by sClassId
--聚合函数不能出现在where子句中
--错误
select count(sId),sClassId,avg(sAge)
from student
where avg(sAge) > 20
group by sClassId
--求平均年龄超过20岁的那些班
--having 对分组过后的数据进行筛选(和where的作用不用)
select sClassId,avg(sAge) as'平均年龄'
from student
group by sClassId
having avg(sAge) > 20
--求人数超过5个的那些班
select sClassId,count(sId)
from student
group by sClassId
having count(sId) > 5
--分组练习
--求男生和女生分别有多少人
select count(sId),sSex from student
group by sSex
--求每个班有多少男生
select count(sId),sClassId from student
where sSex='男'
group by sClassid
--每个班中的男同学的平均年龄
select count(sId),sClassId,avg(sAge) from student
where sSex='男'
group by sClassid
--求平均年龄小于22的那些班
select count(sId),sClassId,avg(sAge) from student
group by sClassid
having avg(sAge) < 22
--union
--列的个数一致,类型一致
--排序 去除重复数据
select tName,tSex,-1 from teacher union
select sName,sSex,sClassId from student
--union all
--直接连接两个结果集
select tName,tSex from teacher union all
select sName,sSex from student
--要求在一个表格中查询出学生的英语最高成绩、最低成绩、平均成绩
select '最高成绩',max(english) from score union all
select '最低成绩',min(english) from score union all
select '平均成绩',avg(english) from score
--查询每位老师的信息,包括姓名、工资,并且在最后加上平均工资和最高工资
select tName,tSalary from teacher union all
select '平均工资',avg(tSalary) from teacher union all
select '最高工资',max(tSalary) from teacher
--一次插入多条数据
insert into Score(studentId,english,math)
select 1,80,100 union
select 2,60,80 union
select 3,50,59 union
select 4,66,89 union
select 5,59,100
select * from score
--把现有表中的数据插入到新表中(newStudent表不能存在)
--去除表中重复数据
select * into newStudent from student
select * from newStudent
--把现有表的数据复制到一个已存在的表
truncate table newStudent
insert into newStudent select sName, sAge, sNo, sBirthday, sClassId, sSex, sIntime, sPhone from student
truncate table score
truncate table student
truncate table class
create database Company
go
use company
go
create table department
(
dId int identity(1,1) primary key,
dName nvarchar(50)
)
create table employee
(
eId int identity(1,1) primary key,
eName nvarchar(50) not null,
eSex nchar(1),
eAge int,
eSalary money,
eDepId int not null
)
alter table employee
add constraint FK_employee foreign key (eDepId) references department(dId)
--级联删除
--on delete cascade
insert into department values('财务部')
insert into department values('开发部')
insert into department values('后勤部')
insert into employee(eName,eSex,eAge,eSalary,eDepId)
values ('淡定哥','男',18,3000,2)
insert into employee(eName,eSex,eAge,eSalary,eDepId)
values ('小月月','女',20,4000,1)
insert into employee(eName,eSex,eAge,eSalary,eDepId)
values ('马户','男',18,3500,1)
insert into employee(eName,eSex,eAge,eSalary,eDepId)
values ('凤姐','女',21,2500,2)
insert into employee(eName,eSex,eAge,eSalary,eDepId)
values ('凤,姐','女',21,2500,2)
--7、查询每个部门的平均工资
select avg(eSalary),eDepId from employee
group by eDepId
--8、查询每个部门男员工的平均工资
select avg(eSalary),eDepId from employee
where eSex = '男'
group by eDepId
--9、查询平均工资超过2000的那些部门
select avg(eSalary),eDepId from employee
group by eDepId
having avg(eSalary) > 2000