查询语句基本格式
select <目标序列名称>
from <数据源>
[where <检索条件>]
[group by <分组依据>]
[having <组提取条件>]
[order by <排序依据列>]
#更改日期格式
SQL> alter session set nls_date_format ='yyyy-mm-dd hh24:mi:ss';
# where and ; to_date
1.查询1985年前出生的女生信息
select * from TB_Student
where sex='F' and
birthday<to_date('1985-01-01','rrrr-mm-dd');
注:to_date('1985','rrrr') 表示1986前(包括了1985)
2.成绩表中总评成绩高于80的课程超过两门的学生学号,班级号,课程数,按班级号,学号升序。
select stuid,classid,count(*)
from TB_grade
where TotalScore>80
group by Stuid,classid
having count(*)>2
order by classid,stuid;
3普通连接
计算机系所有女生学号,姓名,性别,生日,按生日降序
select stuid,stuname,sex,birthday
from TB_student join TB_dept on
TB_student.deptid=TB_dept.deptid
where deptname='计算机系' and sex='F'
order by birthday desc ;
3笛卡儿积
计算机系所有女生学号,姓名,性别,生日,按生日降序
select stuid,stuname,sex,birthday
from TB_student , TB_dept
where TB_student.deptid=TB_dept.deptid
and sex='F' and deptname='计算机系'
order by birthday desc;
4.自然连接
计算机系所有女生学号,姓名,性别,生日,按生日降序
select stuid,stuname,sex,birthday
from TB_student natural join TB_dept
where deptname='计算机系' and sex='F'
order by birthday desc ;
注:两表之间的公共属性为连接条件的,选择自然连接;有公共属性但不全是连接条件的,选择普通连接;其他选择普通连接或笛卡儿积.
例:
1.以系为组统计人数
SQL> select TB_dept.deptname ,count(*) as 学生人数
from TB_student join TB_dept on TB_student.deptid=TB_dept.deptid
group by deptname ;
DEPTNAME 学生人数
-------------------- ----------
计算机系 30
机电工程系 10
电子工程系 10
2.统计学生的选课门数
SQL> select stuid,count(*) as 选课门数,AVG(totalscore)
from TB_grade
group by stuid; # select后的字段要么属于group by 要么被聚合函数包围
STUID 选课门数 AVG(TOTALSCORE)
-------- ---------- ---------------
04080104 3 69.354
04080202 2 73.25
04080206 1 77.7
04080101 4 74.3
04080109 3 80.1333333
04080210 1 51.6
04080110 3 78.0666667
04080207 1 82.3
04080107 3 62.9666667
04020104 2 81.3
04080102 3 76
STUID 选课门数 AVG(TOTALSCORE)
-------- ---------- ---------------
04080106 4 80.925
04080108 3 89.1666667
04080205 2 69.05
04020101 1 87.6
04080208 1 83.2
04080201 3 85
04080209 1 75.8
04080103 4 71.15
04080105 3 66.9666667
04080203 2 70.9
04080204 1 65.1
已选择22行。
3 查询成绩表中所有平均成绩大于60的课程号和平均成绩。
SQL> select courseid , AVG(totalscore) from TB_grade
group by courseid
having AVG(totalscore)>=60;
注:此处必须用group by聚合,不然平均成绩是以什么为单位呢
4.列出每门课程的平均,最高,最低分数
SQL> select coursename ,AVG(totalscore),MAX(totalScore),MIN(totalscore)
from TB_grade join TB_course on TB_grade.courseid=TB_course.courseid
group by coursename ;
5.查询有二门以上(含二门)低于70分的课程的学生姓名及其平均成绩。
SQL> select stuname , AVG(totalscore)
from TB_student join TB_grade on TB_student.stuid=TB_grade.stuid
where stuname in ( select stuname
from TB_student join TB_grade on TB_student.stuid=TB_grade.stuid
where totalscore<=70
group by stuname
having count(*)>=2)
group by stuname;
6.根据课程班和任课教师对学生平均分进行分类汇总,并显示平均成绩大于75分的信息,要求显示标题为:课程班号、教师姓名、平均分。
SQL> select TB_courseclass.courseclassid,teachername,AVG(totalscore)
from TB_grade left outer join TB_courseclass on TB_grade.courseclassid=TB_courseclass.courseclassid left outer join TB_teacher on
TB_courseclass.teacherid=TB_teacher.teacherid
group by TB_courseclass.courseclassid, teachername, TB_grade.stuid
having AVG(totalscore)>=75 ;
相关文章: