--第一题查询Student表中的所有记录的Sname、Ssex和Class列。
select Sname,Ssex,Class from Student;
--第二题查询教师所有的单位即不重复的Depart列。
select distinct Depart from Teacher;
--第三题、 查询Student表的所有记录
select * from Student;
--第四题、 查询Score表中成绩在60到80之间的所有记录。
select * from Score where Degree between 60 and 80;
--第五题、 查询Score表中成绩为85,86或88的记录。
select * from Score where Degree = 85 or Degree = 86 or Degree = 88;
--第六题、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from Student where Class = 95031 or Ssex = '女';
--第七题、 以Class降序查询Student表的所有记录。
select * from Student order by Class asc;
--第八题、 以Cno升序、Degree降序查询Score表的所有记录。
select * from Score order by Cno asc , Degree desc;
--第九题、 查询“95031”班的学生人数。
select count(*) from Student where Class = 95031;
--第十题、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
select Sno,Cno from Score where Degree = (select max(Degree) from Score);
--第十一题、 查询每门课的平均成绩。
select avg(Degree) from Score where Cno='3-245';
select avg(Degree) from Score where Cno = '3-105';
select avg(Degree) from Score where Cno = '6-166';
select distinct Cno from Score;
select Cno,avg(Degree) from Score where select distinct Cno from Score;
--上面是错误历程-------------------------------------------------------------------------------------
select Cno,avg(Degree) from Score group by Cno;
--第十二题、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select CNO,avg(Degree) from Score where Cno like '3%' group by Cno having count(*)>4;
--先取首字母为3的 在分组 分组 完成后筛选条件为 Cno数量大于等于5;
--where 后面不能加聚合函数-----------------------------------------------------------------------------------------
--第十三题、查询分数大于70,小于90的Sno列。
select Sno,Degree from Score where Degree>70 and Degree<90;
--第十四题、查询所有学生的Sname、Cno和Degree列。
select Sname,Cno,Degree from Student join Score on Student.Sno = Score.Sno;
--方法二
select Student.Sname,Score.Cno,Score.Degree from Student,Score where Student.Sno = Score.Sno;
--第十五题、查询所有学生的Sno、Cname和Degree列。
select Sno,Cname,Degree from Course join Score on (Score.Cno = Course.Cno);
--第十六题、查询所有学生的Sname、Cname和Degree列。
select Sname,Cname,Degree from Student join Score on (Student.Sno = Score.Sno) join Course on (Course.Cno = Score.Cno);
--第十七题、 查询“95033”班学生的平均分。
select Class,avg(Degree) from Score join Student on(Student.Sno = Score.Sno) where Class = 95033;
--第十八题、 假设使用如下命令建立了一个grade表:
create table grade(low int(3),upp int(3),rank char(1))
insert into grade values(90,100,'A');
insert into grade values(80,89,'B')
insert into grade values(70,79,'C')
insert into grade values(60,69,'D')
insert into grade values(0,59,'E')
--现查询所有同学的Sno、Cno和rank列。
select Sno,Cno,rank from grade join Score;
select Sno,Cno,rank from grade join Score on (Degree between low and upp);
--第十九题、 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select Degree from Score where Sno = 109 and Cno = '3-105';

select * from Score where Degree > (select Degree from Score where Sno = 109 and Cno = '3-105') group by Sno having Cno = '3-105';

select Sno from Score where Cno = '3-105' and Degree > (select max(Degree) from Score where Sno = 109 and Cno = '3-105');

--第二十题、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
select Sno from Score group by Sno having count(Cno)>1--选多门课同学的学号
select max(Degree) from Score--最高分
select * from Score where Degree < (select max(Degree) from Score)--小于最高分的学生信息
--选取选修多门课程且非最高成绩的学生学号
select sno from Score where Degree < (select max(Degree) from Score) group by Sno having count(Cno)>1;
--该学号所有信息
select * from Score where sno in (select sno from Score where Degree < (select max(Degree) from Score) group by Sno having count(Cno)>1)
--第二十一题、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from Score where Degree > (select max(Degree) from Score where Sno = 109) and Cno = '3-105';
--第二十二题、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

select Sno,Sname,Sbirthday from Student where year(Sbirthday) = (select year(Sbirthday) from Student where Sno = 107);

--第二十三题、查询“张旭“教师任课的学生成绩。
select Tno from Teacher where Tname = '张旭'
select Cno from Course where Tno = (select Tno from Teacher where Tname = '张旭')
select Degree from Score where Cno = (select Cno from Course where Tno = (select Tno from Teacher where Tname = '张旭'))
--第二十四题、查询选修某课程的同学人数多于5人的教师姓名。
select Cno from Score group by Cno having count(Cno)>5
select Tno from Course where Cno in (select Cno from Score group by Cno having count(Cno)>5)
select Tname from Teacher where Tno in (select Tno from Course where Cno in (select Cno from Score group by Cno having count(Cno)>5))

--第二十五题、查询95033班和95031班全体学生的记录。

select * from Student where class = 95033 or class = 95031

--第二十六题、 查询存在有85分以上成绩的课程Cno.

select distinct Cno from Score where Degree > 85

--第二十七题、查询出“计算机系“教师所教课程的成绩表。

select Tno from Teacher where Depart = '计算机系'
select Cno from Course where Tno in (select Tno from Teacher where Depart = '计算机系')
select * from Score where Cno in (select Cno from Course where Tno in (select Tno from Teacher where Depart = '计算机系'))

--第二十八题、查询“计算 机系”与“电子工程系“不同职称的教师的Tname和Prof。

select Tname,Prof from Teacher where Prof in (select Prof from Teacher where Depart = '计算机系' or Depart = '电子工程系' group by Prof having count(Prof) = 1)
--select Prof from Teacher where Depart = '计算机系' or Depart = '电子工程系' group by Prof having count(Prof) = 1

--第二十九题、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。

select Cno,Sno,Degree from Score where Cno = '3-105' and Degree >= (select max(Degree) from Score where Cno = '3-245')
order by Degree asc

--第三十题、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select Cno,Sno,Degree from Score where Cno = '3-105' and Degree >= (select max(Degree) from Score where Cno = '3-245')

--第三十一题、 查询所有教师和同学的name、sex和birthday.

select Tname,Tsex,Tbirthday from Teacher union select Sname,Ssex,Sbirthday from Student

--第三十二题、查询所有“女”教师和“女”同学的name、sex和birthday.

select Tname,Tsex,Tbirthday from Teacher where Tsex = '女' union select Sname,Ssex,Sbirthday from Student where Ssex = '女'

--第三十三题、 查询成绩比该课程平均成绩低的同学的成绩表。

select Cno,avg(Degree) from Score group by Cno;
select * from Score join (select Cno as Pno,avg(Degree) as pj from Score group by Pno) as pjb on (Score.Cno = pjb.Pno)
select * from Score join (select Cno as Pno,avg(Degree) as pj from Score group by Pno) as pjb on (Score.Cno = pjb.Pno)
where Degree < pj;

--第三十四题、 查询所有任课教师的Tname和Depart.

select * from Score group by Cno

select distinct Tname,Depart from Score join Course on Course.Cno = Score.Cno join Teacher on Teacher.Tno = Course.Tno where Course.Cno in
(select Cno from Score group by Cno)

--第三十五题、 查询所有未讲课的教师的Tname和Depart.
select Cno from Score group by Cno
select Tno from Course where Cno not in (select Cno from Score group by Cno)
select Tname,Depart from Teacher where Tno in (select Tno from Course where Cno not in (select Cno from Score group by Cno))

--第三十六题、查询至少有2名男生的班号。

select * from Student group by Class having count(Ssex) > 1 and Ssex = '男'
select Class from Student where Sno in (select Sno from Student group by Class having count(Ssex) > 1 and Ssex = '男')

--第三十七题、查询Student表中不姓“王”的同学记录。

select * from Student where Sname not in (select Sname from Student where Sname like '李%')

--第三十八题、查询Student表中不姓“王”的同学记录。

select sname from Student where Sname not like '王%';

-- 第三十九题、查询Student表中最大和最小的Sbirthday日期值。

select MAX(sbirthday) as '最大值',MIN(sbirthday)'最小值' from Student

-- 第四十题、以班号和年龄从大到小的顺序查询Student表中的全部记录。

select class,sbirthday from Student order by Class desc,Sbirthday asc

-- 第四十一题、查询“男”教师及其所上的课程。

select cname from course where tno in(select tno from teacher where tsex=’男’)

select tname,cname from teacher ,course where teacher.tno=course.tno and tsex='男'

-- 第四十二题、查询最高分同学的Sno、Cno和Degree列。

(1)select * from score where Degree = (select max(Degree) from score)

(2)select top 1 * from score order by degree desc   

-- 第四十三题、查询和“李军”同性别的所有同学的Sname.

select sname from Student where Ssex=(select Ssex from Student where Sname='李军')

-- 第四十四题、查询和“李军”同性别并同班的同学Sname.

select sname from Student where Ssex=(select Ssex from Student where Sname='李军')and class=(select Class from Student where Sname='李军');

-- 第四十五题、查询所有选修“计算机导论”课程的“男”同学的成绩表。

select Degree from score where Sno in(select Sno from student where Ssex='1') and Cno in (select Cno from course where Cname = '计算机导论');

MYSQL数据库45道练习题的更多相关文章

  1. MySQL查询 45道练习题

    SQL查询45道练习题 1.查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from student2.查询教师所有的单位即不重复 ...

  2. MySQL语句45道练习题及答案

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  3. 学生选课数据库SQL语句45道练习题整理及mysql常用函数(20161019)

    学生选课数据库SQL语句45道练习题: 一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四 ...

  4. MYSQL 45道练习题

    学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示.用S ...

  5. SQL 查找 45道练习题

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  6. 补充:MySQL经典45道题型

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher). 四个表的结构分别如表1-1的表(一)~表 ...

  7. mysql数据库的相关练习题及答案

    表结构示意图: 表结构创建语句: class表创建语句 create table ) not null)engine=innodb default charset=utf8; student表创建语句 ...

  8. 100道MySQL数据库经典面试题解析(收藏版)

    前言 100道MySQL数据库经典面试题解析,已经上传github啦 https://github.com/whx123/JavaHome/tree/master/Java面试题集结号 公众号:捡田螺 ...

  9. 【Python全栈-后端开发】MySQL数据库-练习题

    MySQL数据库-练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号 ...

随机推荐

  1. 转:【Java集合源码剖析】LinkedList源码剖析

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/35787253   您好,我正在参加CSDN博文大赛,如果您喜欢我的文章,希望您能帮我投一票 ...

  2. python--学校管理系统(只做了学校管理的接口)

    1.老师的要求是做学校管理.学生.老师的接口,本文档,只做了学校管理的接口 2.具体要求: 角色:学校.学员.课程.讲师 要求: 1. 创建北京.上海 2 所学校 2. 创建linux , pytho ...

  3. 集美大学网络1413第十五次作业成绩(团队十) -- 项目复审与事后分析(Beta版本)

    题目 团队作业10--项目复审与事后分析(Beta版本) 团队作业10成绩 --团队作业10-1 Beta事后诸葛亮  团队/分值 设想和目标 计划 资源 变更管理 设计/实现 测试/发布 团队的角色 ...

  4. GUI线程 :打字母游戏

    代码: /** * */ package com.niit.syntronized; import java.awt.Color; import java.awt.FlowLayout; import ...

  5. 201521123040 《Java程序设计》第6周学习总结

    1.本章学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 2. 书面作业 clone方法1.1 Object对象中的c ...

  6. 201521123076《java程序设计》第三周学习总结

    1. 本周学习总结 2.书面作业 Q1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...

  7. 201521123015 《Java程序设计》第2周学习总结

    1.本章学习总结 (1)学习了枚举,数组等方法 (2)通过实验内容的讲解,解决了一些问题 (3)进一步运用和了解码云 书面作业 1.使用Eclipse关联jdk源代码,并查看String对象的源代码( ...

  8. 201521123019 《Java程序设计》第9周学习总结

    1. 本章学习总结 2. 书面作业 一.题目5-1.常用异常 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避免? 答:经常会出现Ar ...

  9. java 程序编写规则(自己总结)

    1.命名规范 (1)所有的标示符都只能用ASCⅡ字母(A-Z或a-z).数字(0-9)和下划线"_". (2)类名是一个名词,采用大小写混合的方式,每个单词的首字母大写.例如:Us ...

  10. 多线程面试题系列(3):原子操作 Interlocked系列函数

    上一篇中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是否运行出错.这也非常类似于统计一个网站每天有多少用户登录,每个用户登录用一个线程模拟,线程运行时会将 ...