select 语句的一般语法
1.select
2.from 表用逗号分开
3.where行条件语句在条件中不能出现集合函数(分组函数)
4.group by分组子句
5.order by 排序子句
常用的查询语句条件
比较 = ,> ,< ,>= ,<= ,! ,<>
确定范围
between and not between and
是否为空值
is NUll is not NULL
字符匹配
like NOT like
逻辑
and or not
查询全体学生学号和姓名
select sno,sname from student
查询所有考试不及格的学生的学号
SELECT sno from sc where grade<60
查询不是信息系 计算机系的学生性别年龄系别
select ssex,sage,sdept from student where sdept not in ('is','cs')
查询年龄18-20岁的学生学号姓名系别年龄
select sno,sname,sdept,sage from student where sage between 18 and 20
查询了选修四号课程的学生的学号和成绩结果按成绩降序排列
select sno,grade from sc where cno='004'order by grade desc| asc升序
查询姓刘的学生的情况
select * from student where like '刘*'
* 匹配0个或多个字符
?匹配单个字符
#匹配单个数字
下划线 匹配单个字符
%匹配0个或更多字符的任意字符
常用的分组函数
count
avg
sum
max
min
查询每门课学生的平均成绩
select sno ,SUM(grade) from sc group by sno
查询每个系的学生的人数
select sdept ,count(sno) from student group by sdept
having 短语 必须依赖于group by 子句存在group by 子句时候才会出现having短信
select sno,sum(grade) from sc group by sno having sum(grade>20)
转载于:https://blog.51cto.com/xiaochaozi/1213682