數據庫表
sql語句格式:
實戰:
- 設置sc(成績表)表中sno,cno唯一鍵
alter table sc add unique(sno,cno);
- 查詢所有同學的學號、姓名、選課數、總成績
SELECT sc.sno,sname,COUNT(cno) '選課數',SUM(score) 'sum'
FROM student,sc
WHERE sc.sno=student.sno
GROUP BY sc.sno
- 查詢姓“李”的老師的個數
SELECT count(tno)
FROM teacher
where tname LIKE '李%'
- 求選擇了Java課程且成績小於60的同學名單,按名字升序排列
select student.sname from sc,student
where student.sno=sc.sno and sc.cno in
(select cno from course where cname='JAVA')and sc.score<60
ORDER BY student.sname
- 查詢課程1的成績比課程2的成績高的所有學生的學號
SELECT s1.sno
from
(SELECT sno ,score from sc where cno=1) s1,
(SELECT sno ,score from sc where cno=2) s2
where s1.sno=s2.sno and s1.score>s2.score;
- 求Java課程小於平均分的學生的各科成績
select sno ,cno,score
from sc
where sno in(
SELECT sno
from sc,(SELECT cno,avg(score) a from sc where cno =(SELECT cno from course where cname='JAVA')) avgsc
where sc.cno=avgsc.cno and sc.score<avgsc.a
);
- 查詢平均成績大於60分的同學的學號和平均成績
SELECT sno ,AVG(score)
FROM sc
GROUP BY sno
HAVING AVG(score)>60
- 查詢所有課程成績小於60分的同學的學號、姓名
SELECT sno,sname
FROM student
where sno not in (SELECT DISTINCT sno from sc where score >60 );
- 給選擇了JAVA課程且成績小於等於90分的成績加10分
UPDATE sc
SET score=score+10
where cno= (SELECT cno from course where cname='JAVA') and sc.score<90
相關連接:
SQL常用語句總結 https://blog.csdn.net/u012757419/article/details/94829361
經典sql實例大全 https://blog.csdn.net/LoulseLong/article/details/79388345?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task