查询成绩小于85且是计算机的一项应用,查询练习2

查询练习

创建表

学生表

student

学号

姓名

性别

出生日期

班级

create table student(

sno varchar(20) primary key,

sname varchar(20) not null,

ssex varchar(10)not null,

sbirthday datetime,

class varchar(20));

课程表

Course

课程号

课程名称

教师编号

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

学号

课程号

成绩

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

编号

名字

性别

出生日期

职称

部门

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);

插入数据

添加学生信息

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','李诚','男','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-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');

查询练习

1.查询student表的所有记录。

select * from student;# * 表示所有字段

2.查询student表中的所有记录的sname,ssex和class列。

select sname,ssex,class from student;

3.查询教师所有单位及不重复的depart列。

select distinct depart from teacher; #distinct:排除重复

4、查询score表中成绩在60到80之间的所有记录。

查询区间 between....and......

select * from score where degree between 60 and 80;

select * from score where degree > 60 and degree<80;#运算符表示

5、查询score表中成绩为85,86或88的记录。

表示或者关系的查询 in

select * from score where degree in(85,86,88);

6、查询student表中“95031"班或性别为“女”的同学记录。#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; #默认升序

select * from student order by class asc;#升序

8、以cno升序、degree降序查询score表的所有记录。#先以cno为升序,遇见相同的异score为降序

select * from score order by cno asc,degree desc;

9、查询“95031"班的学生人数。

统计 count

select count(*) from student where class="95031";

10、查询score表中的最高分的学生学号和课程号。(子查询或者排序)

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表示查几条

查询练习

11.查询每门课的平均成绩

select * from course;

avg 求平均值

select avg(degree) from score where cno ="3-105";#一门课程

select cno,avg(degree) from score group by cno;#先利用group进行分组

12.查询score表中至少有2名学生选修的并以3开头的课程的平均分数

select cno,avg(degree),count(*) from score group by cno having count(cno)>=2 and cno like "3%";#like%3以3为开头的

13.查询分数大于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.查询所有学生的sname、cno、degree列

示例一:进行分开查询,其中sno在两个表中是存在交集的

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 |

+-----+-------+--------+

示例二,将两个查询进行合并

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.查询所有学生的sno,cname,和degree列。

示例一先分别表示

select cno,cname from course;

+-------+------------+

| cno | cname |

+-------+------------+

| 3-105 | 计算机导论 |

| 3-245 | 操作系统 |

| 6-166 | 数字电路 |

| 9-888 | 高等数学 |

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.三联表查询

查询所有学生的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 |

+------------+-------+

| 计算机导论 | 3-105 |

| 操作系统 | 3-245 |

| 数字电路 | 6-166 |

| 高等数学 | 9-888 |

+------------+-------+

select sname,cname,degree from score,student,course where course.cno=score.cno and student.sno=score.sno;

+--------+------------+--------+

| sname | cname | degree |

+--------+------------+--------+

| 王丽 | 计算机导论 | 92 |

| 王丽 | 操作系统 | 86 |

| 王丽 | 数字电路 | 85 |

| 王芳 | 计算机导论 | 88 |

| 王芳 | 操作系统 | 75 |

| 王芳 | 数字电路 | 79 |

| 赵铁柱 | 计算机导论 | 76 |

| 赵铁柱 | 操作系统 | 68 |

| 赵铁柱 | 数字电路 | 81 |

+--------+------------+--------+

我TM就是个天才

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 语句需要学习

子查询

17.子查询加分组求平均分

查询“95031”班学生每门课的平均分

示例一,先进行拆分的表示

select * from student where class="95031";

mysql> select * from score where sno in(select sno from student where class="95031");#此处的in是非常值得学习的,筛出来的必须是同一样的

mysql> select cno,avg(degree) from score where sno in(select sno from student where class="95031") group by cno;#此处的group用的也非常棒

TM逻辑鬼才啊,反正就是抽丝剥茧,反复练习,看好最小范围

18.子查询

查询选修“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");

19.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录

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.查询学号为108、101的同学出生的所有学生的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);

进一步进行查询

select * from student where year(sbirthday) in (select year(sbirthday) from student where sno in (108,101));

21.查询“张旭”教师任课的学生成绩

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.查询选修课程的同学多于2人的教师的姓名(计数)

示例一自我研发

select cno from score group by cno having count()>2;#第一步先找出选修多于2人的课程号

select tno from course where cno=(select cno from score group by cno having count()>2);#第二步找出联系

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班全体学生的记录

slect * from student where class in("95033","95031");#我感觉这是在侮辱我智商

24.查询存在85分以上成绩的课程Cno;

select cno,degree from score where degree>85;

25.查询出“计算机系”教师所教课程的成绩表

select tno from teacher where depart="计算机系";#第一步先找出计算机系的

select cno from course where tno in (select tno from teacher where depart="计算机系");#第二步找出其中连接的关键词

select degree,cno from score where cno in ( select cno from course where tno in (select tno from teacher where depart="计算机系"));#最后得出最终的结果

26.查询“计算机系”与“电子工程系”不同职称的tname和port,审题有困难 not in/union

select prof from teacher where depart="电子工程系";

select * from teacher where depart="计算机系" 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="计算机系");#not in 和union用的挺好的

27 查询选修编号为“3-105”课程且成绩至少高于选修编号为“3-245”的同学的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.查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、sno和degree.

且,all的用法

mysql> select * from score where cno ="3-105" and degree > all(select degree from score where cno = "3-245");

29.查询所有教师和同学的name、sex、和birthday

union以及as的应用

select sname,ssex,sbirthday from student;#先找出学生的

select tname,tsex,tbirthday from teacher;#在找出老师的

select sname,ssex,sbirthday from student union select tname,tsex,tbirthday from teacher;#利用union进行合并

select sname as name,ssex as sex,sbirthday as birthday from student union select tname,tsex,tbirthday from teacher;#利用as对名称进行修改

30.查询所用“女”教师和“女”同学的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="女";

复制表数据做条件查询

31.查询成绩比该课程平均成绩低的同学此的成绩表

select * from score a where degree

这个a和b用的妙啊,

???where a.cno=b.cno不太明白

32.查询所有任课老师的Tname和depart

select tname,depart from teacher where tno in (select tno from course);

条件加分组筛蹄选

33.查询至少有2名男生的班号

select class from student where ssex="男" group by class having count()>1; #计数的语法不太熟悉,having count()>1返回表中几轮数大于1的记录数

not like模糊查询

34.查询student表中不姓“王”的同学记录

mysql> select * from student where sname not like "王%";

year逃数与now丞数

35.查询student表中每个学生的姓名和年龄

select year(now());#当前年份

select year(sbirthday) from student;#出生的年份

select sname,year(now())-year(sbirthday)as"年龄" from student;#加减乘除还是很有意思的

max与min函数

36.查询student表中最大和最小的sbirthday;

select sbirthday from student order by sbirthday;

select max(sbirthday) as "最小",min(sbirthday)as "最小" from student;

多字段排序

37.以班号和年龄的顺序查询student的全部记录

select * from student order by class desc,sbirthday;

38.查询“男”教师及所上的课程

select * from course where tno in (select tno from teacher where tsex="男");

max函数与子查询

39.查询最高分同学的sno、cno和degree列

select * from score where degree=( select max(degree)from score); #max用的挺好的

40.查询和“李军”同性别的所有同学的sname

select sname from student where ssex=(select ssex from student where sname="李军");

41.查询和“李军”同性别并同班的同学的sname

select sname from student where ssex=(select ssex from student where sname="李军") and class = (select class from student where sname="李军");

42.查询所有选修“计算机导论”课程的“男”同学的成绩表

select degree from score where cno=(select cno from course where cname ="计算机导论")and sno in(select sno from student where ssex="男");

按等级查询

43.假设使用如下命令建立了一个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");

现查询所有同学的Sno、cno和grade列

mysql> select sno,cno,grade from score,grade where degree between low and upp;#grade用的很棒

SQL的四种连接查询

连接的优点:可以不用通过创建外键,利用某个元素相等来求交集

内连接

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,"农行卡");

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.内联查询

两张表中的数据,通过某个字段相对,查询出相关记录数据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 | 农行卡 |

+------+------+--------+------+--------+

2.left join(左外连接)

会把左边表里的所有数据提取出来,而右边表的数据,如果相等的,就会显示出来;如果没有就会补 NULL

select * from person left (outer)join card on person.cardid=card.id;#outer可有可无

3.right join(右外连接)

会把右边表里的所有数据提取出来,而左边表的数据,如果相等的,就会显示出来;如果没有就会补 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;

标签:degree,练习,查询,score,student,cno,where,select

来源: https://www.cnblogs.com/yangzilaing/p/14617265.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/429636.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

NTP时间服务器

1. NTP简介NTP&#xff08;Network Time Protocol&#xff0c;网络时间协议&#xff09;是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC&#xff0c;其精度在局域网内可达0.1ms&#xff0c;在互联网上绝大多数的地方其精度可以达…

计算机高办报名时间,前方高能!计算机信息技术证报名入口、考试时间已发布...

前方高能&#xff01;计算机信息技术证报名入口、考试时间已发布和发达***相比&#xff0c;我国的计算机信息技术在实际应用的过程中&#xff0c;还存在着许多的局限性&#xff0c;这就使其在使用是计算机信息技术的应用效果无法达到理想的状态。计算机信息技术证报名入口、考试…

API文档工具-Swagger的集成

最近安装了API文档工具swagger&#xff0c;因为Github上已有详细安装教程&#xff0c;且安装过程中没有碰到大的阻碍&#xff0c;所以此文仅对这次安装做一份大致记录 相关网站 Swagger 官方地址&#xff1a;http://swagger.wordnik.com Github安装详解【springmvc集成swagger】…

计算机翻译辅助工具安卓版,计算机辅助翻译软件

OmegaT是一款电脑翻译软件&#xff0c;此软件能够帮助用户对一些机器专业语言进行快速翻译&#xff0c;目前软件支持任何Java的操作系统&#xff0c;用户无需担心系统不支持的问题。另外&#xff0c;软件可以在任意目录中搜索所支持的格式的文件&#xff0c;翻译速度快、准确性…

计算机应用基础试模块5ACCSE,2015年计算机二级《Access》上机最后冲刺卷(1)

二、基本操作题41在考生文件夹下的“Acc1.mdb”数据库中已建立表对象“职工”。试按以下操作要求&#xff0c;完成对表“职工”的编辑修改和操作&#xff1a;(1)将“职工号”字段改名为“编号”&#xff0c;并设置为主键。(2)设置“年龄”字段的有效性规则为“年龄>20”。(3…

情人节引发的血案

首先&#xff0c; 如果你能看到这句话&#xff0c;那我就应该恭喜你&#xff0c;你已经被此文的标题所吸引。不过&#xff0c;千万不要想太多&#xff0c;此文不是什么《今日说法》&#xff0c;但也与法有那么一丁点的关系&#xff1b;此文也不是什么《我们约会吧》&#xff0c…

css 可编辑,如何设置DIV可编辑

「来源: &#xff5c;web前端开发 ID&#xff1a;web_qdkf」如何让一个div变成可编辑状态&#xff0c;比如富文本的输入框就可以用可编辑的div(自定义一个富文本时可用)&#xff0c;类似textare。有2种方案可以实现&#xff1a;1是通过contenteditable属性设置为true&#xff0…

Java中的局部变量表及使用jclasslib进行查看

直接上下载地址 jclasslib是一个独立的工具&#xff0c;不是包含在JDK中的工具&#xff0c;需要自己进行下载&#xff0c;下载地址如下&#xff1a; http://downfile.downcc.com/down/JClassLib_windows.zip 什么是局部变量表 在《java中的栈》中我们说到了一个栈帧至少需要包含…

在线学ajax,ajax学习

AJAX&#xff1a;1. 概念&#xff1a; ASynchronous JavaScript And XML异步的JavaScript 和 XML1. 异步和同步&#xff1a;客户端和服务器端相互通信的基础上* 客户端必须等待服务器端的响应。在等待的期间客户端不能做其他操作。* 客户端不需要等待服务器端的响应。在服务器处…

绝对定位和浮动的区别和运用

当一个元素使用绝对定位后&#xff0c;它的位置将依据浏览器左上角开始计算或相对于父容器&#xff08;在父容器使用相对定位时&#xff09;。 绝对定位使元素脱离文档流&#xff0c;因此不占据空间。普通文档流中元素的布局就当绝对定位的元素不存在时一样。因为绝对定位的框与…

服务器网盘系统怎么装,云服务器上怎么安装操作系统

云服务器上怎么安装操作系统 内容精选换一换安装传输工具在本地主机和Windows云服务器上分别安装数据传输工具&#xff0c;将文件上传到云服务器。例如QQ.exe。在本地主机和Windows云服务器上分别安装数据传输工具&#xff0c;将文件上传到云服务器。例如QQ.exe。本地磁盘映射(…

Ubuntu 16.04 64位安装YouCompleteMe

之前记录在OneNote上感觉有点乱&#xff0c;而且不适合保存shell&#xff0c;这次重新安装又出问题了&#xff0c;干脆写篇博客记录。 从零开始 1、git&#xff08;用来下载vim和相关插件&#xff09; sudo apt-get install git2、cmake&#xff08;用来编译clang-llvm&#xf…

学大数据找IT十八掌

《IT十八掌大数据内功修炼到企业实战2.0课程》免费自学马拉松计划 1、关于十八掌 学了大数据&#xff0c;还是不敢找工作&#xff1f; 内功不够&#xff01;跟随十八掌掌门徐培成炼内功&#xff01; 十八掌教育努力打造一套地表最强【大数据云计算】内功修炼系列课程&#xff…

OSG设置警告等级

osg::setNotifyLevel(osg::FATAL);//控制台只输出严重错误信息转载于:https://www.cnblogs.com/coolbear/p/6420494.html

spring源码分析2本最高清带书目PDF百度网盘分享

SPRING技术内幕__深入解析SPRING架构与设计原理完整版 Spring源码深度解析 [郝佳编著][人民邮电出版社][2013.09][386页] 链接&#xff1a;http://pan.baidu.com/s/1kVQDcIN 密码&#xff1a;u3od 转载于:https://www.cnblogs.com/kool/p/6695530.html

POJ2891 Strange Way to Express Integers (扩展欧几里德)

本文为博主原创文章&#xff0c;欢迎转载&#xff0c;请注明出处 www.cnblogs.com/yangyaojia 题目大意 &#xfeff;求解一组同余方程 x ≡ r1 (mod a1) x ≡ r2 (mod a2) x ≡ r3 (mod a3) ...... x ≡ rk (mod ak) 的解x&#xff08;a1,a2,a3,.....ak 并不一定互质&#xff…

js中的回调函数的理解和使用方法

一. 回调函数的作用 js代码会至上而下一条线执行下去&#xff0c;但是有时候我们需要等到一个操作结束之后再进行下一个操作&#xff0c;这时候就需要用到回调函数。 二. 回调函数的解释 因为函数实际上是一种对象&#xff0c;它可以存储在变量中&#xff0c;通过参数传递给另一…

C语言中的小数取整和四舍五入

将小数直接抹掉的取整1自动类型转换 例如 &#xff1a;#include <stdio.h> main() {int a;a2.7;printf("a %d",a); } // a 22强制类型转换例如&#xff1a;#include <stdio.h> main() {printf("%d",(int)3.75); //输出结果为 …

sql exist 优化查询时间

1.非exist&#xff0c;查询需要20多秒 2.使用exist后 3.表连接也能优化 转载于:https://www.cnblogs.com/alamZ/p/6423166.html

1-4 数组元素的区间删除 (20 分)

题目&#xff1a; 给定一个顺序存储的线性表&#xff0c;请设计一个函数删除所有值大于min而且小于max的元素。删除后表中剩余元素保持顺序存储&#xff0c;并且相对位置不能改变。 函数接口定义&#xff1a; int Delete( int A[], int L, int minA, int maxA );其中A是整型数…