数据库(Mysql)----mysql查询练习(2、实操)
– 1.查询student表中所有的记录SELECT * FROM student;
– 2.查询student表中所有记录的s_name,s_sex和s_class列
SELECT s_no,s_name,s_class FROM student;
– 3.查询教师所有的单位但是不重复的t_depart列
SELECT distinct (t_depart) FROM teacher;
– 4.查询score表中成绩在60-80之间所有的记录(sc_degree)
SELECT * FROM score WHERE sc_degree BETWEEN 61 AND 79;
SELECT * FROM score WHERE sc_degree < 80 AND sc_degree > 60 ;
– 5.查询score表中成绩为85, 86, 或者88的记录(sc_degree)
SELECT * FROM score WHERE sc_degree IN(85, 86, 88);
– 6.查询student表中’95031’班或者性别为’女’的同学记录
SELECT * FROM student WHERE s_class = '95031' OR s_sex = '女';
– 7.以class降序查询student表中所有的记录
SELECT * FROM student ORDER BY s_class desc;
– 8.以c_no升序.sc_degree降序插叙score表中所有的数据先以c_no进行升序,若c_no相同,则以sc_degree降序
SELECT * FROM score ORDER BY c_no ASC,sc_degree DESC;
– 9.查询’95031’班的学生人数
SELECT COUNT(s_no) FROM student WHERE s_class = '95031';
–10.查询score表中的最高分数的学生号和课程号.(子查询或者排序)
子查询:
--查询步骤
1.先找到最高分
最高分: SELECT MAX(sc_degree) FROM score;
2.通过我们找到的最高分的分数来从score中找到我们需要的学生号和课程号
SELECT c_no, s_no FROM score WHERE sc_degree = (最高分)
最终:
SELECT c_no, s_no FROM score WHERE sc_degree = (SELECT MAX(sc_degree) FROM score);
排序:
排序 最高分有多个的情况下可能有数据问题
limit x,y (x:表示从X条数据开始 y:需要查出多少条)
SELECT c_no, s_no FROM score ORDER BY sc_degree DESC LIMIT 0,1;这个OK
但是我们再插入一条数据:
INSERT INTO score VALUES('101','9-888','92');
再用排序法去查得到:
+-------+------+
| c_no | s_no |
+-------+------+
| 9-888 | 101 |
+-------+------+
有两条数据但是只显示一条,有问题
– 11.查询每门课的平均成绩
SELECT c_no,AVG(sc_degree) FROM score GROUP BY c_no;
– 12,查询score表中至少有2名学生选修的,并且以3开头的课程的平均分
SELECT AVG(sc_degree),c_no from score GROUP BY c_no HAVING COUNT(c_no) >= 2 AND c_no like '3%' ;
– 13.查询分数大于70但是小于90的s_no列:
SELECT s_no,sc_degree FROM score WHERE sc_degree BETWEEN 71 AND 89;
– 14.查询所有的学生 s_name , c_no, sc_degree列
SELECT s_name, c_no, sc_degree FROM student,score WHERE student.s_no = score.s_no;
– 15.查询所有学生的s_no, c_name, sc_degree列
SELECT student.s_no, c_name, sc_degree FROM student, course,score WHERE student.s_no = score.s_no AND score.c_no = course.c_no ;
– 16.查询所有的学生 s_name , c_name, sc_degree列
SELECT s_name, c_name, sc_degree FROM student, course, score WHERE student.s_no = score.s_no AND score.c_no = course.c_no;
– 17.查询班级是’95031’班学生每门课的平均分
select c_no,AVG(sc_degree) from score WHERE s_no IN (select s_no from student where s_class = '95031') GROUP BY c_no;
select c_no,AVG(sc_degree) from score AS sc LEFT JOIN student AS s ON sc.s_no = s.s_no WHERE s.s_class = '95031' GROUP BY sc.c_no;
select sc.c_no,AVG(sc.sc_degree) FROM student AS s, score AS SC WHERE s.s_class = '95031' AND s.s_no = sc.s_no GROUP BY sc.c_no ;
进阶,加入课程名称:
SELECT sc.c_no, c.c_name, AVG(sc.sc_degree) FROM student AS s, score AS SC, course AS c WHERE s.s_class = '95031' AND s.s_no = sc.s_no AND c.c_no = sc.c_no GROUP BY sc.c_no ;
– 18.查询选修"3-105"课程的成绩高于’109’号同学’3-105’成绩 的所有同学的记录
SELECT * FROM student AS s, score AS sc WHERE sc.c_no = '3-105' AND sc.sc_degree > (SELECT sc_degree FROM score WHERE s_no = '109' AND c_no = '3-105') AND s.s_no = sc.s_no;
– 19.查询成绩高于学号为’109’,课程号为’3-105’的成绩的所有记录
SELECT * FROM score WHERE sc_degree >(SELECT sc_degree FROM score WHERE s_no = '109' AND c_no = '3-105');
– 20.查询所有学号为108.101的同学同年出生的所有学生的s_no,s_name和s_birthday
SELECT * FROM student WHERE YEAR(s_birthday) IN (SELECT YEAR(s_birthday) FROM student WHERE s_no IN('108','101'));