mysql复杂查询练习

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:
  1. 在teacher表中查询与刘伟教师职称相同的教师号、姓名

拓展练习:在course表中检索与“计算机组成原理”先选课相同的课程的cno,cname,cpno

mysql> select cno,cname,cpno from course where cpno=
    -> (select cpno from course where cname='计算机组成原理');
+-----+-----------------------+------+
| cno | cname                 | cpno |
+-----+-----------------------+------+
| kc3 | 微机原理              | kc1  |
| kc4 | 计算机组成原理        | kc1  |
+-----+-----------------------+------+
2 rows in set (0.00 sec)
  1. 在teacher表与tc表中查询讲授课程号为kc2的教师姓名

mysql> select sno,cno,grade from sc
    -> where sno=any(select sno from student where sdept=
    -> (select sdept from student where sname='李涛')
    -> );
+-----+-----+-------+
| sno | cno | grade |
+-----+-----+-------+
| st2 | kc2 |    88 |
| st2 | kc3 |    92 |
| st3 | kc1 |    85 |
| st3 | kc2 |    90 |
| st3 | kc3 |    60 |
+-----+-----+-------+
5 rows in set (0.00 sec)

不包含李涛的学生选课信息:

mysql> select sno,cno,grade from sc 
    -> where sno=any(select sno from student where sdept=
    ->  (select sdept from student where sname='李涛')
    -> and sname!='李涛');        -- 注意and在哪层子查询内
+-----+-----+-------+
| sno | cno | grade |
+-----+-----+-------+
| st3 | kc1 |    85 |
| st3 | kc2 |    90 |
| st3 | kc3 |    60 |
+-----+-----+-------+
3 rows in set (0.00 sec)
  1. 在teacher表中查询其他系中比数学系所有教师工资都高的教师的姓名和工资。

拓展练习:在student表中查询所有比信息工程系年龄大的学生的sname、sage

mysql> select sname,sage from student where sage>all
    ->  (select sage from student where sdept='信息工程')
    ->  and sdept!='信息工程';
+-----------+------+
| sname     | sage |
+-----------+------+
| 张飞      |   21 |
| 刘鹏程    |   21 |
+-----------+------+
2 rows in set (0.00 sec)

或者使用max关键字:

mysql> select sname,sage from student where sage>
    -> (select max(sage) from student where sdept='信息工程')
    -> and sdept!='信息工程';
+-----------+------+
| sname      | sage  |
+-----------+------+
| 张飞       |   21  |
| 刘鹏程     |   21  |
+-----------+------+
2 rows in set (0.00 sec)

4.在student表中查询与“”李涛”不在同一系的学生信息

mysql> select * from student
    -> where sdept not in 
    -> (select sdept from student where sname='李涛');
+-----+-----------+------+------+--------------+
| sno | sname     | ssex | sage | sdept        |
+-----+-----------+------+------+--------------+
| st1 | 杨云      | 女   |   20 | 信息工程     |
| st4 | 张飞      | 男   |   21 | 体育         |
| st5 | 刘备      | 男   |   20 | 中文         |
| st6 | 赵梦      | 女   |   20 | 音乐         |
| st9 | 刘鹏程    | 男   |   21 | 音乐         |
+-----+-----------+------+------+--------------+
5 rows in set (0.01 sec)

5.查询与“”刘备”一同上课的学生学号和姓名

mysql> select sno,sname from student where sno in
    -> (select sno from sc where cno in
    ->  (select cno from sc where sno in
    ->   (select sno from student where sname='刘备')
    ->  )
    -> );
+-----+--------+
| sno | sname  |
+-----+--------+
| st1 | 杨云   |
| st2 | 李涛   |
| st3 | 王伟   |
| st5 | 刘备   |
+-----+--------+
4 rows in set (0.01 sec)

或:

mysql> select distinct student.sno as 学号,sname as 姓名
    -> from student,sc
    -> where student.sno=sc.sno and cno in
    -> (select cno from sc,student where sc.sno=
    -> student.sno and student.sno in 
    -> (select sno from student where student.sname='刘备')
    -> );
+--------+--------+
| 学号   | 姓名   |
+--------+--------+
| st1    | 杨云   |
| st2    | 李涛   |
| st3    | 王伟   |
| st5    | 刘备   |
+--------+--------+
4 rows in set (0.00 sec)
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
16天前
|
关系型数据库 MySQL 数据库
轻松入门MySQL:精准查询,巧用WHERE与HAVING,数据库查询如虎添翼(7)
轻松入门MySQL:精准查询,巧用WHERE与HAVING,数据库查询如虎添翼(7)
|
16天前
|
缓存 关系型数据库 MySQL
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
|
16天前
|
缓存 关系型数据库 MySQL
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
MySQL 查询优化:提速查询效率的13大秘籍(索引设计、查询优化、缓存策略、子查询优化以及定期表分析和优化)(中)
|
2天前
|
关系型数据库 MySQL Shell
MySQL 查询
MySQL 查询
|
4天前
|
SQL 关系型数据库 MySQL
DQL语言之基础查询(mysql)
DQL语言之基础查询(mysql)
|
4天前
|
SQL 关系型数据库 MySQL
DQL语言之连接查询(mysql)
DQL语言之连接查询(mysql)
|
4天前
|
关系型数据库 MySQL
MySQL全局库表查询准确定位字段
information_schema.COLUMNS 详细信息查询
183 4
|
9天前
|
关系型数据库 MySQL
Mysql查询语句的执行顺序
Mysql查询语句的执行顺序
10 0
|
11天前
|
SQL 关系型数据库 MySQL
mysql多表查询、函数查询
mysql多表查询、函数查询
|
11天前
|
SQL 关系型数据库 MySQL
mysql基本查询、运算符、排序和分页
mysql基本查询、运算符、排序和分页