(1)消除取值重复行(DISTINTY)[如果没有指定,则缺省ALL]
SELECT DISTINCT Sno FROM SC
查询满足条件的元祖
查询条件 | 谓词 |
比较 | =,>,<,>=,<=,!=,<>,!>,!<;NOT+上述比较运算符 |
确定范围 | BETWEEN AND , NOT BETWEEN AND |
确定集合 | IN , NOT IN |
字符匹配 | LIKE , NOT LIKE |
空值 | IS NULL , IS NOT NULL |
多重条件 | AND ,OR ,NOT |
( 2 )ORDER BY子句
对查询结果按照一个或多个属性列的自序(ASC)或降序排列(DESC),缺省值为升序
SELECT * FROM Student ORDER BY Sdept , Sage DESC;
(3)聚集函数
COUNT( [DISTINCT | ALL] * ) | 统计元祖个数 |
COUNT( [DISTINCT | ALL]<列名> ) | 统计一列中值得个数 |
SUM( [DISTINCT | ALL]<列名> ) | 统计一列值的总数(此列必须是数值型) |
AVG( [DISTINCT | ALL]<列名> ) | 统计一列值的平均数(此列必须是数值型) |
MAX( [DISTINCT | ALL]<列名> ) | 求一列值得最大值 |
MIN( [DISTINCT | ALL]<列名> ) | 求一列值得最小值 |
SELECT COUNT(*)FROM Student;
(4)GROUP BY子句
将查询结果按某一列或者多列的值分组,值相等的为一组,如果按一定条件对这些组进行筛选,可以用HAVING短语
SELECT SnoFROM SCGROUP BY SnoHAVING COUNT( * ) > 3;
(5)链接查询
等值于非等值查询
自身连接
外连接 SELECT Student.Sno FROM Student LEFT JOIN SC ON (Student.Sno = SC.Sno);
符合条件连接
嵌套查询SELECT Sname FROM Student WHERE Sno IN ( SELECT Sno FROM SC WHERE Cno = '2' );
集合查询 SELECT * FROM Student WHERE Sdept = 'CS' UNION SELECT * FROM Student WHERE Sage <= 9;