美文网首页
MySQL练习题(二)

MySQL练习题(二)

作者: 龙小江i | 来源:发表于2018-10-17 19:10 被阅读0次

原始数据

# 创建习题数据库
create database school;
# 使用习题数据库
use school;
# 创建学生信息表
create table student
(
id int(3) not null unique auto_increment comment '学号',
name varchar(4) not null comment '姓名',
sex char(1) comment '性别',
birth year comment '出生年份',
department varchar(4) not null comment '院系',
address varchar(6) comment '家庭住址',
primary key(id)
)
char set utf8;
# 创建学生成绩表
create table score
(
id int(2) not null unique auto_increment comment '编号',
stu_id int(3) not null comment '学号',
c_name varchar(3) comment '课程名',
grade int(3) comment '分数',
primary key(id)
)
char set utf8;
# 学生信息表插值
insert into student values
(901,'张老大','男','1985','计算机系','北京市海淀区'),
(902,'张老二','男','1986','中文系','北京市昌平区'),
(903,'张三','女','1990','中文系','湖南省永州市'),
(904,'李四','男','1990','英语系','辽宁省阜新市'),
(905,'王五','女','1991','英语系','福建省厦门市'),
(906,'王六','男','1988','计算机系','湖南省衡阳市');
# 学生成绩表插值
insert into score values
(null,901,'计算机',98),
(null,901,'英语',80),
(null,902,'计算机',65),
(null,902,'中文',88),
(null,903,'中文',95),
(null,904,'计算机',70),
(null,904,'英语',92),
(null,905,'英语',94),
(null,906,'计算机',90),
(null,906,'英语',85);

查询练习

  1. 查询student表的第2条到4条记录
select * from student limit 1,3;
  1. 查询计算机系和英语系的学生的信息
select * from student
where department = '计算机系' or department = '英语系';
  1. 从student表中查询每个院系年龄18~30岁的学生信息
select * from student
where year(curdate())-birth between 18 and 30
order by id;
  1. 查询计算机成绩低于95的学生信息
# 方法一: 左连接
select * from student
left join score on student.id = score.stu_id
where score.grade < 95 and score.c_name = '计算机'
group by student.id
order by student.id;
# 方法二: 子查询
select * from student
where id in
(select stu_id from score
where c_name = '计算机' and grade < 95);
  1. 查询同时参加计算机和英语考试的学生的信息
select * from student
where id in
(select stu_id from score
where c_name in ('计算机','英语')
group by stu_id having count(stu_id) = 2);

相关文章

  • 数据库语言杂记

    MySQL ORDER BY 排序 IF 及 IN 字符串连接函数concat() MySQL练习题:练习题一 ...

  • 2020 -7-23

    mysql 练习题1、mysql中的varchar 和 char 有什么区别?二者存储长度不同,char 是不可变...

  • MySQL练习题(二)

    原始数据 查询练习 查询student表的第2条到4条记录 查询计算机系和英语系的学生的信息 从student表中...

  • 数据蛙第九期就业班 2020/7/23

    MYSQL练习题 1、MySQL中的varchar和char有什么区别? 1、CHAR的长度是固定的,而VARCH...

  • Mysql练习题-50

    Mysql 练习题 文章来自网络,仅供自学 我使用的Mysql版本是5.7.19。答案可能会因版本会有少许出入。 ...

  • 7.27 mysql练习

    mysql练习题目: GROUP BY 语句 基本用法: GROUP BY 语句中的GROUP_CONCAT()函...

  • 10-16练习题

    MySQL练习题 题目1 问题1:如何暂停或开启mysql服务? 按win+r打开,输入cmd,快速打开命令行界面...

  • LeetCode-mysql练习题

    leetcode-mysql练习题总结: 老师指路->https://www.jianshu.com/u/989c...

  • mysql练习题

    端口登录 3306或3308

  • MySQL 练习题

    01第一天20180703 【SQL练习】经典SQL练习题 02第二天20180704 sql语句练习50题(My...

网友评论

      本文标题:MySQL练习题(二)

      本文链接:https://www.haomeiwen.com/subject/dlorzftx.html