第二章 数据的查询

--1.查询所有列
select * from Student 
--2.查询一个表中的指定的列
select 姓名, 总学分 from Student 
--3.定义列别名
select 姓名 as xm , 总学分 as zxf from Student
--4.替换查询结果中的数据
select 姓名,
case when 总学分<50 then '不及格'
when 总学分<52 then '合格'
else '优秀' 
end as 等级
from Student
--5.计算列
select 学号, 姓名,出生时间,YEAR(getdate())-YEAR(出生时间) as 年龄 from Student 
--6.消除重复行
select distinct 专业 from Student
--7.前6行的数据
select top 6 * from Student 
--8.对查询结果的排序
select * from Student order by 出生时间 asc
select * from Student order by 出生时间 desc
--聚合函数
select sum(总学分) from Student
select max(总学分) from Student
select min(总学分 )from Student
select avg(总学分) from Student 
select count(*) from Student 
--where 子句
select * from Student where 性别=''
select * from Student where 专业='通信工程'and 总学分>=42
--模糊查询
select * from Student where 姓名 like '王%'
select * from Student where 姓名 like '王_'
--范围比较
select * from Student where 出生时间>='1990-1-1'and 出生时间<='1995-12-31'
select * from Student where 出生时间 between '1990-1-1'and '1995-12-31'
select * from Student where 专业 in('计算机','通信工程')
--空值比较
select * from Student where 备注=''
select * from Student where 备注 is null


 
原文地址:https://www.cnblogs.com/zhang1997/p/7840063.html