今天学了SqlServer模糊查询、聚合函数 和分组查询
2017-05-03 16:47
363 查看
use Student
create table ClassInfo(
id int identity(1,1) primary key,
name varchar(20) not null
)
create table StudentInfo1(
id int identity(1,1) primary key,
name varchar(20) not null,
gender varchar(5) check(gender in('男','女')),
city varchar(10) default('郑州'),
physics int,
mathmatics int,
classId int foreign key references ClassInfo(id)
)
insert into ClassInfo values('一年一班')
insert into ClassInfo values('一年二班')
insert into ClassInfo values('二年一班')
insert into ClassInfo values('二年二班')
alter table StudentInfo1
add age int check(age>6 and age<130)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('张三',16,'男','开封',76,87,1)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('张四',17,'男','开封',66,85,1)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('刘三',19,'男','新乡',97,81,2)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('刘四',19,'女','新乡',86,87,2)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('刘五',16,'女','开封',76,87,2)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('刘实在',16,'男','许昌',76,87,2)
select * from StudentInfo1 where age>=19 and gender='男'
select * from StudentInfo1 where name like '[^刘%]'
select * from StudentInfo1 where name like '%实%'
select * from StudentInfo1 where name like '_实_'
select * from StudentInfo1 where age between 16 and 18
select * from StudentInfo1 where city in ('开封','许昌')
select SUM(physics) as '物理总成绩',SUM(mathmatics) as '数学总成绩',SUM(physics+mathmatics) as '总成绩' from StudentInfo1
select SUM(mathmatics) as '数学总成绩' from StudentInfo1
select SUM(physics+mathmatics) as '总成绩' from StudentInfo1
select MAX(physics) as '物理最高分' ,min(mathmatics) as '数学最低分' ,AVG(physics) as '物理平均分' from StudentInfo1
select COUNT(*) as '物理及格人数(成绩>80分)' from StudentInfo1 where physics >80
select city as '城市',COUNT(*)as '人数' from StudentInfo1 group by city
select city as '城市',MIN(age) as '最小年龄' from StudentInfo1 group by city
order by MIN(age) desc
select COUNT(*) as '人数',gender,classid from StudentInfo1 group by classId, gender
select MIN(age) as '最小年龄',city as '城市' from StudentInfo1 group by city
having COUNT(*)>=2
order by MIN(age) asc
create table ClassInfo(
id int identity(1,1) primary key,
name varchar(20) not null
)
create table StudentInfo1(
id int identity(1,1) primary key,
name varchar(20) not null,
gender varchar(5) check(gender in('男','女')),
city varchar(10) default('郑州'),
physics int,
mathmatics int,
classId int foreign key references ClassInfo(id)
)
insert into ClassInfo values('一年一班')
insert into ClassInfo values('一年二班')
insert into ClassInfo values('二年一班')
insert into ClassInfo values('二年二班')
alter table StudentInfo1
add age int check(age>6 and age<130)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('张三',16,'男','开封',76,87,1)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('张四',17,'男','开封',66,85,1)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('刘三',19,'男','新乡',97,81,2)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('刘四',19,'女','新乡',86,87,2)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('刘五',16,'女','开封',76,87,2)
insert into StudentInfo1 (name,age,gender,city,physics,mathmatics,classId)
values ('刘实在',16,'男','许昌',76,87,2)
select * from StudentInfo1 where age>=19 and gender='男'
select * from StudentInfo1 where name like '[^刘%]'
select * from StudentInfo1 where name like '%实%'
select * from StudentInfo1 where name like '_实_'
select * from StudentInfo1 where age between 16 and 18
select * from StudentInfo1 where city in ('开封','许昌')
select SUM(physics) as '物理总成绩',SUM(mathmatics) as '数学总成绩',SUM(physics+mathmatics) as '总成绩' from StudentInfo1
select SUM(mathmatics) as '数学总成绩' from StudentInfo1
select SUM(physics+mathmatics) as '总成绩' from StudentInfo1
select MAX(physics) as '物理最高分' ,min(mathmatics) as '数学最低分' ,AVG(physics) as '物理平均分' from StudentInfo1
select COUNT(*) as '物理及格人数(成绩>80分)' from StudentInfo1 where physics >80
select city as '城市',COUNT(*)as '人数' from StudentInfo1 group by city
select city as '城市',MIN(age) as '最小年龄' from StudentInfo1 group by city
order by MIN(age) desc
select COUNT(*) as '人数',gender,classid from StudentInfo1 group by classId, gender
select MIN(age) as '最小年龄',city as '城市' from StudentInfo1 group by city
having COUNT(*)>=2
order by MIN(age) asc
相关文章推荐
- 什么是数据库?数据库的作用想学数据库必看 (3)数据库操作 排序, 聚合函数 ,分组查询,分页查询,模糊查询,数据完整性
- 【mysql】 mysql 子查询、联合查询、模糊查询、排序、聚合函数、分组----------语法
- 聚合函数:sum,avg,max,min,count;模糊查询;排序
- SqlServer查询分组后每个分组的第N条数据
- sqlserver 模糊查询通配符问题
- sqlserver模糊查询【转】
- sqlserver中like模糊查询如何区分大小写
- sql sever模糊查询和聚合函数
- C#语言和SQL Server数据库技术_模糊查询和聚合函数
- 模糊查询和聚合函数
- Mysql学习之分组查询配合聚合函数
- 使用聚合函数group by后面必须要带着所有要查询的列,但是我只想根据group by后面的第一个列进行分组,怎么办? [
- 聚合函数和分组查询
- SQL Server之 (二) SQL语句 模糊查询 空值处理 聚合函数
- sqlServer 聚集函数 和 分组查询
- sqlserver中如何实现时间按月,日,小时等时间分组查询
- SQLserver 实现分组统计查询(按月、小时分组)
- 关于SQLServer存储过程动态拼接,模糊查询的处理
- mysql排序查询,多条件排序查询,聚合函数的使用,分组统计查询,
- sqlserver 模糊查询