MySQL查询之每日十题(二)

885 阅读4分钟

承接MySQL查询(一)

这是我参与8月更文挑战的第23天,活动详情查看:8月更文挑战

资源问题:

将student各表,以及study表放到百度网盘中,自取;

链接:pan.baidu.com/s/1CxZA_pb9… 提取码:1234

每日十题(二)

(11)查询选修了课程的学生的总人数。

(要求:分别使用嵌套子查询的谓词IN和EXISTS完成)

谓词IN:

SELECT count(DISTINCT sno) FROM sc WHERE sno in (select sno from sc);

谓词EXISTS:

SELECT COUNT(DISTINCT sno) FROM sc WHERE  EXISTS (SELECT sno FROM sc);

(12)统计各门课程选修人数,要求输出课程代号,课程名,选修人数,有成绩人数(grade不为NULL),最高分,最低分,平均分,按课程号排序。

Select c.cno,cname,count(*),MAX(grade),MIN(grade),AVG(grade),COUNT(grade)

From student s,sc,course c where s.sno=sc.sno and sc.cno=c.cno GROUP BY c.cno;

(13)统计各门课程的重修人数(包括grade为NULL),要求输出课程代号,重修人数。

SELECT cno,COUNT(*) from sc WHERE grade<60 OR grade is NULL GROUP BY cno;

(14)查询选修通过2门(包括2门)以上的学生的信息,输出学号、选修通过门数、平均成绩,按门数降序排序,若门数相同,按照成绩降序。

select sno, count(*),avg(grade)

from sc	where grade >= 60

group by sno

having count(*) >= 2 order by count(*) DESC,avg(grade) desc;

(15)查询与“王大力”同一个系的学生的基本信息。

SELECT * FROM student WHERE sname !='王大力' and sdept in (

SELECT distinct sdept FROM student WHERE sname='王大力');

(16)查询每个学生高于他自己选修平均分的那门课程的成绩,输出学号,课程号,课程成绩,他所有课程的平均分,并按学号升序排列。

(要求:使用基于子查询派生表的查询方法)

SELECT sc.sno,cno,grade, avggrade FROM sc,

(SELECT sno, AVG(grade) avggrade FROM sc GROUP BY sno) AS  avg_sc

WHERE sc.sno=avg_sc.sno AND

 sc.grade>avg_sc.avggrade

ORDER BY sc.sno;

(17)查询没有同时选修“计算机导论”和“计算机网络”两门课的学生的学号,姓名。

Select sno,sname from student where sno not in(Select sno from sc,course where sc.cno=course.cno and cname='计算机导论'And sno in (Select sno from sc,course where sc.cno=course.cno and cname='计算机网络'));(包含了没有任何选课的同学的信息)
Select sno,sname from student where sno not in (select sno from student where not exists(

Select * from course where cname in ('计算机网络','计算机导论') and not exists(

Select * from sc where sno=student.sno and cno=course.cno)

));(包含了没有任何选课的同学信息)
Select distinct sc.cno,sname from student,sc where student.sno=sc.sno and

sc.sno not in(select sno from sc,course where sc.cno=course.cno and cname='计算机导论' and sno in (select sno from sc,course where sc.cno=course.cno and cname='计算机网络'));

(18)查询选修了全部课程的学生的学号,姓名,系名。

select student.sno,sname,sdept from student where NOT exists     

(select  * from course where NOT exists    

(select * from sc where sc.sno = student.sno  and sc.cno = course.cno));

补充:

Select sno,sname,sdept from student where sno in(

Select sno from sc group by sno having count(*)=(select count(*) from course)

);

(19)输出“高等数学”课程成绩前三名的学生的学号,姓名,系名

SELECT s.sno,sname,sdept from student s,sc,course c WHERE s.sno=sc.sno and sc.cno=c.cno AND cname='高等数学'

ORDER BY grade DESC LIMIT 3;(不考虑成绩有重复值的情况)

补充:

Select student.sno,sname,sdept from student,sc,course,(select distinct grade from sc,course where sc.cno=course.cno and cname='高等数学'order by grade
desc limit 3) as g where student.sno=sc.sno and sc.cno=course.cno and sc.grade=g.grade and cname='高等数学';

加量不加文:

3、导入数据库study,完成下列查询,将查询语句写在下方。

(1)查询总经理、经理以下的职员信息,包括NULL值记录。

select * from employee where job_title is null or job_title not in (
select job_title from employee where job_title ='总经理' or job_title='经理'
);

(2)查询“联荣资产”的客户信息。

select * from customer where customer_name like '%联荣资产%';

3、导入数据库study,完成下列查询,将查询语句写在下方。

(1)查询总经理、经理以下的职员信息,包括NULL值记录。

SELECT * FROM employee WHERE job_title is NULL

OR job_title not IN(

SELECT job_title from employee WHERE job_title='总经理'

OR job_title='经理'

);

(2)查询“联荣资产”的客户信息。

SELECT * from customer WHERE customer_name LIKE '%联荣资产%';

(3)查询价格5000-6000的“联想”品牌和价格在5000以下的“小米”品牌的产品信息。

select * FROM product WHERE description LIKE '%联想%' AND price BETWEEN 5000 AND 6000 

UNION 

SELECT * FROM product WHERE description LIKE '%小米%' AND price < 5000;

(4)查询如“GTX950M”/“GTX960M”系列的产品信息。

SELECT * FROM product WHERE description LIKE '%GTX950M%' OR description LIKE '%GTX960M%';

(5)统计各年份订单总数,订单总额,按年份降序排列。

SELECT YEAR(pay_time),count(*) as number,sum(total_money) as money FROM payment GROUP BY YEAR(pay_time) ORDER BY YEAR(pay_time) DESC;

(6)统计2016年各产品的订购数量(降序排列),输出5-10名的统计信息,包括产品ID,订购总数。

select product_id,payment.order_id, count(*), payment_id

from payment, order_detail where year(pay_time) = 2016 

and payment.order_id = order_detail.order_id group by order_id

order by buy_number desc limit 4,6;

结束:

如果你看到这里或者正好对你有所帮助,希望能点个👍或者⭐感谢;

有错误的地方,欢迎在评论指出,作者看到会进行修改。