一般格式:

select all/distinct  目标列表达式1,目标列表达式2...

from 表名or视图名

where 条件表达式

group by 列名1 having<条件表达式>  //将结果按“列名1”的值进行分组,列值相等的为一个组,如果带有having短语,                                                                  则满足制定条件的组才予以输出

order by  列名2  ASC/DESC   // 按“列名2”的值升序或降序排序

单表查询

一、选择表中的若干列
1.查询全部列
 
 
select * from student
 
 
2.查询经过计算的列
 
 
select 2012-age from student  // 2012-age不是列名,而是一个表达式,计算学生的出生年份
 
 

 
 
select  'year of birth' birth,  ISLOWER(sdept)  department  from student   //birth, department是列表达式的别名,                                                                                                                     且department列用小写字母显示 
 
 
二、选择表中的若干元组
1.取消取值重复的行
select distinct sno from sc //去掉表中重复的行必须制定短语distinct,如果没有指定,缺省值为all
2.查询满足条件的元组
 
 
1)比较大小
 
 

查询计算机系全体学生的名单
 
  
selec sname from student where sdept='cs' //有字符必须加单引号,数字可以不加  
 
  

 
  
查询年龄20岁以下的学生姓名及其年龄
 
  
select sname age 
 
  
from student 
 
  
where age<20
 
  
or
 
  
select sname age 
 
  
from student 
 
  
where not age>=20
 
  

 
  
查询考试不及格学生的学号
 
  
select distinct sno
 
  
from course
 
  
where grade<60   //这里使用distinct短语,当一个学生多门课不及格,他的学号也只显示一次
 
  

2)确定范围
查找年龄在20-23岁(包括20和23岁)之间的学生的姓名,系别
select sname,sdept  from student where age between 20 and 23
查找年龄不在20-23岁(包括20和23岁)之间的学生的姓名,系别
select sname,sdept  from student where age not between 20 and 23
3)确定集合
查找信息系(is),数学系(math)学生的姓名和性别
select sname,sex from student where sdept in('is','math')//谓词in 用来查找属性值属于指定集                                                               合的元组
与in相对的 not in,可用于查找属性值不属于指定集合的元组。
4)字符匹配
谓词like可以用来进行字符串的匹配,一般语法格式如下:
like '匹配串'
匹配串既可以是一个完整的字符串,也可以含有通配符%和_
%(百分号)代表任意长度(可以为0)的字符串
_(下横线)代表任意单个字符
查询学号为001的学生的信息
select * from student where sno='001'  //匹配串中不含通配符时,可以用=代替like,用!=或>,<代替not like
查找所有姓刘的学生的信息
select * from student where sname like '刘%'
查询姓张且全名为3个汉字的学生的姓名
select sname from student where sname like '张_ _ _ _' //一个汉字占两个字符的位置,所以匹配串后面                                                          跟4个下横线
5)空值查询
查询没有成绩的学生的学号和课程号
select sno,cno from sc where grade is null  //注意,这里的is不能用等号=代替
查询所有有成绩的学生的学号和课程号
select sno,cno from sc where grade is not null
6)多重条件查询
and 和 or可以连结多个查询条件,and的优先级高于or,用户可以用括号改变优先级
查询计算机系年龄在20岁以下的学生的姓名
select sname from student where sdept='cs' and age<20
三、对查询结果排序
order by子句对查询结果按照一个或多个属性列的升序(ASC)或降序(DESC)排列,缺省值为升序
查询选修了3号课程的学生的学号及其成绩,结果按分数降序排列
select sno,grade from sc where cno='3' order by grade desc
对于空值,升序排列,含空值的元组将最后显示;降序排列,含空值的元组将最先显示
四、使用集函数
count distinct/all *       计算元组个数
 
  
count distinct/all 列名    统计一列中值的个数
 
  
sum   distinct/all 列名    计算一列值的总和,此列必须是数值型
 
  
avg   distinct/all 列名    计算一列值的平均值,此列必须是数值型
 
  
max   distinct/all 列名    计算一列值的最大值
 
  
min   distinct/all 列名    计算一列值的最小值
如果指定distinct,计算时取消重复值,默认为all,表示不取消重复值
查询学生总人数
select count * from student
查询选修了课程的学生人数
select count(distinct sno) from sc
计算1号课程的学生平均成绩
select avg(grade) from sc where cno='1'
查询选修1号课程的学生最高分数
select max(grade) from sc where cno='1'
五、对查询结果分组
group by 子句对查询结果按某一列或多列值分组,值相等的为一组
 
  

 
  
求各个课程号及相应的选课人数
 
  
select cno,count(sno)
 
  
from sc             //该语句对查询结果按cno分组,具有相同cno值的为一组,然后对每一组作用集函
 
  
group by cno           数count计算,求得该组的学生人数
 
  

 
  
查询选修了3门以上课程的学生学号
 
  
select sno
 
  
from sc 
 
  
group by sno          //这里先用group by子句按sno进行分组,再用集函数count对每一组计数。
 
  
having count(*)>3   //having短语指定选择组的条件,只有满足条件的组才会被选出来
 
  

 
  
where子句和having短语的区别在于作用对象的不同。where子句作用于基本表或视图,从中选择满足条件的元组。having短语作用于组,从中选择满足条件的组。