SQL语句练习题(二)

文章提供了一系列关于MySQL数据库的查询示例,包括查询课程及教师信息、统计学生性别比例、查找特定科目高分学生、计算平均成绩、分析选修课程情况等。此外,还涉及到了多表关联操作,如查询门派成员信息、统计未入派人员和无人选修的门派。最后,文章展示了如何处理课程选修记录,如删除特定教师课程的记录。
摘要由CSDN通过智能技术生成

题目

一、准备数据

创建表及插入记录

CREATE TABLE class (
cid int(11) NOT NULL AUTO_INCREMENT,
caption varchar(32) NOT NULL,
PRIMARY KEY (cid)
) ENGINE=InnoDB CHARSET=utf8;

INSERT INTO class VALUES
(1, ‘三年二班’),
(2, ‘三年三班’),
(3, ‘一年二班’),
(4, ‘二年九班’);

CREATE TABLE course(
cid int(11) NOT NULL AUTO_INCREMENT,
cname varchar(32) NOT NULL,
teacher_id int(11) NOT NULL,
PRIMARY KEY (cid),
KEY fk_course_teacher (teacher_id),
CONSTRAINT fk_course_teacher FOREIGN KEY (teacher_id) REFERENCES teacher (tid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO course VALUES
(1, ‘生物’, 1),
(2, ‘物理’, 2),
(3, ‘体育’, 3),
(4, ‘美术’, 2);

CREATE TABLE score (
sid int(11) NOT NULL AUTO_INCREMENT,
student_id int(11) NOT NULL,
course_id int(11) NOT NULL,
num int(11) NOT NULL,
PRIMARY KEY (sid),
KEY fk_score_student (student_id),
KEY fk_score_course (course_id),
CONSTRAINT fk_score_course FOREIGN KEY (course_id) REFERENCES course (cid),
CONSTRAINT fk_score_student FOREIGN KEY (student_id) REFERENCES student(sid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO score VALUES
(1, 1, 1, 10),
(2, 1, 2, 9),
(5, 1, 4, 66),
(6, 2, 1, 8),
(8, 2, 3, 68),
(9, 2, 4, 99),
(10, 3, 1, 77),
(11, 3, 2, 66),
(12, 3, 3, 87),
(13, 3, 4, 99),
(14, 4, 1, 79),
(15, 4, 2, 11),
(16, 4, 3, 67),
(17, 4, 4, 100),
(18, 5, 1, 79),
(19, 5, 2, 11),
(20, 5, 3, 67),
(21, 5, 4, 100),
(22, 6, 1, 9),
(23, 6, 2, 100),
(24, 6, 3, 67),
(25, 6, 4, 100),
(26, 7, 1, 9),
(27, 7, 2, 100),
(28, 7, 3, 67),
(29, 7, 4, 88),
(30, 8, 1, 9),
(31, 8, 2, 100),
(32, 8, 3, 67),
(33, 8, 4, 88),
(34, 9, 1, 91),
(35, 9, 2, 88),
(36, 9, 3, 67),
(37, 9, 4, 22),
(38, 10, 1, 90),
(39, 10, 2, 77),
(40, 10, 3, 43),
(41, 10, 4, 87),
(42, 11, 1, 90),
(43, 11, 2, 77),
(44, 11, 3, 43),
(45, 11, 4, 87),
(46, 12, 1, 90),
(47, 12, 2, 77),
(48, 12, 3, 43),
(49, 12, 4, 87),
(52, 13, 3, 87);

CREATE TABLE student(
sid int(11) NOT NULL AUTO_INCREMENT,
gender char(1) NOT NULL,
class_id int(11) NOT NULL,
sname varchar(32) NOT NULL,
PRIMARY KEY (sid),
KEY fk_class (class_id),
CONSTRAINT fk_class FOREIGN KEY (class_id) REFERENCES class (cid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO student VALUES
(1, ‘男’, 1, ‘理解’),
(2, ‘女’, 1, ‘钢蛋’),
(3, ‘男’, 1, ‘张三’),
(4, ‘男’, 1, ‘张一’),
(5, ‘女’, 1, ‘张二’),
(6, ‘男’, 1, ‘张四’),
(7, ‘女’, 2, ‘铁锤’),
(8, ‘男’, 2, ‘李三’),
(9, ‘男’, 2, ‘李一’),
(10, ‘女’, 2, ‘李二’),
(11, ‘男’, 2, ‘李四’),
(12, ‘女’, 3, ‘如花’),
(13, ‘男’, 3, ‘刘三’),
(14, ‘男’, 3, ‘刘一’),
(15, ‘女’, 3, ‘刘二’),
(16, ‘男’, 3, ‘刘四’);

CREATE TABLE teacher(
tid int(11) NOT NULL AUTO_INCREMENT,
tname varchar(32) NOT NULL,
PRIMARY KEY (tid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO teacher VALUES
(1, ‘张磊老师’),
(2, ‘李平老师’),
(3, ‘刘海燕老师’),
(4, ‘朱云海老师’),
(5, ‘李杰老师’);

1、查询所有的课程的名称以及对应的任课老师姓名

select course.cname,teacher.tname from teacher inner join course on course.teacher_id=teacher.tid;

2、查询学生表中男女生各有多少人

select gender,count(1) as 人数 from student group by gender;

3、查询物理成绩等于100的学生的姓名

select sname from student where sid in (select student_id from course inner JOIN score on score.course_id=(SELECT course.cid FROM course WHERE cname='物理') and score.num=100 GROUP BY student_id);

4、查询平均成绩大于八十分的同学的姓名和平均成绩

select sname,avg(num) from student inner join score on score.student_id=student.sid GROUP BY student_id HAVING avg(num)>80;

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

select student.sid,student.sname,count(1),sum(num) from student INNER JOIN score on student.sid=score.student_id GROUP BY student_id;

6、 查询姓李老师的个数

select count(1) from teacher WHERE tname like '李%';

7、 查询没有报李平老师课的学生姓名

SELECT sname from student WHERE sid not in (select student_id FROM score WHERE course_id IN(select cid from course where teacher_id=(select tid from teacher where tname='李平老师')));

8、 查询物理课程比生物课程高的学生的学号

select A.student_id from (SELECT * from score WHERE course_id = (SELECT cid from course WHERE cname='物理')) as A
INNER JOIN
(SELECT * from score WHERE course_id = (SELECT cid from course WHERE cname='生物')) as B
on A.student_id=B.student_id
WHERE A.num>B.num;

9、 查询没有同时选修物理课程和体育课程的学生姓名

SELECT sname from student WHERE sname not in(
SELECT sname from student WHERE sid in(
    SELECT student_id from score WHERE course_id=(SELECT cid from course WHERE cname='物理'))
        and sid in (SELECT student_id from score WHERE course_id=(SELECT cid from course WHERE cname='体育')));

10、查询挂科超过两门(包括两门)的学生姓名和班级

SELECT sname,class_id from student WHERE sid in (SELECT student_id from score WHERE num<60 GROUP BY student_id HAVING count(1)>=2);

11、查询选修了所有课程的学生姓名

SELECT sname from student WHERE sid in(select sid from score WHERE course_id in (select cid from course));

12、查询李平老师教的课程的所有成绩记录

SELECT * from score where course_id in (SELECT course_id from course where teacher_id=(SELECT tid from teacher where tname='李平老师'));

13、查询全部学生都选修了的课程号和课程名

select course_id from score GROUP BY course_id HAVING count(1)=(SELECT count(1) from student);

14、查询每门课程被选修的次数

select count(1),course_id from score GROUP BY course_id;

15、查询之选修了一门课程的学生姓名和学号

select student_id from score GROUP BY student_id HAVING count(1)=1;

16、查询所有学生考出的成绩并按从高到低排序(成绩去重)

SELECT num from score GROUP BY num ORDER BY num desc;

17、查询平均成绩大于85的学生姓名和平均成绩

SELECT student.sname,avg(num) from score INNER JOIN student on student.sid = score.student_id GROUP BY student_id HAVING avg(num)>80;

18、查询生物成绩不及格的学生姓名和对应生物分数

SELECT A.num,B.sname from (SELECT * from score WHERE course_id=(SELECT cid from course WHERE cname='生物') 
GROUP BY student_id HAVING num<60) as A INNER JOIN (SELECT * from student) as B on A.student_id=B.sid;

19、查询在所有选修了李平老师课程的学生中,这些课程(李平老师的课程,不是所有课程)平均成绩最高的学生姓名

SELECT sname from student where sid=(SELECT student_id from score WHERE course_id in (SELECT cid from course where teacher_id=(SELECT tid from teacher WHERE tname='李平老师')) GROUP BY student_id ORDER BY avg(num) desc limit 1);

进阶练习

# 进阶练习一:

CREATE TABLE t_dept (
id INT(11) NOT NULL AUTO_INCREMENT,
deptName VARCHAR(30) DEFAULT NULL,
address VARCHAR(40) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

CREATE TABLE t_emp (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(20) DEFAULT NULL,
age INT(3) DEFAULT NULL,
deptId INT(11) DEFAULT NULL,
empno int not null,
PRIMARY KEY (id),
KEY idx_dept_id (deptId)
#CONSTRAINT fk_dept_id FOREIGN KEY (deptId) REFERENCES t_dept (id)
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

INSERT INTO t_dept(deptName,address) VALUES(‘华山’,‘华山’);
INSERT INTO t_dept(deptName,address) VALUES(‘丐帮’,‘洛阳’);
INSERT INTO t_dept(deptName,address) VALUES(‘峨眉’,‘峨眉山’);
INSERT INTO t_dept(deptName,address) VALUES(‘武当’,‘武当山’);
INSERT INTO t_dept(deptName,address) VALUES(‘明教’,‘光明顶’);
INSERT INTO t_dept(deptName,address) VALUES(‘少林’,‘少林寺’);

INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘风清扬’,90,1,100001);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘岳不群’,50,1,100002);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘令狐冲’,24,1,100003);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘洪七公’,70,2,100004);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘乔峰’,35,2,100005);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘灭绝师太’,70,3,100006);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘周芷若’,20,3,100007);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘张三丰’,100,4,100008);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘张无忌’,25,5,100009);
INSERT INTO t_emp(NAME,age,deptId,empno) VALUES(‘韦小宝’,18,null,100010);

ALTER TABLE t_dept
add CEO INT(11) ;

CEO=2 值,都应该是t_emp 中id的值。

update t_dept set CEO=2 where id=1;
update t_dept set CEO=4 where id=2;
update t_dept set CEO=6 where id=3;
update t_dept set CEO=8 where id=4;
update t_dept set CEO=9 where id=5;
1 所有有门派的人员信息

SELECT e.`name`,d.`deptName` FROM t_emp e INNER JOIN t_dept d ON e.`deptId`=d.`id`;

2 列出所有用户,并显示其机构信息

SELECT e.`name`,d.`deptName` FROM t_emp e LEFT JOIN t_dept d ON e.`deptId`=d.`id`;

3 列出不入派的人员:

SELECT * FROM t_emp WHERE deptId IS NULL;

4 所有没人入的门派 :

SELECT d.* FROM  t_dept d LEFT JOIN t_emp e ON d.`id`=e.`deptId` WHERE e.`deptId` IS NULL;

5 列出所有人员和门派的对照关系

SELECT * FROM t_emp e LEFT JOIN t_dept d ON e.`deptId`=d.`id`
UNION
SELECT * FROM t_emp e RIGHT JOIN t_dept d ON e.`deptId`=d.`id`;

6 列出所有没入派的人员和没人入的门派

SELECT * FROM t_emp e  LEFT JOIN t_dept d ON e.`deptId`=d.`id` WHERE e.deptId IS NULL
UNION
SELECT * FROM  t_dept d LEFT JOIN t_emp e ON d.`id`=e.`deptId` WHERE e.`deptId` IS NULL;

7 求各个门派对应的掌门人名称:

SELECT d.deptName,e.name FROM t_dept d LEFT JOIN t_emp e ON d.ceo=e.id

8 求所有当上掌门人的平均年龄:

SELECT AVG(e.age) FROM t_dept d LEFT JOIN t_emp e ON d.ceo=e.id

9 求所有人物对应的掌门名称:

SELECT ed.name '人物',c.name '掌门' FROM

(SELECT e.name,d.ceo from t_emp e LEFT JOIN t_dept d on e.deptid=d.id) ed

 LEFT JOIN t_emp c on ed.ceo= c.id;

进阶练习二:

CREATE TABLE course (
cid varchar(50) NOT NULL,
cname varchar(50) NOT NULL,
tid int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


– Records of course


INSERT INTO course VALUES (‘001’, ‘PHP’, ‘1’);
INSERT INTO course VALUES (‘002’, ‘C’, ‘1’);
INSERT INTO course VALUES (‘003’, ‘C++’, ‘2’);
INSERT INTO course VALUES (‘004’, ‘JAVA’, ‘3’);
INSERT INTO course VALUES (‘005’, ‘python’, ‘4’);
INSERT INTO course VALUES (‘006’, ‘R’, ‘5’);
INSERT INTO course VALUES (‘007’, ‘HTML’, ‘6’);

CREATE TABLE sc (
sid int(10) NOT NULL,
cid varchar(50) NOT NULL,
score int(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


– Records of sc


INSERT INTO sc VALUES (‘1001’, ‘001’, ‘89’);
INSERT INTO sc VALUES (‘1002’, ‘001’, ‘80’);
INSERT INTO sc VALUES (‘1003’, ‘001’, ‘30’);
INSERT INTO sc VALUES (‘1004’, ‘001’, ‘78’);
INSERT INTO sc VALUES (‘1005’, ‘001’, ‘68’);
INSERT INTO sc VALUES (‘1006’, ‘001’, ‘93’);
INSERT INTO sc VALUES (‘1007’, ‘001’, ‘62’);
INSERT INTO sc VALUES (‘1001’, ‘002’, ‘67’);
INSERT INTO sc VALUES (‘1002’, ‘002’, ‘86’);
INSERT INTO sc VALUES (‘1003’, ‘002’, ‘67’);
INSERT INTO sc VALUES (‘1004’, ‘002’, ‘77’);
INSERT INTO sc VALUES (‘1006’, ‘002’, ‘84’);
INSERT INTO sc VALUES (‘1007’, ‘002’, ‘72’);
INSERT INTO sc VALUES (‘1001’, ‘003’, ‘82’);
INSERT INTO sc VALUES (‘1002’, ‘003’, ‘85’);
INSERT INTO sc VALUES (‘1003’, ‘003’, ‘32’);
INSERT INTO sc VALUES (‘1004’, ‘003’, ‘73’);
INSERT INTO sc VALUES (‘1005’, ‘003’, ‘64’);
INSERT INTO sc VALUES (‘1006’, ‘003’, ‘87’);
INSERT INTO sc VALUES (‘1007’, ‘003’, ‘77’);
INSERT INTO sc VALUES (‘1001’, ‘004’, ‘39’);
INSERT INTO sc VALUES (‘1003’, ‘004’, ‘80’);
INSERT INTO sc VALUES (‘1004’, ‘004’, ‘88’);
INSERT INTO sc VALUES (‘1005’, ‘004’, ‘68’);
INSERT INTO sc VALUES (‘1006’, ‘004’, ‘59’);
INSERT INTO sc VALUES (‘1007’, ‘004’, ‘42’);
INSERT INTO sc VALUES (‘1008’, ‘004’, ‘64’);
INSERT INTO sc VALUES (‘1001’, ‘005’, ‘89’);
INSERT INTO sc VALUES (‘1002’, ‘005’, ‘70’);
INSERT INTO sc VALUES (‘1003’, ‘005’, ‘60’);
INSERT INTO sc VALUES (‘1004’, ‘005’, ‘58’);
INSERT INTO sc VALUES (‘1005’, ‘005’, ‘38’);
INSERT INTO sc VALUES (‘1006’, ‘005’, ‘89’);
INSERT INTO sc VALUES (‘1007’, ‘005’, ‘72’);
INSERT INTO sc VALUES (‘1008’, ‘005’, ‘64’);
INSERT INTO sc VALUES (‘1001’, ‘006’, ‘49’);
INSERT INTO sc VALUES (‘1002’, ‘006’, ‘90’);
INSERT INTO sc VALUES (‘1003’, ‘006’, ‘70’);
INSERT INTO sc VALUES (‘1004’, ‘006’, ‘48’);
INSERT INTO sc VALUES (‘1005’, ‘006’, ‘98’);
INSERT INTO sc VALUES (‘1006’, ‘006’, ‘59’);
INSERT INTO sc VALUES (‘1007’, ‘006’, ‘72’);
INSERT INTO sc VALUES (‘1008’, ‘006’, ‘74’);
INSERT INTO sc VALUES (‘1001’, ‘007’, ‘49’);
INSERT INTO sc VALUES (‘1002’, ‘007’, ‘50’);
INSERT INTO sc VALUES (‘1003’, ‘007’, ‘70’);
INSERT INTO sc VALUES (‘1004’, ‘007’, ‘88’);
INSERT INTO sc VALUES (‘1005’, ‘007’, ‘78’);
INSERT INTO sc VALUES (‘1006’, ‘007’, ‘99’);

CREATE TABLE student (
sid int(10) NOT NULL,
sname varchar(50) NOT NULL,
age int(10) NOT NULL,
sex varchar(10) NOT NULL,
PRIMARY KEY (sid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


– Records of student


INSERT INTO student VALUES (‘1001’, ‘张宇’, ‘12’, ‘男’);
INSERT INTO student VALUES (‘1002’, ‘张梅’, ‘18’, ‘女’);
INSERT INTO student VALUES (‘1003’, ‘王鑫’, ‘16’, ‘男’);
INSERT INTO student VALUES (‘1004’, ‘马东东’, ‘13’, ‘女’);
INSERT INTO student VALUES (‘1005’, ‘孙子涵’, ‘12’, ‘男’);
INSERT INTO student VALUES (‘1006’, ‘钱一’, ‘12’, ‘男’);
INSERT INTO student VALUES (‘1007’, ‘赵贺’, ‘15’, ‘男’);
INSERT INTO student VALUES (‘1008’, ‘周雪’, ‘16’, ‘女’);

CREATE TABLE teacher (
tid int(10) NOT NULL,
tname varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


– Records of teacher


INSERT INTO teacher VALUES (‘1’, ‘李雷’);
INSERT INTO teacher VALUES (‘2’, ‘李纯’);
INSERT INTO teacher VALUES (‘3’, ‘胡悦悦’);
INSERT INTO teacher VALUES (‘4’, ‘朱清时’);
INSERT INTO teacher VALUES (‘5’, ‘赛鸿飞’);
INSERT INTO teacher VALUES (‘6’, ‘宋三东’);
1、查询平均成绩大于70分

select sid,avg(score) from sc
group by sid
having avg(score)>70

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

select * from student;
select * from sc;

select s1.sid,s1.sname,count(s2.sid),sum(s2.score)
from student s1
inner join sc s2
on s1.sid=s2.sid
group by s2.sid


select sid,count(*) from sc
group by sid;

– 查询姓“李”的老师的个数

select count(1) from teacher
where tname like '李%'

– 查询学过“李纯”老师课的同学的学号、姓名

select * from student;
select * from course;
select * from sc;
select * from teacher;

连接查询:

select a.sid,a.sname
from student a,course b,sc c,teacher d 
where a.sid=c.sid
and b.cid=c.cid
and b.tid=d.tid
and d.tname='李纯'

子查询:

1、查询“李纯”老师tid
select tid from teacher where tname='李纯'

2、查询“李纯”老师的cid

select cid from course
where tid = (select tid from teacher where tname='李纯')

3、通过sc查询学过“李纯”老师课程的sid

select sid from sc
where cid in (
select cid from course
where tid = (select tid from teacher where tname='李纯')
)

4、查询学生信息

select sid,sname from student
where sid in (
select sid from sc
where cid in (
select cid from course
where tid = (select tid from teacher where tname='李纯')
)
)

– 查询没学过“李雷”老师课的同学的学号、姓名
1、查出李雷的tid

select tid from teacher where tname='李雷'

2、查询李雷教的课程cid

select cid from course
where tid=(select tid from teacher where tname='李雷')

3、查询学过李雷课程的sid

select distinct sid from sc
where cid in (
select cid from course
where tid=(select tid from teacher where tname='李雷')
)

4、没学过李雷课程的同学

SELECT * FROM student WHERE sid NOT IN (
	SELECT DISTINCT sid FROM sc WHERE cid IN ( SELECT cid FROM course WHERE tid =( SELECT tid FROM teacher WHERE tname = '李雷' ) ) 
	)

– 查询学过“001”并且也学过编号“002”课程的同学的学号、姓名

select * from sc;
select * from student;

1、使用子查询

select s.sid,s.sname from student s
where s.sid in (
select t1.sid from 
        (select * from sc where cid='001') t1,
        (select * from sc where cid='002') t2
where t1.sid = t2.sid)

2、使用连接查询

select s.sid,s.sname from student s
,sc t1,sc t2
where s.sid=t1.sid
and t1.cid='001'
and t2.cid='002'
and t1.sid=t2.sid

– 查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名
课程001和002的成绩

select s.sid,s.sname from student s
where s.sid in (
select t1.sid from 
        (select * from sc where cid='001') t1,
        (select * from sc where cid='002') t2
where t1.sid = t2.sid and t2.score<t1.score)
            

select s.sid,s.sname from student s
,sc t1,sc t2
where s.sid=t1.sid
and t1.cid='001'
and t2.cid='002'
and t1.sid=t2.sid
and t2.score<t1.score

– 查询学过“李雷”老师所教的所有课的同学的学号、姓名
1、查询李雷老师的tid

select tid from teacher where tname='李雷'

2、查询李雷老师的cid

select cid from course
where tid in (select tid from teacher where tname='李雷')

3、查询学习过李雷老师的学生sid

select distinct sid from sc
where cid in (
select cid from course
where tid in (select tid from teacher where tname='李雷')
)

4、查询学过李雷老师的学生的学号姓名

select sid,sname from student
where sid in (
select distinct sid from sc
where cid in (
select cid from course
where tid in (select tid from teacher where tname='李雷')
)
)

– 查询各科成绩最高和最低的分:以如下形式显示:课程ID,课程名最高分,最低分
1、按cid分组,查询各科的最高和最低分

select cid,max(score),min(score) from sc
group by cid

2、1的结果和course连接查询

select a.cid,a.cname,b.max,b.min from course a
inner join (select cid,max(score) max,min(score) min from sc
group by cid) b
on a.cid=b.cid

– 查询没有学全所有课的同学的学号、姓名

1、统计课程的个数

select count(1) from course

2、按sid分组,统计每个学生学习了多少课程

select sid,count(1) from sc group by sid;

3、找出没学全部课程的sid

select sid from sc group by sid
having count(1)< (select count(1) from course)

4、根据3的结果输出学生信息

select sid,sname from student
where sid in (
select sid from sc group by sid
having count(1)< (select count(1) from course)
)

– 查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名
1、找出学号为1001的同学学习的课程cid

select cid from sc
where sid=1001

2、找出至少学习过1结果中的任意一门课程的学生的sid

select distinct sid from sc
where cid in (
select cid from sc
where sid=1001
) and sid!=1001

3、根据2的结果输出学生信息

select sid,sname from student 
where sid in(
select distinct sid from sc
where cid in (
select cid from sc
where sid=1001
) and sid!=1001
)

– 按各科平均成绩从低到高和及格率的百分数从高到低顺序。补充:concat()
– 方法用于连接两个或多个数组,百分数表示方法 CONCAT(值1/值2*100,‘%’)
计算各科的及格率:及格人数/总人数

计算各科及格人数:

select cid cid2,count(1) c2 from sc
where score>=60 group by cid

select s1.cid,avg(s1.score) avg_sc,concat(t.c2/count(s1.score)*100,'%') rate
from sc s1
left join (
select cid cid2,count(1) c2 from sc
where score>=60 group by cid
) t
on s1.cid = t.cid2
group by s1.cid
order by avg_sc,rate desc

– 删除学习“李纯”老师课的SC表记录
1、“李纯”老师的cid

select cid from teacher t,course c where t.tid=c.tid and t.tname = '李纯'

2、根据cid删除SC表记录

delete from sc
where cid in (
select cid from teacher t,course c where t.tid=c.tid and t.tname = '李纯'
)

mysql练习题(单表多表查询)

表1 Employee表

create table Employee(
num int primary key auto_increment,
name varchar(30) not null,
addr varchar(30) not null unique,
zip varchar(30) not null,
tel varchar(30) not null,
email varchar(30) unique,
depno int not null,
birth date not null,
sex set (“男”,“女”)
)

表2 Department

create table department(
depno int primary key auto_increment,
depName varchar(30) unique not null,
remark varchar(50)
)

表3 salary

create table salay(
num int primary key auto_increment,
inCome double not null,
outCome double not null
)

alter table Employee add constraint fk_Employee_department foreign key(depno) references department(depno);

往表里添加数据:

insert into employee VALUES
(1,“王林”,“武汉大学”,“430074”,“87598405”,null,2,“1985-2-1”,“男”),
(2,"王芳 ",“华中科大”,“430073”,“62534231”,null,1,“1966-3-28”,“男”),
(3,“张晓”,“武汉理工大”,“430072 “,“87596985”,Null,1,“1972-12-9”,“男”),
(4,“王小燕”,“武汉交大”,“430071”,“85743261”,“lili@sina.com”,1 ,“1950-7-30”,“女”),
(5,“李华”,” 华中农大”,“430070”,“87569865”,Null,5,“1962-10-18”,“男”),
(6,“李明”,“华中师大”,“430075”,“85362143”,"zhujun@sina.com ",5,“1955-09-28”,“男”),
(7,“田丽”,“中南财大”,“430076”,“85693265”,“zgming@sohu.com”,3,“1968-08-10”,“女”),
(8,“吴天”,“武汉电力”,“430077”,“36985612 “,“zjamg@china.com”,5,“1964-10-01”,“男”),
(9,“刘备”,” 武汉邮科院”,“430078”,“69865231”,Null,3,“1967-04-02”,“男”),
(10,“赵云”,“学府家园”,“430071”,“68592312 “,Null,4,“1968-11-18”,“男”),
(11,“貂禅”,“湖北工大”,” 430074”,“65987654”, null,4,“1959-09-03”,“女”);

insert INTO department(depName,remark) values (“财务部”,Null),(“人力资源部”,Null),(“经理办公室”,Null),
(“研发部”,Null),(“市场部”,Null);

INSERT INTO salay(inCome,outCome) values (2100.7,123.09),(1582.62,88.03),(2569.88,185.65),(1987.01 ,79.58),
(2066.15 ,108.0),( 2980.7, 210.2),(3259.98 ,281.52),(2860.0,198),(2347.68,180),(2531.98,199.08),(2240.0,121.0);

练习1:SELECT语句的基本使用

  1. 查询每个雇员的所有记录;
SELECT *from employee;
  1. 查询前5个会员的所有记录;
SELECT *from employee LIMIT 0,5;
  1. 查询每个雇员的地址和电话;
SELECT addr,tel from employee;
  1. 查询num为001的雇员地址和电话;
SELECT addr,tel FROM employee WHERE num=001;
  1. 查询表Employee表中女雇员的地址和电话,使用AS子句将结果列中各列的标题分别指定为地址、电话;
SELECT addr AS 地址,tel AS 电话 FROM employee WHERE sex="女";
  1. 计算每个雇员的实际收入;
SELECT num,income-outcome from salay;
  1. 找出所有性王的雇员的部门号(部门号不能重复显示);
SELECT  DISTINCT depno  from employee WHERE name LIKE"王%";
  1. 找出所有收入在2000-3000元之间的雇员编号
SELECT num FROM salay WHERE income BETWEEN 2000 AND 3000;

练习2:子查询的使用(答案可以不唯一)

  1. 查找在财务部工作的雇员情况;
select *from employee WHERE depno=(SELECT depno FROM department WHERE depName="财务部");
  1. 查找在财务部且年龄不低于研发部任一个雇员年龄的雇员的姓名;
SELECT name from employee WHERE depno=
(SELECT depno from department WHERE depName="财务部") AND birth<ALL
(SELECT birth FROM employee WHERE depno=(SELECT depno FROM department WHERE depName="研发部"));
  1. 查找比所有财务部雇员收入都高的雇员的姓名;
SELECT DISTINCT income,name from employee,salay,department WHERE employee.num=
salay.num AND income>all
(SELECT income FROM employee,salay,department WHERE employee.num=salay.num AND employee.depno=department.depno and depName="财务部");

练习3:连接查询的使用

  1. 查找每个雇员的情况及薪水情况;
SELECT * FROM employee ,salay WHERE employee.num=salay.num;
SELECT e.*, s.* FROM employee e
INNER JOIN salay s
ON e.Num=s.Num;
  1. 查找财务部收入在2200元以上的雇员姓名及其薪水详细情况;
SELECT name,salay.* FROM employee,salay,department WHERE employee.num=salay.num AND employee.depno=department.depno AND depName="财务部"AND income>2200;

练习4:数据汇总

  1. 求财务部雇员的平均实际收入;
SELECT avg(income-outcome) FROM employee,salay,department WHERE employee.num=salay.num AND employee.depno=department.depno and depName="财务部";
  1. 求财务部雇员的总人数;
SELECT count(*) FROM employee,salay,department WHERE employee.num=salay.num AND employee.depno=department.depno and depName="财务部";

练习5:GROUP BY 、ORDER BY 子句的使用

  1. 求各部门的雇员数(要求显示,部门号、部门名称和部门雇员数);
SELECT e.depno AS 部门号,d.depname AS 部门名称,count(*)AS 部门雇员数 FROM employee e INNER JOIN department d on e.depno=d.depno  GROUP BY  d.depno ;
  1. 求部门的平均薪水大于2500的部门信息(要求显示,部门号、部门名称和平均工资)
SELECT e.depno AS 部门号,d.depname AS 部门名称,avg(income)AS 部门平均工资 FROM employee e,department d,salay s  GROUP BY  d.depno,e.num ;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值