MySql数据库基本select查询语句练习题,初学者易懂。

MySql数据库基本select查询语句练习题,初学者易懂。MySQL数据库的基本查询语句题目:

大家好,又见面了,我是你们的朋友全栈君。



数据库建立四个表:分别为

student(sid,sname,sage,ssex)

teacher(tid,tname)

course(cid,cname,tid)

sc(sid,cid,score)

— 1、查询“001”课程比”002″课程成绩高的所有学生的学号。

select a.sid FROM
(select * from sc where cid=”001″) as a,
(select * from sc where cid = “002”)as b
where a.sid = b.sid and a.score>b.score

— 2、查询平均成绩大于60分的同学的学号和平均成绩

select sid,avg(score) 
FROM sc
GROUP BY sid
HAVING avg(score)>=70;

— 3、查询所有的同学的学号、姓名、选课数、总成绩

select student.sid, sname,COUNT(*),sum(score)
from sc,student
where sc.sid = student.sid 
GROUP BY sid;

— 4、查询姓“李”的老师的个数

select COUNT(*)
from teacher
where tname LIKE ‘大%’;

— 5、查询没学过“叶平“老师的课程的同学的学号、姓名

SELECT sid,sname
from student
where sid NOT in(select sid
from sc,course,teacher
where sc.cid = course.cid and teacher.tid = course.tid and teacher.tname = “叶良辰”);

—  6、查询所有    课程有挂科的同学的学号、姓名

SELECT sid ,sname
from student
where 
sid in (select sid from sc) AND
sid not in(select sid from sc where score<60 GROUP BY sid);

—  7、查询至少一门课与学号为“2”的同学所学相同的学生的学号和姓名

SELECT DISTINCT(sc.sid)
from sc,student
where student.sid = sc.sid and cid in(select cid from sc where sc.sid=’2′) and student.sid<>’2′;

— 8、统计列印各科成绩,各分数段人数:课程ID、课程名称,100-85,85-70,70-60,<60

select sc.cid as ‘课程ID’,cname as ‘课程名称’,
SUM(case WHEN score between 85 and 100 then 1 else 0 end) as ’85-100′,
SUM(case WHEN score between 70 and 84 then 1 else 0 end) as ’70-84′,
SUM(case WHEN score between 69 and 60 then 1 else 0 end) as ’60-69′,
SUM(case WHEN score between 0 and 100 then 59 else 0 end) as ‘0-59’
from course,sc
where sc.cid=course.cid
GROUP BY sc.cid;

—  9、查询每门课程的课程名和选修的学生数

select cname,count(*)
from sc,course
where course.cid=sc.cid 
group by sc.cid;

— 10、查询出只选修了一门课程的全部同学的学号、姓名

select sc.sid,sname
from sc,student
where student.sid=sc.sid
GROUP BY (sc.sid)
HAVING COUNT(*)=1;

— 11、查询男生、女生的人数

(select “男生” AS “性别”,count(*) from student where ssex=”男”)
UNION
(select “女生” as “性别”, count(*) from student where ssex=”女”);

—  12、查询姓“李”的师生名单

(select sname as ‘名单’ from student where sname like ‘李%’)
union
(select tname as ‘名单’ from teacher where tname like “李%”);

在数据库建立三个表:

学生表:student(sno,sname,sage,ssex,sdept)==(学号,姓名,年龄,性别,系别)

课程表:course(cno,cname,credit)==(课程号,课程名,学分)

选课表:sc(sno,cno,grade)===(学号,课程号,成绩)

— 写出选修了数据结构的同学的学号和姓名

select *
from student
where sno in(select sno
 from sc,course 
where sc.cno=course.cno and cname=’数据结构’ 
);

— 1.统计每门课的选课人数,包括没有人选的课程,列出课程号及选课情况,其中选课情况为,如果此门课的选课人数超过100
— 人,则显示人多,40-100一般 1-40人好,无人选

select  course.cno ,
case 
when (count(*)>=40 and count(*)<=100) then  ‘较多’ 
when (count(*)>1 and count(*)<40) then ‘较少’
else ‘无人选’ end as ‘选课情况’
from course left join sc
on course.cno=sc.cno
GROUP BY cno;

—  2.查询计算机有哪些学生没有选课,列出姓名和学号(用外连接)
select sname
from student left join  sc on
sc.sno=student.sno
where sdept=”计科” and sc.sno is null;

2>– 成绩小于60的学生姓名,课程,成绩

select sname,cname,grade
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno
and grade<60;

— 3. 统计每个学生的选课人数和考试总成绩,并按照选课门数升序排列
select sno,count(*)’选课门数’ ,sum(grade)’总成绩’
from sc
GROUP BY sno
ORDER BY count(*) DESC;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/140907.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月8日 下午10:00
下一篇 2022年5月8日 下午10:00


相关推荐

  • seekg ()[通俗易懂]

    seekg ()[通俗易懂] seekg语法:istream&seekg(off_typeoffset,ios::seekdirorigin);istream&seekg(pos_typeposition);函数seekg()用于输入流,并且它将重新设置”get”指针到当前流的从origin偏移offset个字节的位置上,或是置”get”指针在position位

    2022年6月10日
    41
  • Matlab axis函数[通俗易懂]

    Matlab axis函数[通俗易懂]axis  用于操作普通的坐标属性,(轴的缩放和外观)。axis([xminxmaxyminymax])  设置当前坐标轴x轴和y轴的限制范围axis([xminxmaxyminymaxzminzmaxcmincmax])设置x,y,z轴的限制范围和色差范围。v=axis返回一个行向量,记录了坐标范围axisauto解除限制,恢复到默认状态例…

    2022年6月13日
    64
  • PAT乙级题目对应知识点分类梳理

    PAT乙级题目对应知识点分类梳理PAT乙级的90道题的知识点与对应的题号整理如下,便于做专项练习和巩固!1、字符串函数考察字符串相关知识,如逆转、字母与数字的判断与转化、字符串拼接、字符串比较题号:1002、1006、1009、1014、1021、1024、1031/1039、1042、1043、/1048/1052/1054/1058/1067/1079、1081/1084/1086、2、STL容器考察ST…

    2022年5月4日
    57
  • OpenClaw 接入 QQ 机器人完整教程(2026):扫码创建、三条命令搞定

    OpenClaw 接入 QQ 机器人完整教程(2026):扫码创建、三条命令搞定

    2026年3月13日
    2
  • 2019PHP面试题大全【PHP基础部分】

    2019PHP面试题大全【PHP基础部分】

    2021年11月5日
    42
  • BERT中文实战(文本相似度)

    BERT中文实战(文本相似度)个人 githubBERT 本质上是一个两段式的 NLP 模型 第一个阶段叫做 Pre training 跟 WordEmbeddin 类似 利用现有无标记的语料训练一个语言模型 第二个阶段叫做 Fine tuning 利用预训练好的语言模型 完成具体的 NLP 下游任务 Google 已经投入了大规模的语料和昂贵的机器帮我们完成了 Pre training 过程附上中文预训练 bert 链接

    2026年3月19日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号