MySQL數據庫基礎練習一

MySQL查詢練習一


數據準備:

學生表studentmysql

學號:姓名:性別:出生日期:所在班級;sql

create table student (
    sno varchar(20) primary key,
    sname varchar(20) not null,
    ssex varchar(10) not null,
    sbirthday datetime,
    class varchar(10)
);

教師表teacher操作系統

教師編號,教師名字,教師性別,出生日期,職稱,所在部門。code

create table teacher(
    tno varchar(20) primary key,
    tname varchar(20) not null,
    tsex varchar(10) not null,
    tbirthday datetime,
    prof varchar(20),
    depart varchar(20) not null
);

課程表course排序

課程號,課程名稱,教師編號ci

create table course(
    cno varchar(20) primary key,
    cname varchar(20) not null,
    tno varchar(20) not null,
    foreign key(tno) references teacher(tno)
);

成績表score數學

學號,課程號,成績it

create table score(
    sno varchar(20) not null,    cno varchar(20) not null,
    degree decimal,
    foreign key(sno) references student(sno),
    foreign key(cno) references course(cno),
    primary key(sno,cno)
);

添加數據:table

insert into student values('101', '曾華','男','1977-09-01','95033');
insert into student values('102','匡明','男','1975-10-02', '95031');
insert into student values('103','王麗','女','1976-01-23', '95033');
insert into student values('104', '李軍','男','1976-02-20','95033');
insert into student values('105','王芳','女','1975-02-10', '95031');
insert into student values('106','陸君','男','1974-06-03','95031');
insert into student values('107','王尼瑪','男','1974-06-03','95031');
insert into student values('108','張全蛋','男','1974-06-03','95031');
insert into student values('109','趙鐵柱','男','1974-06-03','95031');

insert into teacher values('804','李誠','男','1958-12-02', '副教授','計算機系');
insert into teacher values('856','張旭','男','1969-03-12','講師','電子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','計算機系');
insert into teacher values('831','劉冰','女','1977-08-14','助教','電子工程系');

insert into course values('3-105','計算機導論', '825');
insert into course values('3-245','操做系統','804');
insert into course values('6-166','數字電路','856');
insert into course values('9-888','高等數學','831');

insert into score values('103','3-105','92');
insert into score values('103','3-245','86');
insert into score values('103','6-166','85');
insert into score values('105','3-105','88');
insert into score values('105','3-245','75');
insert into score values('105','6-166','79');
insert into score values('109','3-105','76');
insert into score values('109','3-245','68');
insert into score values('109','6-166','81');

一、查詢student表中的全部記錄;

mysql> select * from student;
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 101 | 曾華      | 男   | 1977-09-01 00:00:00 | 95033 |
| 102 | 匡明      | 男   | 1975-10-02 00:00:00 | 95031 |
| 103 | 王麗      | 女   | 1976-01-23 00:00:00 | 95033 |
| 104 | 李軍      | 男   | 1976-02-20 00:00:00 | 95033 |
| 105 | 王芳      | 女   | 1975-02-10 00:00:00 | 95031 |
| 106 | 陸君      | 男   | 1974-06-03 00:00:00 | 95031 |
| 107 | 王尼瑪    | 男   | 1974-06-03 00:00:00 | 95031 |
| 108 | 張全蛋    | 男   | 1974-06-03 00:00:00 | 95031 |
| 109 | 趙鐵柱    | 男   | 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+
9 rows in set (0.00 sec)

二、查詢student表中的全部記錄的sname,ssex,class列;

mysql> select sname, ssex, class from student;
+-----------+------+-------+
| sname     | ssex | class |
+-----------+------+-------+
| 曾華      | 男   | 95033 |
| 匡明      | 男   | 95031 |
| 王麗      | 女   | 95033 |
| 李軍      | 男   | 95033 |
| 王芳      | 女   | 95031 |
| 陸君      | 男   | 95031 |
| 王尼瑪    | 男   | 95031 |
| 張全蛋    | 男   | 95031 |
| 趙鐵柱    | 男   | 95031 |
+-----------+------+-------+
9 rows in set (0.00 sec)

三、查詢教師全部的單位即不重複的depart列;

關鍵字distinct排除重複。class

mysql> select distinct depart from teacher;
+-----------------+
| depart          |
+-----------------+
| 計算機系        |
| 電子工程系      |
+-----------------+
2 rows in set (0.01 sec)

四、查詢score表中成績在60到80之間的全部記錄;

查詢區間:
方式一:between...and...

mysql> select * from score where degree between 60 and 80;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
+-----+-------+--------+
4 rows in set (0.00 sec)

方式二:直接使用運算符比較;

mysql> select * from score where degree > 60 and degree < 80;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 105 | 3-245 |     75 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 109 | 3-245 |     68 |
+-----+-------+--------+
4 rows in set (0.00 sec)

五、查詢score表中成績爲85,86或88的記錄;

表示或着關係的查詢;in關鍵字。

mysql> select * from score where degree in(85,86,88);
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-245 |     86 |
| 103 | 6-166 |     85 |
| 105 | 3-105 |     88 |
+-----+-------+--------+
3 rows in set (0.00 sec)

六、查詢student表中「95031」班或者性別爲女的記錄;

關鍵字or,能夠是不一樣的字段

mysql> select * from student where class='95031' or ssex='女';
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 102 | 匡明      | 男   | 1975-10-02 00:00:00 | 95031 |
| 103 | 王麗      | 女   | 1976-01-23 00:00:00 | 95033 |
| 105 | 王芳      | 女   | 1975-02-10 00:00:00 | 95031 |
| 106 | 陸君      | 男   | 1974-06-03 00:00:00 | 95031 |
| 107 | 王尼瑪    | 男   | 1974-06-03 00:00:00 | 95031 |
| 108 | 張全蛋    | 男   | 1974-06-03 00:00:00 | 95031 |
| 109 | 趙鐵柱    | 男   | 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+
7 rows in set (0.00 sec)

七、以class降序查詢student表的全部記錄;

排序的關鍵字:order by...
降序:desc
升序:asc默承認以不寫

mysql> select * from student order by class desc;
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 101 | 曾華      | 男   | 1977-09-01 00:00:00 | 95033 |
| 103 | 王麗      | 女   | 1976-01-23 00:00:00 | 95033 |
| 104 | 李軍      | 男   | 1976-02-20 00:00:00 | 95033 |
| 102 | 匡明      | 男   | 1975-10-02 00:00:00 | 95031 |
| 105 | 王芳      | 女   | 1975-02-10 00:00:00 | 95031 |
| 106 | 陸君      | 男   | 1974-06-03 00:00:00 | 95031 |
| 107 | 王尼瑪    | 男   | 1974-06-03 00:00:00 | 95031 |
| 108 | 張全蛋    | 男   | 1974-06-03 00:00:00 | 95031 |
| 109 | 趙鐵柱    | 男   | 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+
9 rows in set (0.00 sec)

八、以cno升序、degree降序查詢score表中的全部記錄;

以逗號隔開。

mysql> select * from score order by cno asc,degree desc;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
| 105 | 3-105 |     88 |
| 109 | 3-105 |     76 |
| 103 | 3-245 |     86 |
| 105 | 3-245 |     75 |
| 109 | 3-245 |     68 |
| 103 | 6-166 |     85 |
| 109 | 6-166 |     81 |
| 105 | 6-166 |     79 |
+-----+-------+--------+
9 rows in set (0.00 sec)

九、查詢’95031‘班的全部人數;

統計:count

mysql> select * from student;
+-----+-----------+------+---------------------+-------+
| sno | sname     | ssex | sbirthday           | class |
+-----+-----------+------+---------------------+-------+
| 101 | 曾華      | 男   | 1977-09-01 00:00:00 | 95033 |
| 102 | 匡明      | 男   | 1975-10-02 00:00:00 | 95031 |
| 103 | 王麗      | 女   | 1976-01-23 00:00:00 | 95033 |
| 104 | 李軍      | 男   | 1976-02-20 00:00:00 | 95033 |
| 105 | 王芳      | 女   | 1975-02-10 00:00:00 | 95031 |
| 106 | 陸君      | 男   | 1974-06-03 00:00:00 | 95031 |
| 107 | 王尼瑪    | 男   | 1974-06-03 00:00:00 | 95031 |
| 108 | 張全蛋    | 男   | 1974-06-03 00:00:00 | 95031 |
| 109 | 趙鐵柱    | 男   | 1974-06-03 00:00:00 | 95031 |
+-----+-----------+------+---------------------+-------+
9 rows in set (0.00 sec)

十、查詢score表中的最高分的學生學號和課程號;

子查詢或者排序;
複合語句。

mysql> select sno,cno from score where degree=(select max(degree) from score);
+-----+-------+
| sno | cno   |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)

步驟:
①、找到最高分

select max(degree) from score;

②、找最高分的sno和cno

mysql> select sno,cno from score where degree=(select max(degree) from score);

③、排序的作法:
存在必定缺陷,好比成績最高有多個分相同。

mysql> select sno,cno,degree from score order by degree desc;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
| 105 | 3-105 |     88 |
| 103 | 3-245 |     86 |
| 103 | 6-166 |     85 |
| 109 | 6-166 |     81 |
| 105 | 6-166 |     79 |
| 109 | 3-105 |     76 |
| 105 | 3-245 |     75 |
| 109 | 3-245 |     68 |
+-----+-------+--------+
9 rows in set (0.00 sec)

limit第一個數字表示從第幾條開始,第二個數字表示查多少條。

mysql> select sno,cno,degree from score order by degree desc limit 0,1;
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
+-----+-------+--------+
1 row in set (0.00 sec)

十一、查詢每門課的平均成績;

avg()
查詢一門的平均成績;

mysql> select avg(degree) from score where cno='3-105';
+-------------+
| avg(degree) |
+-------------+
|     85.3333 |
+-------------+
1 row in set (0.00 sec)

一個語句中查詢多門課程的平均成績;
...group by...分組

mysql> select cno,avg(degree) from score group by cno;
+-------+-------------+
| cno   | avg(degree) |
+-------+-------------+
| 3-105 |     85.3333 |
| 3-245 |     76.3333 |
| 6-166 |     81.6667 |
+-------+-------------+
3 rows in set (0.00 sec)

十二、查詢score表中至少有兩名學生選修的並以3開頭的課程的平均成績

分組條件與模糊查詢

mysql> select cno,avg(degree),count(*) from score group by cno having count(cno)>=2 and cno like '3%';
+-------+-------------+----------+
| cno   | avg(degree) | count(*) |
+-------+-------------+----------+
| 3-105 |     85.3333 |        3 |
| 3-245 |     76.3333 |        3 |
+-------+-------------+----------+
2 rows in set (0.00 sec)

分組條件:group by...having...
模糊查詢:like

1三、查詢分數大於70小於80的sno列

範圍查詢的兩種方式
方式一:

mysql> select sno,degree from score where degree>70 and degree<90;
+-----+--------+
| sno | degree |
+-----+--------+
| 103 |     86 |
| 103 |     85 |
| 105 |     88 |
| 105 |     75 |
| 105 |     79 |
| 109 |     76 |
| 109 |     81 |
+-----+--------+
7 rows in set (0.00 s

方式二:between...and...

mysql>  select sno,degree from score where degree between 70 and 90;
+-----+--------+
| sno | degree |
+-----+--------+
| 103 |     86 |
| 103 |     85 |
| 105 |     88 |
| 105 |     75 |
| 105 |     79 |
| 109 |     76 |
| 109 |     81 |
+-----+--------+
7 rows in set (0.00 sec)

1四、查詢全部學生的cno,sname和degree

多表查詢

mysql> select sname,cno,degree from student,score where student.sno=score.sno;
+-----------+-------+--------+
| sname     | cno   | degree |
+-----------+-------+--------+
| 王麗      | 3-105 |     92 |
| 王麗      | 3-245 |     86 |
| 王麗      | 6-166 |     85 |
| 王芳      | 3-105 |     88 |
| 王芳      | 3-245 |     75 |
| 王芳      | 6-166 |     79 |
| 趙鐵柱    | 3-105 |     76 |
| 趙鐵柱    | 3-245 |     68 |
| 趙鐵柱    | 6-166 |     81 |
+-----------+-------+--------+
9 rows in set (0.00 sec)

1五、查詢全部學生的sno,cname和degree列

mysql> select sno,cname,degree from course,score where course.cno=score.cno;
+-----+-----------------+--------+
| sno | cname           | degree |
+-----+-----------------+--------+
| 103 | 計算機導論      |     92 |
| 103 | 操做系統        |     86 |
| 103 | 數字電路        |     85 |
| 105 | 計算機導論      |     88 |
| 105 | 操做系統        |     75 |
| 105 | 數字電路        |     79 |
| 109 | 計算機導論      |     76 |
| 109 | 操做系統        |     68 |
| 109 | 數字電路        |     81 |
+-----+-----------------+--------+
9 rows in set (0.05 sec)

1六、全部學生的sname,cname,degree列

三表關聯查詢,經過共同字段的相等關係來聯繫在一塊兒。

mysql> select sname,cname,degree from student,course,score
where student.sno=score.sno and course.cno = score.cno;
+-----------+-----------------+--------+
| sname     | cname           | degree |
+-----------+-----------------+--------+
| 王麗      | 計算機導論      |     92 |
| 王麗      | 操做系統        |     86 |
| 王麗      | 數字電路        |     85 |
| 王芳      | 計算機導論      |     88 |
| 王芳      | 操做系統        |     75 |
| 王芳      | 數字電路        |     79 |
| 趙鐵柱    | 計算機導論      |     76 |
| 趙鐵柱    | 操做系統        |     68 |
| 趙鐵柱    | 數字電路        |     81 |
+-----------+-----------------+--------+
9 rows in set (0.00 sec)

1七、查詢95031班學生每門課的平均分

子查詢加分組求平均分

mysql> select cno,avg(degree)
    -> from score
    -> where sno in(select sno from student where class = '95031')
    -> group by cno;
+-------+-------------+
| cno   | avg(degree) |
+-------+-------------+
| 3-105 |     82.0000 |
| 3-245 |     71.5000 |
| 6-166 |     80.0000 |
+-------+-------------+
3 rows in set (0.08 sec)

1八、查詢選修3-105課程的成績高於109號同窗3-105成績的全部同窗的記錄

子查詢

步驟:
① select degree from score where sno='109'and cno ='3-105';
② select * from score where cno = '3-105' and degree > (select degree from score where sno='109'and cno ='3-105');

mysql> select * from score where cno = '3-105'and degree >(select degree from score where sno='109'and cno ='3-105');
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
| 105 | 3-105 |     88 |
+-----+-------+--------+
2 rows in set (0.00 sec)

19查詢成績高於學號10九、課程號爲3-105的成績的全部記錄

mysql> SELECT * from score 
where degree > (SELECT degree FROM score where sno = '109' and cno = '3-105' );
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 3-105 |     92 |
| 103 | 3-245 |     86 |
| 103 | 6-166 |     85 |
| 105 | 3-105 |     88 |
| 105 | 6-166 |     79 |
| 109 | 6-166 |     81 |
+-----+-------+--------+
6 rows in set (0.00 sec)

20、查詢和學號爲10八、101的同窗同年出生的全部學生的sno、sname、和sbirthday列

year()+in關鍵字的子查詢

mysql> SELECT sno,sname, sbirthday from student
    -> where YEAR(sbirthday) in (SELECT year(sbirthday) from student where sno in( 101,108));
+-----+-----------+---------------------+
| sno | sname     | sbirthday           |
+-----+-----------+---------------------+
| 101 | 曾華      | 1977-09-01 00:00:00 |
| 106 | 陸君      | 1974-06-03 00:00:00 |
| 107 | 王尼瑪    | 1974-06-03 00:00:00 |
| 108 | 張全蛋    | 1974-06-03 00:00:00 |
| 109 | 趙鐵柱    | 1974-06-03 00:00:00 |
+-----+-----------+---------------------+
5 rows in set (0.00 sec)

2一、查詢張旭教師任課的學生成績

多層嵌套子查詢

步驟:
① SELECT tno FROM teacher WHERE tname='張旭';
② SELECT cno from course where tno =(SELECT tno FROM teacher WHERE tname='張旭');
③ SELECT * FROM score 
WHERE cno =(SELECT cno from course where tno =(SELECT tno FROM teacher WHERE tname='張旭'));

mysql> SELECT * FROM score WHERE cno =(SELECT cno from course where tno =(SELECT tno FROM teacher WHERE tname='張旭'));
+-----+-------+--------+
| sno | cno   | degree |
+-----+-------+--------+
| 103 | 6-166 |     85 |
| 105 | 6-166 |     79 |
| 109 | 6-166 |     81 |
+-----+-------+--------+
3 rows in set (0.00 sec)

2二、查詢選修某課程的同窗人數多於5人的教師姓名

多表查詢+子查詢

步驟:
① SELECT cno from score GROUP BY cno HAVING count(*)>5;
② select tno from course where cno=(SELECT cno from score GROUP BY cno HAVING count(*)>5);
③ SELECT tname from teacher 
WHERE tno = (select tno from course where cno=(SELECT cno from score GROUP BY cno HAVING count(*)>5));

mysql> SELECT tname from teacher WHERE tno = (select tno from course where cno=(SELECT cno from score GROUP BY cno HAVING count(*)>5));
+--------+
| tname  |
+--------+
| 王萍   |
+--------+
1 row in set (0.00 sec)

2三、查詢95033班95031班全體學生的記錄

in表示或者關係

SELECT * from student WHERE class IN(95033,95031);

2四、查詢存在成績有85分以上成績的課程cno

select cno from score where degree>85;

mysql> SELECT cno,degree FROM score WHERE degree>85;
+-------+--------+
| cno   | degree |
+-------+--------+
| 3-105 |     90 |
| 3-105 |     91 |
| 3-105 |     92 |
| 3-245 |     86 |
| 3-105 |     88 |
| 3-105 |     88 |
+-------+--------+
6 rows in set (0.00 sec)
相關文章
相關標籤/搜索