首页 > 数据库 >sql练习-1

sql练习-1

时间:2023-06-24 10:46:21浏览次数:46  
标签:INSERT -- 练习 score VALUES sql sid sc

-- 创建表

-- CREATE TABLE `course` (

-- `cid` int(3) NOT NULL AUTO_INCREMENT COMMENT '课程编号',

-- `cname` varchar(10) DEFAULT NULL COMMENT '课程名',

-- `tid` int(3) DEFAULT NULL COMMENT '教师编号',

-- PRIMARY KEY (`cid`))

-- ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

 

-- CREATE TABLE `sc` (

-- `sid` int(11) NOT NULL COMMENT '学号',

-- `cid` int(11) NOT NULL COMMENT '课程编号',

-- `score` int(11) DEFAULT NULL COMMENT '分数',

-- PRIMARY KEY (`sid`,`cid`))ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

 

-- CREATE TABLE `student` (

-- `sid` int(4) NOT NULL AUTO_INCREMENT COMMENT '学号',

-- `sname` varchar(10) DEFAULT NULL COMMENT '学生姓名',

-- `sage` int(3) DEFAULT NULL COMMENT '学生年龄',

-- `ssex` varchar(1) DEFAULT NULL COMMENT '学生性别',

-- `birthday` datetime(6) DEFAULT NULL COMMENT '学生生日',

-- PRIMARY KEY (`sid`)

-- ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

--

--

-- CREATE TABLE `teacher` (

-- `tid` int(3) unsigned NOT NULL AUTO_INCREMENT COMMENT '教师编号',

-- `tname` varchar(10) DEFAULT NULL COMMENT '教师姓名',

-- PRIMARY KEY (`tid`)

-- ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- 创建数据

-- INSERT INTO `student` VALUES (1, '刘一', 23, '男','2000-05-01 09:31:25');

-- INSERT INTO `student` VALUES (2, '钱二', 25, '女','1998-03-05 09:31:44');

-- INSERT INTO `student` VALUES (3, '张三', 21, '男','2002-01-01 09:32:08');

-- INSERT INTO `student` VALUES (4, '李四', 22, '女','2001-02-08 15:32:43');

-- INSERT INTO `student` VALUES (5, '王五', 32, '男','1991-04-18 18:33:05');

-- INSERT INTO `student` VALUES (6, '赵六', 34, '女','1989-01-14 23:33:22');

--

--

--

-- INSERT INTO `teacher` VALUES (1, '叶平');

-- INSERT INTO `teacher` VALUES (2, '贺高');

-- INSERT INTO `teacher` VALUES (3, '杨艳');

-- INSERT INTO `teacher` VALUES (4, '周磊');

--

--

-- INSERT INTO `course` VALUES (1, '语文', 1);

-- INSERT INTO `course` VALUES (2, '数学', 2);

-- INSERT INTO `course` VALUES (3, '英语', 3);

-- INSERT INTO `course` VALUES (4, '物理', 4);

-- INSERT INTO `course` VALUES (5, '化学', 5);

--

--

--

-- INSERT INTO `sc`(sid,cid,score) VALUES (1,1,50);

-- INSERT INTO `sc`(sid,cid,score) VALUES (1,2,60);

-- INSERT INTO `sc`(sid,cid,score) VALUES (1,3,70);

-- INSERT INTO `sc`(sid,cid,score) VALUES (1,4,80);

-- INSERT INTO `sc`(sid,cid,score) VALUES (1,5,90);

--

-- INSERT INTO `sc`(sid,cid,score) VALUES (2,1,90);

-- INSERT INTO `sc`(sid,cid,score) VALUES (2,2,80);

-- INSERT INTO `sc`(sid,cid,score) VALUES (2,3,70);

-- INSERT INTO `sc`(sid,cid,score) VALUES (2,4,88);

--

-- INSERT INTO `sc`(sid,cid,score) VALUES (3,1,32);

-- INSERT INTO `sc`(sid,cid,score) VALUES (3,2,82);

-- INSERT INTO `sc`(sid,cid,score) VALUES (3,3,53);

-- INSERT INTO `sc`(sid,cid,score) VALUES (3,4,89);

-- INSERT INTO `sc`(sid,cid,score) VALUES (3,5,99);

--

-- INSERT INTO `sc`(sid,cid,score) VALUES (4,1,92);

-- INSERT INTO `sc`(sid,cid,score) VALUES (4,2,82);

-- INSERT INTO `sc`(sid,cid,score) VALUES (4,3,93);

-- INSERT INTO `sc`(sid,cid,score) VALUES (4,4,89);

-- INSERT INTO `sc`(sid,cid,score) VALUES (4,5,99);

--

--

-- INSERT INTO `sc`(sid,cid,score) VALUES (5,1,93);

-- INSERT INTO `sc`(sid,cid,score) VALUES (5,2,83);

-- INSERT INTO `sc`(sid,cid,score) VALUES (5,3,84);

-- INSERT INTO `sc`(sid,cid,score) VALUES (5,4,86);

-- INSERT INTO `sc`(sid,cid,score) VALUES (5,5,100);

--

--

-- INSERT INTO `sc`(sid,cid,score) VALUES (6,1,55);

-- INSERT INTO `sc`(sid,cid,score) VALUES (6,2,46);

-- INSERT INTO `sc`(sid,cid,score) VALUES (6,3,80);

-- INSERT INTO `sc`(sid,cid,score) VALUES (6,4,60);

-- INSERT INTO `sc`(sid,cid,score) VALUES (6,5,70);

--

1、查询所有学生信息
select * from student;

2、查询年龄大于23岁的学生学号、姓名和性别
select sid,sname,ssex from student;

3、查询年龄在19-27岁之间的学生信息
select * from student where sage between sage 19 and 27;
4、查询出张三、王五、赵六的学生信息
select * from student where sname in ("张三","王五","赵六");

5、查询出性别为“男”,且年龄大于25的学生信息
select * from student where ssex = "男" and sage > 25;

6、查询出姓“王”的学生的所有课程的分数
select sc.cid,sc.score from student
join sc on student.sid = sc.sid where student.sname like "王%"


7、查询出每个学生的平均分且排行
select student.sname,avg(sc.score) from student
join sc on student.sid = sc.sid group by sc.sid order by avg(sc.score) desc

8、查询出“语文”成绩的最高分
select max(sc.score) from sc
join course on sc.cid = course.cid where course.cname = "语文"


9、查询出总分为第四名的学生信息
select student.*,sum(sc.score) from student
join sc on student.sid = sc.sid
GROUP BY student.sid order by sum(sc.score) desc limit 3,1


10、查询出哪个老师所带的课程总分最高
select teacher.tname,sum(sc.score) from sc
join course on sc.cid = course.cid join teacher on course.tid = teacher.tid
GROUP BY sc.cid order by sum(sc.score) desc limit 1


11、查询出总成绩高于张三的每个学生的总分且成绩排行
select student.sname, sum(sc.score) from student
join sc on student.sid = sc.sid group by sc.sid
having sum(sc.score) >
(select sum(sc.score) from student
join sc on student.sid = sc.sid where student.sname = "张三")

 

12、查询出选修课程数量小于5门课的学生信息
select student.* from student
join sc on student.sid = sc.sid group by sc.sid HAVING count(sc.cid) <5


13、查询出总成绩最低的学生姓名、学号和年龄
select student.sname,student.sid,student.sage,sum(sc.score) from student
join sc on student.sid = sc.sid
group by sc.sid order by sum(sc.score) asc limit 1


14、查询出哪些学生的平均成绩高于75分
select student.sname,avg(sc.score) from student
join sc on student.sid = sc.sid
group by sc.sid having avg(sc.score)>75

 

15、查询出成绩第一名的学生信息、和各科分数
select student.*,course.cname,sc.score from student
join sc on student.sid = sc.sid join course on sc.cid = course.cid
where student.sid =
(select sc.sid from student
join sc on student.sid = sc.sid join course on sc.cid = course.cid
GROUP BY sc.sid ORDER BY sum(sc.score) desc limit 1)

 

16、查询出男生和女生各多少人
SELECT ssex,count(ssex) from student group by ssex


17、查询出女生的平均分、总分
select AVG(sc.score),sum(sc.score) from student
join sc on student.sid = sc.sid
where student.ssex = "女"

 

18、查询出姓名一样的人的有几个
select sname,count(sname) from student group by sname having count(sname) >1;


19、查询出没有成绩的学生有哪些人
select student.sid,student.sname from student
left join sc on student.sid = sc.sid where sc.sid is null

20、查询出哪一个老师没有任何课程
select teacher.tid,teacher.tname from teacher
left join course on teacher.tid = course.tid
where course.tid is null

标签:INSERT,--,练习,score,VALUES,sql,sid,sc
From: https://www.cnblogs.com/zlbstruggl/p/17500779.html

相关文章

  • sql练习-2
    2023-6-19sql语句----以下题目涉及到的表有--ecs_users(用户表)、--ecs_order_info(订单表)、--ecs_goods(商品表)、--ecs_goods_type(商品类型表)--usetest;select*fromecs_users;select*fromecs_goods;select*fromecs_order_info;select*fromecs_order_......
  • 一天吃透MySQL面试八股文
    内容摘自我的学习网站:topjavaer.cn什么是MySQLMySQL是一个关系型数据库,它采用表的形式来存储数据。你可以理解成是Excel表格,既然是表的形式存储数据,就有表结构(行和列)。行代表每一行数据,列代表该行中的每个值。列上的值是有数据类型的,比如:整数、字符串、日期等等。数据库的三......
  • MySQL的wait_timeout 参数 set global 设置不生效
    MySQL服务连接数突然暴增,登录服务查看大都是sleep进程,并且1分钟会启用一个新的连接,紧急处理方案是需要手动去释放连接数。登录服务器查看当前超时时间mysql>showvariableslike'%timeout%';+-----------------------------+----------+|Variable_name|Valu......
  • MySQL 中常见的高可用架构部署方案
    MySQL中常见的高可用架构部署方案IT奋斗的青年 2023-06-0608:36 发表于山东收录于合集#mysql17个MySQL中的集群部署方案前言这里来聊聊,MySQL中常用的部署方案。MySQLReplicationMySQLReplication 是官方提供的主从同步方案,用于将一个MySQL的实例同步到另......
  • PostgreSQL 配置远程访问
    PostgreSQL安装完毕后需要设置客户端远程访问,具体步骤参考如下postgresql.conf找到配置文件目录[root@hadoop201pgsql]#locateostgresql.conf/usr/pgsql-15/share/postgresql.conf.sample/var/lib/pgsql/15/data/postgresql.conf打开文件进行编辑[root@hadoop201pgs......
  • mysql的读写分离
    读写分离的作用和实现的方式实验环境:上文的主从复制,以及新增了一台客户端进入主mysql数据库创建读写分离账号查看建立的读写分离账号挂载云计算光盘移动解压jdk一直回车后看到此询问输入yes将Java环境从1.8.0_131更改为1.6.0_14修改环境变量添加此三行数据移动jdk安装位置更新环境......
  • 通过Maxwell同步mysql数据至kafka
    实验环境本地虚拟机maraidb10.8.8kafka2.12-3.3.1maxwell由容器部署1mariadb1.1配置log_bin配置文件中加入如下内容server-id=111log_bin=mysql-binbinlog_format=ROWexpire_logs_days=1重启服务systemctlrestartmariadb查询命令SHOWVARIABLESLI......
  • debezium同步mysql数据至kafka(未完待续)
    实验环境全部部署于本地虚拟机1mysql参考官方文档和根据官方示例镜像(debezium/example-mysql,mysql版本为8.0.32)1.1创建用户官方镜像里一共有三个账号debezium:connect用户mysqluser:普通用户replicator:用于主从?设置命令createuser'debezium'@'%'identifiedby"db......
  • Scala练习
    wordCountpackagecom.doit.day03importscala.io.{BufferedSource,Source}objectWordCountDemo{defmain(args:Array[String]):Unit={//读取文件,获取到一个Source对象valsource:BufferedSource=Source.fromFile("D:\\develop\\ideaWorkSpace\\......
  • DVWA靶场之SQL注入通关详解
    原理SQL注入通过将恶意的SQL代码插入到应用程序后台的SQL语句中,以获取未授权的访问权限或者窃取应用程序中的敏感信息。通常,SQL注入攻击的目标是Web应用程序,因为Web应用程序通常需要与数据库进行交互,并且大多数Web应用程序使用的是SQL语言。存在原因Web应用程序没有对用户输入......