数据库的查询Select语句
查询操作
查询学生表所有学生的信息
数据库查询语法
select [all | distinct] [Top n ] 表达式 […]
1.列明表投影某些项:
语法: 列名[,…]
例题 查询学生表中前3行信息
select top 3 *
from student
2.Top关键字限制返回行数
语法: Top n [PER CENT]
如果未指定关键字PER CENT ,则返回查询结果前n行数据,如果指定了关键字PER CENT, n 就是查询返回结果集行的百分比
例题2. 查询学生表中学号为2011216001号的学生的姓名和专业
select sname,specialtyfrom student
where sid='2011216001'
3.all 查询全部 distinct 取消重复
例题3.消除重复的专业名称
select distinct Specialty
from Student
4.通配符 * 投影所有列
select * from student
5.自定义列标题
语法1:‘列的新名称’ = 表达式
语法2:表达式 AS ‘列的新名称’
例题.查询课程表中学分大于4分的课程,并按学分降序排列
select cid ,credit,cname
from course
where credit>4
order by credit desc
6.查询为空的信息
例题. 查询出生日期非空的学生信息
select * from student
where birthday is not null
7.聚合函数group by
例题.分组统计每一个专业的男女同学人数
select specialty as '专业名称', sex as '性别',COUNT(* ) as '人数'
from student
group by sex,specialty
8。% 和 _的不同
例题5.查询姓李的同学信息
select * from student
where sname like '李%'
%匹配0或多个字符
例题6. 查询姓李的名字是两个字的女同学信息
select * from student
where sname like '李__' and sex ='女'
_匹配任意1个字符(必须有一个)