转自:http://yuncode.net/code/c_58df7a8ca687e58
1、查询“1”课程比“2”课程成绩高的所有学生的学号; |
( SELECT score, student_id FROM student_score WHERE course_id = 1 ) t1, |
( SELECT score, student_id FROM student_score WHERE course_id = 2 ) t2 |
WHERE t1.score > t2.score AND t1.student_id = t2.student_id |
2、查询平均成绩大于60分的同学的学号和平均成绩: |
select student_id, avg (score) from student_score group by student_id HAVING avg (score)>=60 ; |
select ss.student_id, st.student_name, count (course_id), sum (score) from student_score ss |
left join student st on ss.student_id=st.student_id |
group by ss.student_id, st.student_name |
select count (teacher_name) from teacher where teacher_name like '李%' |
select student_id,student_name from student where student_id not in ( |
select ss.student_id from |
left join course co on ss.course_id=co.course_id |
left join teacher te on te.teacher_id= co.teacher_id |
where te.teacher_name= '叶平' |
6、查询学过“1”并且也学过编号“2”课程的同学的学号、姓名; |
select st.student_id,st.student_name from student st |
inner join ( select student_id from student_score where course_id=1) t1 on st.student_id=t1.student_id |
inner join ( select student_id from student_score where course_id=2) t2 on t1.student_id=t2.student_id |
7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; |
st.student_id,st.student_name |
inner join student st on ss.student_id=st.student_id |
inner join course co on ss.course_id=co.course_id |
inner join teacher te on te.teacher_id=co.teacher_id |
where te.teacher_name= '叶平' |
8、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名; |
select st.student_id,st.student_name from student st |
inner join ( select student_id, score from student_score where course_id=1) t1 on st.student_id=t1.student_id |
inner join ( select student_id, score from student_score where course_id=2) t2 on t1.student_id=t2.student_id |
9、查询所有课程成绩小于60分的同学的学号、姓名; |
SELECT st.student_id,st.student_name FROM student st WHERE st.student_id not in ( |
SELECT DISTINCT ss.student_id FROM student_score ss |
select st.student_id, st.student_name from student_score ss |
inner join student st on st.student_id=ss.student_id |
group by st.student_id,st.student_name |
having count (ss.course_id)<( select count (course_id) from course) |
11、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名; |
select st.student_id,st.student_name from |
student st inner join student_score ss on st.student_id=ss.student_id |
where course_id in ( select course_id from student_score where student_id=1) |
group by st.student_id,st.student_name |
14、查询和“1”号的同学学习的课程完全相同的其他同学学号和姓名; |
select ss.student_id from student_score ss |
inner join student st on ss.student_id=st.student_id |
where ss.course_id in ( select course_id from student_score where student_id=1) |
having count (ss.course_id)=( select count (course_id) from course) |
17、按平均成绩从高到低显示所有学生的“语文”、“数学”、“英语”三门课程成绩 |
( select score from student_score |
where course_id = ( SELECT course_id FROM course WHERE course_name = '语文' ) |
and student_id = ss.student_id) as 语文, |
( select score from student_score |
where course_id = ( SELECT course_id FROM course WHERE course_name = '数学' ) |
and student_id = ss.student_id) as 数学, |
( select score from student_score |
where course_id = ( SELECT course_id FROM course WHERE course_name = '英语' ) |
and student_id = ss.student_id) as 英语 |
GROUP BY ss.student_id,语文,数学,英语 |
ORDER BY avg (ss.score) DESC |
18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分 |
SELECT course_id AS 课程id, MAX (score) AS 最高分, MIN (score) AS 最低分 |
19、按各科平均成绩从低到高和及格率的百分数从高到低顺序 |
CAST ( CAST ( SUM ( case WHEN COALESCE (ss.score,0)>=60 THEN 1 ELSE 0 END ) AS FLOAT )/ |
( SELECT COUNT (*) FROM course) AS DECIMAL (10, 2)) |
INNER JOIN course co ON ss.course_id = co.course_id |
CAST ( AVG (score) AS DECIMAL (10, 2)) AS 平均成绩, |
CAST ( CAST ( COUNT (*) AS FLOAT ) / ( SELECT COUNT (*) FROM course) AS DECIMAL (10, 2)) AS 及格率 |
ORDER BY AVG (score), CAST ( CAST ( COUNT (*) AS FLOAT ) / ( SELECT COUNT (*) FROM course) AS DECIMAL (10, 2)) desc |
SELECT COUNT ( DISTINCT 平均成绩) |
( SELECT student_id, AVG (score) AS 平均成绩 FROM student_score GROUP BY student_id ) AS T1 |
( SELECT student_id, AVG (score) 平均成绩 FROM student_score GROUP BY student_id ) AS T2 |
25、查询各科成绩前三名的记录:(不考虑成绩并列情况) |
SELECT ss.student_id, ss.course_id, ss.score |
GROUP BY ss.student_id, ss.course_id, ss.score |
HAVING student_id in ( select student_id from student_score where ss.course_id=course_id LIMIT 3) |
ORDER BY ss.course_id, ss.score |
SELECT course_id, COUNT (course_id) |
27、查询出只选修了一门课程的全部学生的学号和姓名 |
SELECT ss.student_id,st.student_name, COUNT (ss.course_id) |
inner join student st on st.student_id=ss.student_id |
GROUP BY ss.student_id,st.student_name |
having COUNT (ss.course_id)=1 |
SELECT student_sex, COUNT (student_id) |
SELECT * FROM student WHERE student_name LIKE '张%' |
SELECT student_name, count (*) FROM student group by student_name having count (*)>1 |
32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列 |
select course_id, avg (score) from student_score group by course_id order by avg (score) asc ,course_id |
33、查询平均成绩大于70的所有学生的学号、姓名和平均成绩 |
SELECT ss.student_id,st.student_name, AVG (ss.score) |
inner join student st on ss.student_id=st.student_id |
GROUP BY ss.student_id,st.student_name |
ORDER BY AVG (ss.score) ASC , ss.student_id |
34、查询课程名称为“语文”,且分数低于60的学生姓名和分数 |
select st.student_name,ss.score |
inner join student st on ss.student_id=st.student_id |
inner join course co on ss.course_id=co.course_id |
where co.course_name= '语文' and ss.score<60 |
select st.student_name,co.course_name,ss.score |
inner join student st on ss.student_id=st.student_id |
inner join course co on ss.course_id=co.course_id |
36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; |
select st.student_name,co.course_name,ss.score |
inner join student st on ss.student_id=st.student_id |
inner join course co on ss.course_id=co.course_id |
40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩 |
select st.student_name,co.course_name,te.teacher_name,ss.score |
inner join student st on ss.student_id=st.student_id |
inner join course co on ss.course_id=co.course_id |
inner join teacher te on co.teacher_id=te.teacher_id |
where te.teacher_name= '叶平' |
select course_id, count (course_id) from student_score group by course_id order by course_id |
t0.student_id, t0.course_id, t0.score |
WHERE t0.course_id = course_id |
44、统计每门课程的学生选修人数,要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人 数相同,按课程号升序排列 |
select course_id, count (student_id) from student_score group by course_id order by count (student_id) desc , course_id |
select student_id, count (*) |
order by count (*) desc , student_id |
SELECT co.course_id, co.course_name |
INNER JOIN course co ON co.course_id = ss.course_id |
GROUP BY co.course_id, co.course_name |
HAVING COUNT (co.course_id) = ( SELECT COUNT (student_id) FROM student) |
48、查询两门以上不及格课程的同学的学号及其平均成绩 |
select student_id, avg (score) from student_score where score<60 group by student_id having count (1)>=2 |
- 学生表 课程表 成绩表 教师表 50个常用sql语句
原文:http://www.cnblogs.com/zengxiangzhan/archive/2009/09/23/1572276.html Student(S#,Sname,Sage,Ssex) ...
- 面试题: 数据库 sql优化 sql练习题 有用 学生表,课程表,成绩表,教师表 练习
什么是存储过程?有哪些优缺点? 什么是存储过程?有哪些优缺点? 存储过程就像我们编程语言中的函数一样,封装了我们的代码(PLSQL.T-SQL). 存储过程的优点: 能够将代码封装起来 保存在数据库之 ...
- sql面试题(学生表_课程表_成绩表_教师表)
原帖链接:http://bbs.csdn.net/topics/280002741 表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程 ...
- 经典sql面试题(学生表_课程表_成绩表_教师表)
转载:https://www.cnblogs.com/qixuejia/p/3637735.html 表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cna ...
- SQL常见面试题(学生表_课程表_成绩表_教师表)
表架构 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师 ...
- MySQL(学生表、教师表、课程表、成绩表)多表查询
1.表架构 student(sid,sname,sage,ssex) 学生表 course(cid,cname,tid) 课程表 sC(sid,cid,score) 成绩表 teacher(tid,t ...
- SQL Server 基础之《学生表-教师表-课程表-选课表》(二)
表结构 --学生表tblStudent(编号StuId.姓名StuName.年龄StuAge.性别StuSex) --课程表tblCourse(课程编号CourseId.课程名称CourseName. ...
- SQL Server 基础之《学生表-教师表-课程表-选课表》
一.数据库表结构及数据 建表 CREATE TABLE Student ( S# INT, Sname ), Sage INT, Ssex ) ) CREATE TABLE Course ( C# I ...
- SQL Server 基础之《学生表-教师表-课程表-选课表》(一)
数据库表结构及数据 建表 CREATE TABLE Student ( S# INT, Sname ), Sage INT, Ssex ) ) CREATE TABLE Course ( C# INT ...
随机推荐
- CEO的作用
看到有人讨论CEO的作用. 一个观点认为CEO有三大任务: 1)为公司确定战略,并与股东沟通 2)为公司其他职位找来合适的人员 3)保证公司随时有足够的钱 他认为,可能CEO会有其他的作用,但是这三点 ...
- windows10开启内置ubuntu系统,使用xshell连接
windows安装配置ubuntu系统内置子系统 官方文档:https://docs.microsoft.com/zh-cn/windows/wsl/about https://www.jianshu ...
- Microsoft Excel 准确按照一页的宽度和高度打印
设置 Microsoft Excel 准确按照一页的宽度和高度打印 Sheet1. VBA复制 With Worksheets("Sheet1").PageSetup .Zoo ...
- 592. Fraction Addition and Subtraction
Problem statement: Given a string representing an expression of fraction addition and subtraction, y ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submi ...
- [ 浙江大学 程序设计专题 ] 四个专题代码 报告 PPT共享
[原创]转载请注明出处,请勿用于作弊 专题一: 链接: https://pan.baidu.com/s/11xCwvuPHDkTPeOB_yzJWnw 提取码: prup 专题二: 链接: https ...
- Multiprocessing system employing pending tags to maintain cache coherence
A pending tag system and method to maintain data coherence in a processing node during pending trans ...
- Android 网络连接状态的监控
有些应用需要连接网络,例如更新后台服务,刷新数据等,最通常的做法是定期联网,直接使用网上资源.缓存数据或执行一个下载任务来更新数据. 但是如果终端设备没有连接网络,或者网速较慢,就没必要执行这些任务. ...
- cogs——2416. [HZOI 2016]公路修建
2416. [HZOI 2016]公路修建 ★☆ 输入文件:hzoi_road.in 输出文件:hzoi_road.out 简单对比时间限制:1 s 内存限制:128 MB [题目描述 ...
- CSS3(各UI元素状态伪类选择器受浏览器的支持情况)
选择器 Firefox Safari Opera IE Chrome E:hover Y Y Y Y Y E:active Y Y Y N Y E:focus Y Y Y Y Y E:enabled ...