Mysql单表查询例题详解

以下查询例子都是通过下面创建的表进行查询的,可根据自己情况酌情查看:

学生表:
在这里插入图片描述
课程表:
在这里插入图片描述
1.查询全部课程的信息。

select * from course;

2.查询信工学院开设的课程名、课程号及学分。

select cs_name,cs_id,cs_credit from course where cs_depart='信工';

3.查询学分超过3学分的课程代码、课程名和开课单位。

select cs_id,cs_name,cs_depart from course where cs_credit>3;

4.查询计科专业和大数据专业的学生信息。

select * from student where stu_major='计科' or stu_major='大数据';
select * from student where stu_major in ('计科','大数据');

5.查询不是信工学院的学生姓名和学号。

select stu_id,stu_name from student where stu_colleng!='信工学院';
select stu_id,stu_name from student where stu_college not in('信工学院');
select stu_id,stu_name from student where stu_college not like'信工学院';

6.查询年龄是17,18,19的学生姓名和专业。

select stu_name,stu_major from student where stu_age in(17,18,19);
select stu_name,stu_major from student where stu_age=17 or stu_age=18 or stu_age=19;
select stu_name,stu_major from student where stu_age between 17 and 19;

7.查询学分在2到4之间课程的信息。

select * from course where cs_credit>=2 and cs_credit<=4;
select * from course where cs_credit between 2 and 4;

8.查询课程名称中带“数据”的课程名、课程号及开课单位。

select cs_name,cs_id,cs_depart from course where cs_name like'%数据%';

9.查询信工学院的的专业有哪些。

select  distinct stu_major from student where stu_college='信工学院';
select  distinct stu_major from student where stu_college like'信工学院';
select  distinct stu_major from student where stu_college like'信工%';

10.查询年龄为空的学生信息。

扫描二维码关注公众号,回复: 11377700 查看本文章
select * from student where stu_age is null;

11.查询不是信工学院开设的集中实践课的开课单位和课程名称。

select cs_depart,cs_name from course where cs_depart!='信工' or cs_type<>'集中实践';

12.查询信工学院开设的课程的类型有哪些。

select distinct cs_type from course where cs_depart='信工';

13.查询学生所在的专业个数。

select count(distinct stu_major) from student where stu_college='信工学院';

14.查询信工学院开设的课程的平均学分。

select avg(cs_credit) 平均分 from course where cs_depart='信工';

15.查询学生的信息,查询结果按姓名升序排序。

select * from student order by stu_name asc;

16.查询 每个专业的学生的最大年龄、最小年龄和平均年龄,查询结果按平均年龄降序排列。

select stu_major,max(stu_age) 最大年龄,min(stu_age) 最小年龄,avg(stu_age) 平均年龄 
from student group by stu_major order by avg(stu_age) desc;

17.查询每个开课单位开设的课程门数的,查询结果按课程门数升序排列。

select cs_depart,count(*) 课程门数 from course group by cs_depart order by 课程门数;

18.查询单位开课门数少于2门的开课单位和课程名称。

select cs_depart,count(*) 课程门数 from course group by cs_depart having 课程门数<2;

猜你喜欢

转载自blog.csdn.net/qq_44859533/article/details/106098321