⽤mySQL创建学⽣选课表_50个常⽤sql语句⽹上流⾏的学⽣
选课表的例⼦
50个常⽤sql语句
建表:
--学⽣表tblStudent(编号StuId、姓名StuName、年龄StuAge、性别StuSex)
--课程表tblCourse(课程编号CourseId、课程名称CourseName、教师编号TeaId)
--成绩表tblScore(学⽣编号StuId、课程编号CourseId、成绩Score)
--教师表tblTeacher(教师编号TeaId、姓名TeaName)
问题:
1、查询“001”课程⽐“002”课程成绩⾼的所有学⽣的学号;mysql面试题学生表
select a.StuId from (select StuId,Score from tblScore where CourseId='001') a,(select StuId,Score
from tblScore where CourseId='002') b
where a.Score>b.Score and a.StuId=b.StuId;
2、查询平均成绩⼤于60分的同学的学号和平均成绩;
程序设计语言是自然语言吗select StuId,avg(Score)
from tblScore
group by StuId having avg(Score) >60;
3、查询所有同学的学号、姓名、选课数、总成绩;
select tblStudent.StuId,tblStudent.StuName,count(tblScore.CourseId),sum(Score)
from tblStudent left Outer join tblScore on tblStudent.StuId=tblScore.StuId
group by tblStudent.StuId,StuName
4、查询姓“李”的⽼师的个数;
select count(distinct(TeaName))
from tblTeacher
where TeaName like '李%';
5、查询没学过“叶平”⽼师课的同学的学号、姓名;
select tblStudent.StuId,tblStudent.StuName
from tblStudent
where StuId not in (select distinct( tblScore.StuId) from tblScore,tblCourse,tblTeacher where
tblScore.CourseId=tblCourse.CourseId and tblTeacher.TeaId=tblCourse.TeaId and tblTeacher.TeaName='叶平');
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
select tblStudent.StuId,tblStudent.StuName from tblStudent,tblScore where tblStudent.StuId=tblScore.StuId and tblScore.CourseId='001'and exists( Select * from tblScore as tbl
Score_2 where tblScore_2.StuId=tblScore.StuId and tblScore_2.CourseId='002');
7、查询学过“叶平”⽼师所教的所有课的同学的学号、姓名;
select StuId,StuName
from tblStudent
where StuId in (select StuId from tblScore ,tblCourse ,tblTeacher where tblScore.CourseId=tblCourse.CourseId and tblTeacher.TeaId=tblCourse.TeaId and tblTeacher.TeaName='叶平' group byStuId having count(tblScore.CourseId)=(select count(CourseId) from tblCourse,tblTeacher where tblTeacher.TeaId=tblCourse.TeaId and TeaName='叶平'));
8、查询课程编号“002”的成绩⽐课程编号“001”课程低的所有同学的学号、姓名;
Select StuId,StuName from (select tblStudent.StuId,tblStudent.StuName,Score ,(select Score from tblScore tblScore_2 where tblScore_2.StuId=tblStudent.StuId and tblScore_2.CourseId='002') Score2
from tblStudent,tblScore where tblStudent.StuId=tblScore.StuId and CourseId='001') S_2 where Score2
9、查询所有课程成绩⼩于60分的同学的学号、姓名;
select StuId,StuName
from tblStudent
where StuId not in (select tblStudent.StuId from tblStudent,tblScore where S.StuId=tblScore.StuId and Score>60);
10、查询没有学全所有课的同学的学号、姓名;
select tblStudent.StuId,tblStudent.StuName
from tblStudent,tblScore
where tblStudent.StuId=tblScore.StuId group by tblStudent.StuId,tblStudent.StuName having count(CourseId)
11、查询⾄少有⼀门课与学号为“1001”的同学所学相同的同学的学号和姓名;
select StuId,StuName from tblStudent,tblScore where tblStudent.StuId=tblScore.StuId and CourseId
in select CourseId from tblScore where StuId='1001';
12、查询⾄少学过学号为“001”同学所有⼀门课的其他同学学号和姓名;
select distinct tblScore.StuId,StuName
from tblStudent,tblScore
where tblStudent.StuId=tblScore.StuId and CourseId in (select CourseId from tblScore where StuId='001');
13、把“tblScore”表中“叶平”⽼师教的课的成绩都更改为此课程的平均成绩;
update tblScore set Score=(select avg(tblScore_2.Score)
from tblScore tblScore_2
where tblScore_2.CourseId=tblScore.CourseId ) from tblCourse,tblTeacher where
tblCourse.CourseId=tblScore.CourseId and tblCourse.TeaId=tblTeacher.TeaId and tblTeacher.TeaName='叶平');
14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;
mysqlworkbench打开数据库select StuId from tblScore where CourseId in (select CourseId from tblScore where StuId='1002')
group by StuId having count(*)=(select count(*) from tblScore where StuId='1002');
15、删除学习“叶平”⽼师课的tblScore表记录;
Delect tblScore
from tblCourse ,tblTeacher
where tblCourse.CourseId=tblScore.CourseId and tblCourse.TeaId= tblTeacher.TeaId and TeaName='叶平';
16、向tblScore表中插⼊⼀些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2、
号课的平均成绩;
Insert tblScore select StuId,'002',(Select avg(Score)
hbuilder用英语怎么读from tblScore where CourseId='002') from tblStudent where StuId not in (Select StuId from tblScore where
CourseId='002');
17、按平均成绩从⾼到低显⽰所有学⽣的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显⽰: 学⽣ID,,数据库,企业管理,英语,有效课程数,有效平均分
SELECT StuId as 学⽣ID
,(SELECT Score FROM tblScore WHERE tblScore.StuId=t.StuId AND CourseId='004') AS 数据库
,(SELECT Score FROM tblScore WHERE tblScore.StuId=t.StuId AND CourseId='001') AS 企业管理
,(SELECT Score FROM tblScore WHERE tblScore.StuId=t.StuId AND CourseId='006') AS 英语
,COUNT(*) AS 有效课程数, AVG(t.Score) AS 平均成绩
FROM tblScore AS t
GROUP BY StuId
ORDER BY avg(t.Score)
18、查询各科成绩最⾼和最低的分:以如下形式显⽰:课程ID,最⾼分,最低分
SELECT L.CourseId As 课程ID,L.Score AS 最⾼分,R.Score AS 最低分
FROM tblScore L ,tblScore AS R
WHERE L.CourseId = R.CourseId and
L.Score = (SELECT MAX(IL.Score)
FROM tblScore AS IL,tblStudent AS IM
WHERE L.CourseId = IL.CourseId and IM.StuId=IL.StuId
GROUP BY IL.CourseId)
AND
R.Score = (SELECT MIN(IR.Score)
FROM tblScore AS IR
WHERE R.CourseId = IR.CourseId
GROUP BY IR.CourseId
);
19、按各科平均成绩从低到⾼和及格率的百分数从⾼到低顺序
SELECT t.CourseId AS 课程号,max(tblCourse.CourseName)AS 课程名,isnull(AVG(Score),0) AS 平均成绩
,100 * SUM(CASE WHEN isnull(Score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数
FROM tblScore T,tblCourse
where t.CourseId=tblCourse.CourseId
GROUP BY t.CourseId
ORDER BY 100 * SUM(CASE WHEN isnull(Score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC
20、查询如下课程平均成绩和及格率的百分数(⽤"1⾏"显⽰):企业管理(001),马克思(002),OO&UML(003),数据库(004)
SELECT SUM(CASE WHEN CourseId ='001' THEN Score ELSE 0 END)/SUM(CASE CourseId WHEN '001' THEN 1 ELSE 0 END) AS 企业管理平均分
,100 * SUM(CASE WHEN CourseId = '001' AND Score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN CourseId = '001' THEN 1 ELSE 0 END) AS 企业管理及格百分数
,SUM(CASE WHEN CourseId = '002' THEN Score ELSE 0 END)/SUM(CASE CourseId WHEN '002' THEN 1 ELSE 0 END) AS 马克思平均分
,100 * SUM(CASE WHEN CourseId = '002' AND Score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN CourseId = '002' THEN 1 ELSE 0 END) AS 马克思及格百分数
,SUM(CASE WHEN CourseId = '003' THEN Score ELSE 0 END)/SUM(CASE CourseId WHEN '003' THEN 1 ELSE 0 END) AS UML平均分
,100 * SUM(CASE WHEN CourseId = '003' AND Score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN CourseId = '003' THEN 1 ELSE 0 END) AS UML及格百分数
,SUM(CASE WHEN CourseId = '004' THEN Score ELSE 0 END)/SUM(CASE CourseId WHEN '004' THEN 1 ELSE 0 END) AS 数据库平均分
,100 * SUM(CASE WHEN CourseId = '004' AND Score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN CourseId = '004' THEN 1 ELSE 0 END) AS 数据库及格百分数
FROM tblScore
21、查询不同⽼师所教不同课程平均分从⾼到低显⽰
SELECT max(Z.TeaId) AS 教师ID,MAX(Z.TeaName) AS 教师姓名,C.CourseId AS 课程ID,MAX(C.CourseName) AS 课程名称,AVG(Score) AS 平均成绩
FROM tblScore AS T,tblCourse AS C ,tblTeacher AS Z
where T.CourseId=C.CourseId and C.TeaId=Z.TeaId
GROUP BY C.CourseId
ORDER BY AVG(Score) DESC
22、查询如下课程成绩第3名到第6名的学⽣成绩单:企业管理(001),马克思(002),UML(003),数据库(004)
[学⽣ID],[学⽣姓名],企业管理,马克思,UML,数据库,平均成绩
SELECT DISTINCT top 3
tblScore.StuId As 学⽣学号,
tblStudent.StuName AS 学⽣姓名,
T1.Score AS 企业管理,
T2.Score AS 马克思,
T3.Score AS UML,
T4.Score AS 数据库,
ISNULL(T1.Score,0) + ISNULL(T2.Score,0) + ISNULL(T3.Score,0) + ISNULL(T4.Score,0) as 总分
FROM tblStudent,tblScore LEFT JOIN tblScore AS T1
ON tblScore.StuId = T1.StuId AND T1.CourseId = '001'
LEFT JOIN tblScore AS T2
ON tblScore.StuId = T2.StuId AND T2.CourseId = '002'
LEFT JOIN tblScore AS T3
ON tblScore.StuId = T3.StuId AND T3.CourseId = '003'
LEFT JOIN tblScore AS T4
ON tblScore.StuId = T4.StuId AND T4.CourseId = '004'
WHERE tblStudent.StuId=tblScore.StuId and
ISNULL(T1.Score,0) + ISNULL(T2.Score,0) + ISNULL(T3.Score,0) + ISNULL(T4.Score,0)
NOT IN
(SELECT
DISTINCT
TOP 15 WITH TIES
ISNULL(T1.Score,0) + ISNULL(T2.Score,0) + ISNULL(T3.Score,0) + ISNULL(T4.Score,0)
FROM tblScore
LEFT JOIN tblScore AS T1
ajax如何获取后台数据ON tblScore.StuId = T1.StuId AND T1.CourseId = 'k1'
LEFT JOIN tblScore AS T2
ON tblScore.StuId = T2.StuId AND T2.CourseId = 'k2'
LEFT JOIN tblScore AS T3
ON tblScore.StuId = T3.StuId AND T3.CourseId = 'k3'
消失的夫妻判决书LEFT JOIN tblScore AS T4
ON tblScore.StuId = T4.StuId AND T4.CourseId = 'k4'
ORDER BY ISNULL(T1.Score,0) + ISNULL(T2.Score,0) + ISNULL(T3.Score,0) + ISNULL(T4.Score,0) DESC);
23、统计列印各科成绩,各分数段⼈数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
SELECT tblScore.CourseId as 课程ID,CourseName as 课程名称
,SUM(CASE WHEN Score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]
,SUM(CASE WHEN Score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85 - 70]
,SUM(CASE WHEN Score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70 - 60]
,SUM(CASE WHEN Score < 60 THEN 1 ELSE 0 END) AS [60 -]
FROM tblScore,tblCourse
where tblScore.CourseId=tblCourse.CourseId
GROUP BY tblScore.CourseId,CourseName;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论