轉自: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 |