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.查詢年齡爲空的學生信息。

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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章