简单查询

查询数据(关键字:select)

(1)简单查询

select * from 表名

select 列名 from 表名

select 列名 as 别名 from 表名

(2)条件查询 (where  or  and)

select * from 表名 where 条件1

select * from 表名 where 条件1 or 条件2

select * from 表名 where 条件1 and 条件2

(3)范围查询 (between  and)

select * from 表名 where 列名 between 值1 and 值2

(4)离散查询 (in  not in)

select * from 表名 where 列名 in(数据列表)

select * from 表名 where 列名 not in(数据列表)

(5)模糊查询  (like   %任意多个字符    _任意一个字符)

select * from 表名 where 列名 like ‘%_’

(6)排序查询 ( order by    desc降序   asc升序)

select * from 表名 order by 列名 ——默认升序,也可在列名后面加asc

select * from 表名 order by 列名 desc

(7)分组查询  (group by     having)

select * from 表名 group by 列名 (要把非聚合列名都放在子查询中) having  条件 ——having需要跟在group by 后使用

(8)分页查询  (top  n 取前n个值)

select  top n * from 表名

(9)去重查询  (关键字: distinct )

select distinct 列名 from 表名

聚合函数(统计函数)

select count(*) from 表名

select sum(列名) from 表名

select avg(列名) from 表名

select max(列名) from 表名

(10)ROW_NUMBER() 、partition by(按列分组后排序) order by  (排序)

select ROW_NUMBER() over(order by cbatch asc)rown,cwhcode,cinvcode,iQuantity,cbatch from [UFDATA_006_2012]..currentstock where cwhcode=@cwhcode and cinvcode=@cinvcode and iQuantity>0

 1 --最高语文成绩 和最低语文成绩
 2 select MAX(yuscore),MIN(yuscore) from xuesheng
 3 --最高数学成绩和最低数学成绩
 4 select MAX(shuscore) as 数学最高分,MIN(shuscore) as 数学最低分 from xuesheng
 5 --每个班级的平均分
 6 select banji 班级,avg(shuscore) 数学平局分 from xuesheng group by banji
 7 --所有男生的姓名
 8 select name from xuesheng where sex=''
 9 --一班的数学最高分和数学最低分
10 select MAX(shuscore) as 数学最高分,MIN(shuscore) as 数学最低分 from xuesheng where banji='一班'
11 --数学成绩最高的同学的信息
12 select top 1 *from xuesheng order by shuscore desc
13 --女生的人数
14 select COUNT(*) from xuesheng where sex=''
15 --平均分超过81的班级、人数
16 select banji,COUNT(*),avg(shuscore) from xuesheng group by banji having AVG(shuscore)>81
17 --数学成绩大于75的 班级的人数
18 select banji,COUNT(*)from xuesheng where shuscore>75 group by banji order by COUNT(*) desc
19 --一班数学成绩大于75的学生信息
20 select *from xuesheng where shuscore>75 and banji='一班'

 

posted @ 2019-11-30 09:39  tasunny  阅读(894)  评论(0编辑  收藏  举报