1.假设在表Student中查询没有年龄信息的学生
select * from Student where Sage is NULL;
注意:is不能用等号代替、
2.在表Student中,先按专业升序排序,然后同一专业的学生再按年龄降序排序,并输出全部学生信息
select * from Student order by Sdept,Sage desc;
3.limit子句
select* from Student limit 5,10;//检索记录行6-15
select * from Student limit 5;//检索前5个记录行
4.聚集函数
select count(*) as totalcount from table1;
select sum(field) as sumvalue from table1;
select avg(field) as avgvalue from table1;
selet max(field) as maxvalue from table1;//min
5.group by根据一个或多个属性的值来对元组进行分组,值相等的为一组。另外,having子句只能用在group by子句的SQL语句中,用来选取符合条件的特定分组;
查询学生表中具有相同年龄的每个组的人数、
select Sage,count(*) from Student group by Sage;
查询组中人数大于1的年龄
select Sage,count(*) from Student group by Sage having count(*)>1;
6连接查询
自然连接(内连接)
select Student.*,SC.* from Student,SC where Student.Sno=SC.Sno;
若学生表中某些学生没有选课,则在SC表中没有对应的元组,造成最终结果中舍弃掉这些学生信息
外连接:左连接,右连接
select Student.*,SC.* from Student left join SC on(Student.Sno=SC.Sno);
以上是左外连接,左外连接会列出左表的所有元组