查询成绩小于85且是计算机的一项应用,查询练习2
查詢練習(xí)
創(chuàng)建表
學(xué)生表
student
學(xué)號(hào)
姓名
性別
出生日期
班級(jí)
create table student(
sno varchar(20) primary key,
sname varchar(20) not null,
ssex varchar(10)not null,
sbirthday datetime,
class varchar(20));
課程表
Course
課程號(hào)
課程名稱
教師編號(hào)
create table course(
cno varchar(20) primary key,
cname varchar(20) not null,
tno varchar(20) not null,
foreign key(tno) references teacher(tno));
成績(jī)表
score
學(xué)號(hào)
課程號(hào)
成績(jī)
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)
);
教師表
teacher
編號(hào)
名字
性別
出生日期
職稱
部門
create table teacher(
tno varchar(20) primary key,
tname varchar(20) not null,
tsex varchar(10) not null,
tbirthday datetime,
prof varchar(20) not null,
depart varchar(20)not null);
插入數(shù)據(jù)
添加學(xué)生信息
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','王尼瑪','男','1976-01-23','95033');
insert into student values('108','張全蛋','男','1976-02-20', '95033');
insert into student values('109','趙鐵柱','男','1975-02-10','95031');
添加教師信息
insert into teacher values('804','李誠(chéng)','男','1958-12-02','副教授','計(jì)算機(jī)系');
insert into teacher values('856','張旭','男','1969-03-12','講師','電子工程系');
insert into teacher values('825','王萍','女','1972-05-05','助教','計(jì)算機(jī)系');
insert into teacher values( '831','劉冰','女','1977-08-14','助教','電子工程系');
添加課程
insert into course values('3-105',"計(jì)算機(jī)導(dǎo)論",'825');
insert into course values('3-245',"操作系統(tǒng)","804");
insert into course values('6-166',"數(shù)字電路",'856');
insert into course values('9-888',"高等數(shù)學(xué)",'831');
添加成績(jī)表
insert into score values("103", '3-245','86');
insert into score values("105", '3-245', '75');
insert into score values('109', '3-245', '68');
insert into score values('103', "3-105", '92');
insert into score values('105', '3-105', "88");
insert into score values('109', '3-105',"76");
insert into score values("103", '6-166','85');
insert into score values('105', '6-166','79');
insert into score values('109', '6-166','81');
查詢練習(xí)
1.查詢student表的所有記錄。
select * from student;# * 表示所有字段
2.查詢student表中的所有記錄的sname,ssex和class列。
select sname,ssex,class from student;
3.查詢教師所有單位及不重復(fù)的depart列。
select distinct depart from teacher; #distinct:排除重復(fù)
4、查詢score表中成績(jī)?cè)?0到80之間的所有記錄。
查詢區(qū)間 between....and......
select * from score where degree between 60 and 80;
或
select * from score where degree > 60 and degree<80;#運(yùn)算符表示
5、查詢score表中成績(jī)?yōu)?5,86或88的記錄。
表示或者關(guān)系的查詢 in
select * from score where degree in(85,86,88);
6、查詢student表中“95031"班或性別為“女”的同學(xué)記錄。#or表示或者
select * from student where class="95031"or ssex="女";
7、以class降序查詢student表的所有記錄。
升序降序
select * from student order by class desc;#降序
select * from student order by class; #默認(rèn)升序
select * from student order by class asc;#升序
8、以cno升序、degree降序查詢score表的所有記錄。#先以cno為升序,遇見相同的異score為降序
select * from score order by cno asc,degree desc;
9、查詢“95031"班的學(xué)生人數(shù)。
統(tǒng)計(jì) count
select count(*) from student where class="95031";
10、查詢score表中的最高分的學(xué)生學(xué)號(hào)和課程號(hào)。(子查詢或者排序)
select sno,cno from score where degree=(select max(degree)from score);
排序的做法
select sno,cno,degree from score order by degree desc limit 0,1;#limit 0表示從哪開始,1表示查幾條
查詢練習(xí)
11.查詢每門課的平均成績(jī)
select * from course;
avg 求平均值
select avg(degree) from score where cno ="3-105";#一門課程
select cno,avg(degree) from score group by cno;#先利用group進(jìn)行分組
12.查詢score表中至少有2名學(xué)生選修的并以3開頭的課程的平均分?jǐn)?shù)
select cno,avg(degree),count(*) from score group by cno having count(cno)>=2 and cno like "3%";#like%3以3為開頭的
13.查詢分?jǐn)?shù)大于70,小于90的sno列
select sno,degree from score where degree>70 and degree<90;
或者
select sno,degree from score where degree between 70 and 90;
14.查詢所有學(xué)生的sname、cno、degree列
示例一:進(jìn)行分開查詢,其中sno在兩個(gè)表中是存在交集的
select sno,sname from student;
+-----+--------+
| sno | sname |
+-----+--------+
| 101 | 曾華 |
| 102 | 匡明 |
| 103 | 王麗 |
| 104 | 李軍 |
| 105 | 王芳 |
| 106 | 陸君 |
| 107 | 王尼瑪 |
| 108 | 張全蛋 |
| 109 | 趙鐵柱 |
select sno,cno,degree from score;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 105 | 3-105 | 88 |
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
| 109 | 6-166 | 81 |
+-----+-------+--------+
示例二,將兩個(gè)查詢進(jìn)行合并
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 |
+--------+-------+--------+
多表查詢
15.查詢所有學(xué)生的sno,cname,和degree列。
示例一先分別表示
select cno,cname from course;
+-------+------------+
| cno | cname |
+-------+------------+
| 3-105 | 計(jì)算機(jī)導(dǎo)論 |
| 3-245 | 操作系統(tǒng) |
| 6-166 | 數(shù)字電路 |
| 9-888 | 高等數(shù)學(xué) |
select cno,sno,degree from score;
+-------+-----+--------+
| cno | sno | degree |
+-------+-----+--------+
| 3-105 | 103 | 92 |
| 3-245 | 103 | 86 |
| 6-166 | 103 | 85 |
| 3-105 | 105 | 88 |
| 3-245 | 105 | 75 |
| 6-166 | 105 | 79 |
| 3-105 | 109 | 76 |
| 3-245 | 109 | 68 |
| 6-166 | 109 | 81 |
+-------+-----+--------+
示例二
select sno,cname,degree from course,score where course.cno = score.cno;
16.三聯(lián)表查詢
查詢所有學(xué)生的sname、cname和degree列
select sno,cno,degree from score;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 103 | 3-245 | 86 |
| 103 | 6-166 | 85 |
| 105 | 3-105 | 88 |
| 105 | 3-245 | 75 |
| 105 | 6-166 | 79 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
| 109 | 6-166 | 81 |
select sname,sno from student;
+--------+-----+
| sname | sno |
+--------+-----+
| 曾華 | 101 |
| 匡明 | 102 |
| 王麗 | 103 |
| 李軍 | 104 |
| 王芳 | 105 |
| 陸君 | 106 |
| 王尼瑪 | 107 |
| 張全蛋 | 108 |
| 趙鐵柱 | 109 |
+--------+-----+
select cname,cno from course;
+------------+-------+
| cname | cno |
+------------+-------+
| 計(jì)算機(jī)導(dǎo)論 | 3-105 |
| 操作系統(tǒng) | 3-245 |
| 數(shù)字電路 | 6-166 |
| 高等數(shù)學(xué) | 9-888 |
+------------+-------+
select sname,cname,degree from score,student,course where course.cno=score.cno and student.sno=score.sno;
+--------+------------+--------+
| sname | cname | degree |
+--------+------------+--------+
| 王麗 | 計(jì)算機(jī)導(dǎo)論 | 92 |
| 王麗 | 操作系統(tǒng) | 86 |
| 王麗 | 數(shù)字電路 | 85 |
| 王芳 | 計(jì)算機(jī)導(dǎo)論 | 88 |
| 王芳 | 操作系統(tǒng) | 75 |
| 王芳 | 數(shù)字電路 | 79 |
| 趙鐵柱 | 計(jì)算機(jī)導(dǎo)論 | 76 |
| 趙鐵柱 | 操作系統(tǒng) | 68 |
| 趙鐵柱 | 數(shù)字電路 | 81 |
+--------+------------+--------+
我TM就是個(gè)天才
select sname,cname,degree ,student.sno as stu_sno,course.cno as cou_cno from score,student,course where course.cno=score.cno and student.sno=score.sno;#student.sno as 語句需要學(xué)習(xí)
子查詢
17.子查詢加分組求平均分
查詢“95031”班學(xué)生每門課的平均分
示例一,先進(jìn)行拆分的表示
select * from student where class="95031";
mysql> select * from score where sno in(select sno from student where class="95031");#此處的in是非常值得學(xué)習(xí)的,篩出來的必須是同一樣的
mysql> select cno,avg(degree) from score where sno in(select sno from student where class="95031") group by cno;#此處的group用的也非常棒
TM邏輯鬼才啊,反正就是抽絲剝繭,反復(fù)練習(xí),看好最小范圍
18.子查詢
查詢選修“3-105”課程的成績(jī)高于“109”號(hào)同學(xué)“3-105”成績(jī)的所有同學(xué)的記錄。
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");
19.查詢成績(jī)高于學(xué)號(hào)為“109”、課程號(hào)為“3-105”的成績(jī)的所有記錄
select degree from score where sno="109" and cno="3-105";
select * from score where degree>(select degree from score where sno="109" and cno="3-105");
20.查詢學(xué)號(hào)為108、101的同學(xué)出生的所有學(xué)生的sno、sname和sbirthday列。
select * from student where sno="108" or sno ="101";
或者
select * from student where sno in("108",101);
只查年份
select year(sbirthday) from student where sno in("108",101);
進(jìn)一步進(jìn)行查詢
select * from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));
21.查詢“張旭”教師任課的學(xué)生成績(jī)
select * from teacher where tname="張旭";#第一步
mysql> select cno from course where tno=( select tno from teacher where tname="張旭");#第二步
mysql> select sno,degree from score where cno=(select cno from course where tno=( select tno from teacher where tname="張旭"));#第三步
22.查詢選修課程的同學(xué)多于2人的教師的姓名(計(jì)數(shù))
示例一自我研發(fā)
select cno from score group by cno having count()>2;#第一步先找出選修多于2人的課程號(hào)
select tno from course where cno=(select cno from score group by cno having count()>2);#第二步找出聯(lián)系
select tname from teacher where tno in( select tno from course where cno in (select cno from score group by cno having count(*)>2));#第三步
23.查詢95033班和95031班全體學(xué)生的記錄
slect * from student where class in("95033","95031");#我感覺這是在侮辱我智商
24.查詢存在85分以上成績(jī)的課程Cno;
select cno,degree from score where degree>85;
25.查詢出“計(jì)算機(jī)系”教師所教課程的成績(jī)表
select tno from teacher where depart="計(jì)算機(jī)系";#第一步先找出計(jì)算機(jī)系的
select cno from course where tno in (select tno from teacher where depart="計(jì)算機(jī)系");#第二步找出其中連接的關(guān)鍵詞
select degree,cno from score where cno in ( select cno from course where tno in (select tno from teacher where depart="計(jì)算機(jī)系"));#最后得出最終的結(jié)果
26.查詢“計(jì)算機(jī)系”與“電子工程系”不同職稱的tname和port,審題有困難 not in/union
select prof from teacher where depart="電子工程系";
select * from teacher where depart="計(jì)算機(jī)系" and prof not in(select prof from teacher where depart="電子工程系") union select * from teacher where depart="電子工程系" and prof not in(select prof from teacher where depart="計(jì)算機(jī)系");#not in 和union用的挺好的
27 查詢選修編號(hào)為“3-105”課程且成績(jī)至少高于選修編號(hào)為“3-245”的同學(xué)的Cno、Sno和Degree,并按degree從高到低排序。any的用法以及排序的用法。
select * from score where cno ="3-105" and degree > any(select degree from score where cno = "3-245")order by degree desc;
28.查詢選修編號(hào)為“3-105”且成績(jī)高于選修編號(hào)為“3-245”課程的同學(xué)的Cno、sno和degree.
且,all的用法
mysql> select * from score where cno ="3-105" and degree > all(select degree from score where cno = "3-245");
29.查詢所有教師和同學(xué)的name、sex、和birthday
union以及as的應(yīng)用
select sname,ssex,sbirthday from student;#先找出學(xué)生的
select tname,tsex,tbirthday from teacher;#在找出老師的
select sname,ssex,sbirthday from student union select tname,tsex,tbirthday from teacher;#利用union進(jìn)行合并
select sname as name,ssex as sex,sbirthday as birthday from student union select tname,tsex,tbirthday from teacher;#利用as對(duì)名稱進(jìn)行修改
30.查詢所用“女”教師和“女”同學(xué)的name,sex,birthday
select sname as name,ssex as sex,sbirthday as birthday from student where ssex="女" union select tname,tsex,tbirthday from teacher where tsex="女";
復(fù)制表數(shù)據(jù)做條件查詢
31.查詢成績(jī)比該課程平均成績(jī)低的同學(xué)此的成績(jī)表
select * from score a where degree
這個(gè)a和b用的妙啊,
???where a.cno=b.cno不太明白
32.查詢所有任課老師的Tname和depart
select tname,depart from teacher where tno in (select tno from course);
條件加分組篩蹄選
33.查詢至少有2名男生的班號(hào)
select class from student where ssex="男" group by class having count()>1; #計(jì)數(shù)的語法不太熟悉,having count()>1返回表中幾輪數(shù)大于1的記錄數(shù)
not like模糊查詢
34.查詢student表中不姓“王”的同學(xué)記錄
mysql> select * from student where sname not like "王%";
year逃數(shù)與now丞數(shù)
35.查詢student表中每個(gè)學(xué)生的姓名和年齡
select year(now());#當(dāng)前年份
select year(sbirthday) from student;#出生的年份
select sname,year(now())-year(sbirthday)as"年齡" from student;#加減乘除還是很有意思的
max與min函數(shù)
36.查詢student表中最大和最小的sbirthday;
select sbirthday from student order by sbirthday;
select max(sbirthday) as "最小",min(sbirthday)as "最小" from student;
多字段排序
37.以班號(hào)和年齡的順序查詢student的全部記錄
select * from student order by class desc,sbirthday;
38.查詢“男”教師及所上的課程
select * from course where tno in (select tno from teacher where tsex="男");
max函數(shù)與子查詢
39.查詢最高分同學(xué)的sno、cno和degree列
select * from score where degree=( select max(degree)from score); #max用的挺好的
40.查詢和“李軍”同性別的所有同學(xué)的sname
select sname from student where ssex=(select ssex from student where sname="李軍");
41.查詢和“李軍”同性別并同班的同學(xué)的sname
select sname from student where ssex=(select ssex from student where sname="李軍") and class = (select class from student where sname="李軍");
42.查詢所有選修“計(jì)算機(jī)導(dǎo)論”課程的“男”同學(xué)的成績(jī)表
select degree from score where cno=(select cno from course where cname ="計(jì)算機(jī)導(dǎo)論")and sno in(select sno from student where ssex="男");
按等級(jí)查詢
43.假設(shè)使用如下命令建立了一個(gè)grade表:
create table grade(
low int(3),
upp int(3),
grade char(1));
insert into grade values(90,100,"A");
insert into grade values(80,89,"B");
insert into grade values(70,79,"C");
insert into grade values(60,69,"D");
insert into grade values(0,59,"E");
現(xiàn)查詢所有同學(xué)的Sno、cno和grade列
mysql> select sno,cno,grade from score,grade where degree between low and upp;#grade用的很棒
SQL的四種連接查詢
連接的優(yōu)點(diǎn):可以不用通過創(chuàng)建外鍵,利用某個(gè)元素相等來求交集
內(nèi)連接
inner join 或者 join
外連接
左連接 left join 或者 left outer join
右連接 right join 或者 right outer join
完全外連接 full join 或者 full outer join
create database testjoin;
person表
id,
name,
cardid
create table person(
id int,
name varchar(20),
cardid int);
card 表
id,
name
create table card(
id int,
name varchar(20));
insert into card values (1,"飯卡");
insert into card values (2,"建行卡");
insert into card values (3,"農(nóng)行卡");
insert into card values (4,"工商卡");
insert into card values (5,"郵政");
insert into person values(1,"張三",1);
insert into person values(1,"李四",3);
insert into person values(1,"王五",6);
1.內(nèi)聯(lián)查詢
兩張表中的數(shù)據(jù),通過某個(gè)字段相對(duì),查詢出相關(guān)記錄數(shù)據(jù)inner join中inner可以省略,on是必須的
select * from person inner join card on person.cardid=card.id;
+------+------+--------+------+--------+
| id | name | cardid | id | name |
+------+------+--------+------+--------+
| 1 | 張三 | 1 | 1 | 飯卡 |
| 1 | 李四 | 3 | 3 | 農(nóng)行卡 |
+------+------+--------+------+--------+
2.left join(左外連接)
會(huì)把左邊表里的所有數(shù)據(jù)提取出來,而右邊表的數(shù)據(jù),如果相等的,就會(huì)顯示出來;如果沒有就會(huì)補(bǔ) NULL
select * from person left (outer)join card on person.cardid=card.id;#outer可有可無
3.right join(右外連接)
會(huì)把右邊表里的所有數(shù)據(jù)提取出來,而左邊表的數(shù)據(jù),如果相等的,就會(huì)顯示出來;如果沒有就會(huì)補(bǔ) NULL
select * from person right (outer)join card on person.cardid=card.id;
4.full join(全外鏈接)
select * from person full join card on person.cardid=card.id;
mysql不支持full join,其等于
select * from person left join card on person.cardid=card.id union select * from person right join card on person.cardid=card.id;
標(biāo)簽:degree,練習(xí),查詢,score,student,cno,where,select
來源: https://www.cnblogs.com/yangzilaing/p/14617265.html
總結(jié)
以上是生活随笔為你收集整理的查询成绩小于85且是计算机的一项应用,查询练习2的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zookeeper入门系列
- 下一篇: 《代码整洁之道姐妹篇》