子查询

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介: 子查询

(1)查询计算机系学生的姓名、年龄、性别;
mysql> select sname,sage,ssex

-> from student
-> where dno = (select dno
->              from dept
->              where dname = '计算机系');

(2)查询刘晨的平均成绩;
mysql> select avg(grade)

-> from sc
-> where sno = (select sno
->              from student
->              where sname = '刘晨');

(3)查询从未选修过课程的男学生的姓名;
mysql> select sname

-> from student
-> where ssex = '男' and sno not in(select sno from sc);

(4)查询选修了C01号课程或C02号课程的学生姓名;

mysql> select sname
-> from student
-> where sno in(select sno
->              from sc
->              where cno = 'c01' or cno = 'c02');

(5)查询所有选修了“数据库”课程的学生的学号和姓名;
mysql> select sno,sname

-> from student
-> where sno in(select sno
->               from sc
->               where cno = (select cno
->                           from course
->                           where cname = '数据库'));

(6)查询至少选修了3门课的学生的姓名;
mysql> select sname

-> from student
-> where sno in    (select sno
->               from sc
->               group by sno
->               having count(cno) >=3);

(7)查询选修了C03号课程且成绩在85分以上的所有学生学号和姓名;
mysql> select sno,sname

-> from student
-> where sno in(select sno
->              from sc
->              where cno = 'c03' and grade > 85);

(8)查询数据库课程中大于该课平均成绩的学生学号;
select sno
from sc
where cno = (select cno from course where cname = '数据库')

 and
 grade > (select avg(grade) from sc where cno = (select cno from course where cname = '数据库'));

(9)查询和刘晨在同一个系的学生姓名(不包括刘晨);
mysql> select sname

-> from student
-> where sname <> '刘晨' and dno = (select dno
->                                      from student
->                                  where sname = '刘晨');

(10)将女同学的成绩提高5%;
mysql> update sc

-> set grade = grade * 1.05
-> where sno in (select sno
->               from student
->               where ssex = '女');

(11)删除刘晨的选课记录
mysql> delete from sc

-> where sno = (select sno
->             from student
->             where sname = '刘晨');

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3月前
(头哥)多表查询与子查询
(头哥)多表查询与子查询
49 0
|
3月前
|
SQL
深入了解关联查询和子查询
深入了解关联查询和子查询
26 0
|
4月前
聚合函数、子查询
聚合函数、子查询
|
6月前
|
SQL 关系型数据库 MySQL
第9章_子查询
第9章_子查询
46 0
|
11月前
|
SQL 索引
相关子查询
相关子查询
173 0
|
11月前
|
SQL 关系型数据库 MySQL
第09章_子查询
第09章_子查询
71 0
|
11月前
|
SQL
联合查询和子查询
联合查询和子查询
|
数据库管理
子查询(2)
如果子查询的执行依赖于外部查询,通常情况下都是因为子查询中的表用到了外部的表,并进行了条件关联,因此每执行一次外部查询,子查询都要重新计算一次,这样的子查询就称之为 关联子查询 。相关子查询按照一行接一行的顺序执行,主查询的每一行都执行一次子查询。
子查询(2)
|
SQL 关系型数据库 MySQL
子查询(1)
子查询指一个查询语句嵌套在另一个查询语句内部的查询,这个特性从MySQL 4.1开始引入。
子查询(1)
|
开发者
子查询 | 学习笔记
快速学习子查询。
子查询 | 学习笔记