學生表、課程表、 成績表 、教師表sql練習


轉自:http://yuncode.net/code/c_58df7a8ca687e58

1、查詢“1”課程比“2”課程成績高的所有學生的學號;
SELECT t1.student_id
FROM
    (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 ;
 
3、查詢所有同學的學號、姓名、選課數、總成績;
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
order by ss.student_id
 
4、查詢姓“李”的老師的個數;
select count(teacher_name) from teacher where teacher_name like '李%'
 
5、查詢沒學過“葉平”老師課的同學的學號、姓名;
select student_id,student_name from student where student_id not in (
    select ss.student_id from
    student_score ss
    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='葉平'
    group by ss.student_id
)
 
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、查詢學過“葉平”老師所教的所有課的同學的學號、姓名;
SELECT
    st.student_id,st.student_name
FROM student_score ss
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
where t2.score<t1.score
 
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
    WHERE ss.score>=60
)
 
10、查詢沒有學全所有課的同學的學號、姓名;
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)
group by ss.student_id
having count(ss.course_id)=(select count(course_id) from course)
 
17、按平均成績從高到低顯示所有學生的“語文”、“數學”、“英語”三門課程成績
SELECT ss.student_id,
    avg(ss.score) 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 數學,
    (select score from student_score
    where course_id = (SELECT course_id FROM course  WHERE course_name ='英語')
    and student_id = ss.student_id) as 英語
FROM student_score ss
GROUP BY ss.student_id,語文,數學,英語
ORDER BY avg(ss.score)  DESC
 
18、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分
SELECT course_id AS 課程id, MAX (score) AS 最高分, MIN (score) AS 最低分
FROM student_score
GROUP BY course_id
 
19、按各科平均成績從低到高和及格率的百分數從高到低順序
SELECT
    co.course_name AS 科目名稱,
    avg (ss.score) as 平均成績,
    CAST (CAST (SUM(case WHEN COALESCE(ss.score,0)>=60 THEN ELSE ENDAS FLOAT)/
    (SELECT COUNT(*) FROM course) AS DECIMAL (10, 2))
FROM
    student_score ss
INNER JOIN course co ON ss.course_id = co.course_id
GROUP BY
    co.course_name
 
或者
 
SELECT
    student_id,
    CAST (AVG(score) AS DECIMAL(10, 2)) AS 平均成績,
    CAST (CAST (COUNT(*) AS FLOAT) / (SELECT COUNT(*) FROM course) AS DECIMAL (10, 2)) AS 及格率
FROM student_score
WHERE   score >= 60
GROUP BY student_id
ORDER BY AVG (score),CAST (CAST (COUNT(*) AS FLOAT) / (SELECT COUNT(*) FROM course) AS DECIMAL (10, 2)) desc
 
24、查詢學生平均成績及其名次
SELECT 1 + (
        SELECT COUNT (DISTINCT 平均成績)
        FROM
            SELECT student_id, AVG (score) AS 平均成績 FROM student_score GROUP BY student_id ) AS T1
        WHERE 平均成績 > T2.平均成績
    AS 名次,
    student_id AS 學生學號,
    平均成績
FROM
    SELECT student_id, AVG (score) 平均成績 FROM student_score GROUP BY   student_id ) AS T2
ORDER BY
    平均成績 DESC;
 
25、查詢各科成績前三名的記錄:(不考慮成績並列情況)
SELECT ss.student_id, ss.course_id, ss.score
FROM student_score ss
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
 
26、查詢每門課程被選修的學生數
SELECT course_id, COUNT (course_id)
FROM student_score
GROUP BY    course_id
ORDER BY    course_id
 
27、查詢出只選修了一門課程的全部學生的學號和姓名
SELECT ss.student_id,st.student_name, COUNT (ss.course_id)
FROM student_score ss
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
ORDER BY    ss.student_id
 
28、查詢男生、女生人數
SELECT student_sex, COUNT (student_id)
FROM student
GROUP BY student_sex
 
29、查詢姓“張”的學生名單
SELECT FROM   student WHERE student_name LIKE '張%'
 
30、查詢同名同性學生名單,並統計同名人數
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)
FROM student_score ss
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
from student_score ss
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
 
35、查詢所有學生的選課情況;
select st.student_name,co.course_name,ss.score
from student_score ss
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
from student_score ss
inner join student st on ss.student_id=st.student_id
inner join course co on ss.course_id=co.course_id
where ss.score>70
 
40、查詢選修“葉平”老師所授課程的學生中,成績最高的學生姓名及其成績
select st.student_name,co.course_name,te.teacher_name,ss.score
from student_score ss
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='葉平'
order by ss.score desc
limit 1
 
41、查詢各個課程及相應的選修人數
select course_id,count(course_id) from student_score group by course_id order by course_id
 
43、查詢每門功成績最好的前兩名
SELECT
    t0.student_id, t0.course_id, t0.score
FROM student_score t0
WHERE
    t0.student_score_id IN (
        SELECT student_score_id
        FROM student_score
        WHERE t0.course_id = course_id
        ORDER BY    score DESC
        limit 2
    )
 
44、統計每門課程的學生選修人數,要求輸出課程號和選修人數,查詢結果按人數降序排列,查詢結果按人數降序排列,若人    數相同,按課程號升序排列
select course_id,count(student_id) from student_score group by course_id order by count(student_id) desc, course_id
 
45、檢索至少選修兩門課程的學生學號
select student_id,count(*)
from student_score
group by student_id
having count(*)>=2
order by count(*) desc, student_id
 
46、查詢全部學生都選修的課程的課程號和課程名
SELECT co.course_id, co.course_name
FROM student_score ss
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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM