mysql练习题

简单点的

数据准备

create database mysqltest; – 创建数据库
use mysqltest; – 使用数据库

#创建几个库表

-- 学生表
create table Student   (
Sno char(3) NOT NULL Primary key ,    -- 学号 ,设为主键,不允许空值   
Sname char(8) NOT NULL,        -- 学生姓名
Ssex char(2)NOT NULL,        -- 学生性别
Sbirthday datetime,     -- 学生出生年月
Class char(5)         -- 学生所在班级
);
-- 教师表
create table Teacher        (
Tno char(3)NOT NULL primary key,        -- 教工编号设为主键
Tname char(4)NOT NULL,        -- 教工姓名
Tsex char(2)NOT NULL,        -- 教工性别
Tbirthday datetime,        -- 教工出生年月
Prof char(6),        -- 职称
Depart varchar(10)NOT NULL        -- 教工所在部门
)
   -- 课程表
create table Course     (
    Cno char(5) NOT NULL Primary key ,        -- 课程号设为主键
    Cname varchar(10) NOT NULL,        -- 课程名称
    Tno char(3) NOT NULL references Teacher(Tno)        -- 教工编号设为外键
)
create table Score    -- 成绩表
(
Sno char(3) NOT NULL references Student(Sno),    -- 学号设为外码
Cno char(5) NOT NULL references Course(Cno),    -- 课程号设为外码
Degree Decimal(4,1),    -- 成绩
primary key(Sno,Cno)    -- 学号和课程号设为联合主键
)

数据准备

insert into Student values(108,'曾华','男','1977-09-01','95033');
insert into Student values(105,'匡明','男','1975-10-02','95031');
insert into Student values(107,'王丽','女','1976-01-23','95033');
insert into Student values(101,'李军','男','1976-02-20','95033');
insert into Student values(109,'王芳','女','1975-02-10','95031');
insert into Student values(103,'陆君','男','1974-06-03','95031');

insert into Teacher values(804,'李诚','男','1958-12-02','副教授','计算机系');
insert into Teacher values(856,'张旭','男','1969-03-12','讲师','电子工程系');
insert into Teacher values(825,'王萍','女','1972-05-05','助教','计算机系') ;
insert into Teacher values(831,'刘冰','女','1977-08-14','助教','电子工程系');

insert into Course values('3-105','计算机导论',825);
insert into Course values('3-245','操作系统',804);
insert into Course values('6-166','数字电路',856);
insert into Course values('9-888','高等数学',831);

insert into Score values(103,'3-245',86);
insert into Score values(105,'3-245',75);
insert into Score values(109,'3-245',68);
insert into Score values(103,'3-105',92);
insert into Score values(105,'3-105',88);
insert into Score values(109,'3-105',76);
insert into Score values(101,'3-105',64);
insert into Score values(107,'3-105',91);
insert into Score values(108,'3-105',78);
insert into Score values(101,'6-166',85);
insert into Score values(107,'6-166',79);
insert into Score values(108,'6-166',81);

题目准备

– 1、查询Student表中的所有记录的Sname、Ssex和Class列。

SELECT sname,ssex,class FROM Student; 

– 2、 查询教师所有的单位即不重复的Depart列

SELECT DISTINCT Depart ,Prof FROM Teacher;

– 3、 查询Student表的所有记录。

SELECT * FROM Student;

– 4、 查询Score表中成绩在60到80之间的所有记录

SELECT * FROM score WHERE degree BETWEEN 60 AND 80;

– 5、 查询Score表中成绩为85,86或88的记录。

SELECT * FROM score WHERE degree IN (85,86,88);

– 6、 查询Student表中“95031”班或性别为“女”的同学记录。

SELECT * FROM Student WHERE Class=95301 OR Ssex='女';

– 7、 以Class降序查询Student表的所有记录。

SELECT * FROM Student ORDER BY class DESC;

– 8、 以Cno升序、Degree降序查询Score表的所有记录。

SELECT * FROM Score ORDER BY cno ASC ,Degree DESC;

– 9、 查询“95031”班的学生人数。

SELECT COUNT(Class) FROM Student WHERE Class=95031;

– 10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

SELECT * FROM score WHERE degree IN (SELECT MAX(degree) FROM score GROUP BY cno);

– 11、 查询每门课的平均成绩。

SELECT cno ,AVG(degree) FROM score GROUP BY cno;

– 12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

SELECT AVG(Degree) FROM Score WHERE cno IN(SELECT cno FROM Score GROUP BY cno HAVING COUNT(*)>5 AND cno LIKE '3%');

– 13、查询分数大于70,小于90的Sno列。

SELECT sno FROM Score WHERE Degree BETWEEN 70 AND 90;

– 14、查询所有学生的Sname、Cno和Degree列。

SELECT s.sname,score.`Cno`,score.`Degree` FROM Student AS s LEFT JOIN Score AS Score ON Score.`Sno` = s.sno;

– 15、查询所有学生的Sno、Cname和Degree列。

SELECT c.cname,s.sno,s.degree FROM Course c LEFT JOIN Score s ON s.Cno=c.Cno;

– 16、查询出成绩不为空的cname

SELECT c.cname FROM Course c WHERE c.cno IN(SELECT c.cno FROM Course c LEFT JOIN Score s ON s.cno=c.cno WHERE s.degree IS NOT NULL);

– 17、 查询“95033”班学生的平均分。

SELECT AVG(Score.degree) FROM Student LEFT JOIN Score ON Student.Sno=score.Sno WHERE Student.class='95033';

–18、查询“张旭“教师任课的学生成绩。

 SELECT Score.degree FROM Course LEFT JOIN Score ON score.Cno=Course.cno WHERE tno=(SELECT tno FROM Teacher WHERE tname='张旭');

–19、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

SELECT sno,sname,Sbirthday FROM student WHERE YEAR(Sbirthday)=(
SELECT YEAR(Sbirthday) FROM student WHERE sno=108
);

–20、查询选修某课程的同学人数多于5人的教师姓名。

SELECT tname FROM teacher WHERE tno=(SELECT tno FROM course WHERE cno=(
SELECT cno FROM score GROUP BY cno HAVING COUNT(*)>5
));

–21、查询95033班和95031班全体学生的记录。

SELECT * FROM student WHERE class=95033 UNION SELECT * FROM student WHERE class=95031;

–22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列

SELECT sno,sname,sbirthday FROM student WHERE YEAR(sbirthday) IN (SELECT YEAR(Sbirthday) FROM student WHERE sno = 108);  

–23、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录

SELECT * FROM SCORE WHERE degree >= (SELECT degree FROM score WHERE sno= 109 AND cno= '3-105');

–24、查询出“计算机系”教师所教课程的成绩表。Teacher course score

SELECT * FROM score  WHERE cno IN (SELECT cno FROM course WHERE tno IN 	(SELECT tno FROM Teacher WHERE depart = '计算机系') ); 

–25、查询出学生的姓名sname还有授课老师的tname增加一个等级 60-75是及格 75-85是良 85就是优 否则不及格

SELECT stu.sname ,t.`Tname`, 
    CASE 
    WHEN s.`Degree`<75.0 AND s.`Degree`>=60.0 THEN
    '及格'
    WHEN s.`Degree`>=75.0 AND s.`Degree`<85.0 THEN
    '良'
    WHEN s.`Degree`>85.0 THEN
    '忧'
    ELSE '不及格'
    END '等级'
FROM student stu
INNER JOIN score s
    ON s.`Sno` = stu.`Sno`
INNER JOIN course c
    ON c.cno = s.`Cno`
INNER JOIN teacher t
    ON t.tno=c.tno ;

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

SELECT s.cno,s.sno,s.degree FROM score s WHERE s.cno = '3-105' AND degree > 	ANY(SELECT degree FROM score WHERE cno = '3-245' )  ORDER BY s.degree DESC;

–27、查询至少有2名男生的班号

SELECT class FROM student WHERE  ssex = '男' GROUP BY class HAVING 	COUNT(*)>=2;

–28、查询所有教师和同学的name、sex和birthday.

SELECT stu.sname,stu.ssex,stu.`Sbirthday` FROM student stu  
UNION ALL
SELECT tea.tname,tea.tsex,tea.Tbirthday FROM teacher tea

–29、查询Student表中每个学生的姓名和年龄。

SELECT sname,YEAR(NOW())-YEAR(Sbirthday) AS age FROM student;

–30、查询Student表中不姓“王”的同学记录。

SELECT * FROM student WHERE Sname NOT LIKE '王%'; 

复杂点的

数据准备

表名和字段

–1.学生表
Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别
Create table student(
s_id varchar(20),
s_name varchar(20),
s_birth datetime,
s_sex char(5)
);
–2.课程表
Course(c_id,c_name,t_id) – --课程编号, 课程名称, 教师编号
Create table course(
c_id varchar(20),
c_name varchar(20),
t_id varchar(20)
);
–3.教师表
Teacher(t_id,t_name) --教师编号,教师姓名
Create table teacher(
t_id varchar(20),
t_name varchar(20)
);
–4.成绩表
Score(s_id,c_id,s_score) --学生编号,课程编号,分数
Create table score(
s_id varchar(20),
c_id varchar(20),
s_score int(5)
);

测试数据

--插入学生表测试数据
insert into Student values('01' , '赵雷' , '1990-01-01' , '男');
insert into Student values('02' , '钱电' , '1990-12-21' , '男');
insert into Student values('03' , '孙风' , '1990-05-20' , '男');
insert into Student values('04' , '李云' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吴兰' , '1992-03-01' , '女');
insert into Student values('07' , '郑竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
--课程表测试数据
insert into Course values('01' , '语文' , '02');
insert into Course values('02' , '数学' , '01');
insert into Course values('03' , '英语' , '03');
--教师表测试数据
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
--成绩表测试数据
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

题目准备

1、 查询"01"课程比"02"课程成绩高的学生的信息及课程分数

SELECT * FROM (SELECT * from	score WHERE c_id='01') AS b1,
(SELECT * from	score WHERE c_id='02') AS b2
WHERE b1.s_score>b2.s_score AND b1.s_id=b2.s_id

2、 查询"01"课程比"02"课程成绩低的学生的信息及课程分数

SELECT * FROM (SELECT * from	score WHERE c_id='01') AS b1,
(SELECT * from	score WHERE c_id='02') AS b2
WHERE b1.s_score<b2.s_score AND b1.s_id=b2.s_id

3、 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

select t1.s_id,t2.s_name,avg(t1.s_score) as avg_score
from score t1left join student t2 on t1.s_id = t2.s_id
group by t1.s_id having avg(t1.s_score) >= 60

4、 查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

select t1.s_id,t2.s_name,avg(t1.s_score) as avg_score
from score t1left join student t2 on t1.s_id = t2.s_id
group by t1.s_id having avg(t1.s_score) <= 60

5、 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

select t2.s_id, t2.s_name,count(t1.c_id) as sun_course,sum(t1.s_score) as sum_score
from student t2 left join score t1 on
t1.s_id = t2.s_id group by t1.s_id

6、 查询"李"姓老师的数量

select count(s_name) from teacher where t_name like’ 李%’;

7、 查询学过"张三"老师授课的同学的信息

select s_id,s_name,s_sex from student
where s_id 
in(select s_id from score where c_id=(SELECT DISTINCT c_id FROM course c JOIN teacher t ON c.t_id=(SELECT t_id FROM teacher WHERE t_name='张三')));

8、 查询没学过"张三"老师讲授的任一门课程的学生姓名

select s_id,s_name,s_sex from student
where s_id
not in(select s_id from score where c_id=(SELECT DISTINCT c_id FROM course c JOIN teacher t ON c.t_id=(SELECT t_id FROM teacher WHERE t_name='张三')));

9、 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

select b.s_name,a.s_id,count(a.s_id),avg(a.s_score) from score a join student b 
on a.s_id=b.s_id where s_score<60 group by a.s_id

10平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select s.*,a.ss from student s join (
select s_id,avg(s_score) ss from score group by s_id) a on 
a.s_id=s.s_id order by a.ss desc;

加油吧骚年!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值