子查询练习

create database xuexiao
go

use xuexiao
go

--表一:学生表   student
--学号:code   int        (主键)从1开始
--姓名:name  varchar(50)
--性别:sex        char(10)
--班级:banji    char(10)
--语文教师编号:yujiao        int
--数学教师编号:yujiao        int
--英语教师编号:yujiao        int
create table student
(
 scode int primary key identity(10001,1)not null,--主键
 name varchar(50),
 sex        char(10),
 banji     char(10),
 yujiao        int    ,--外键
 shujiao    int,--外键
 yingjiao   int,--外键
)
--插入学生表数据
--一班数据
insert into student values(‘王红‘,‘女‘,‘一班‘,1001,1004,1007)
insert into student values(‘周瑜‘,‘男‘,‘一班‘,1001,1004,1007)
insert into student values(‘张飞‘,‘男‘,‘一班‘,1001,1004,1007)
insert into student values(‘乔雨‘,‘女‘,‘一班‘,1001,1004,1007)
insert into student values(‘王发‘,‘女‘,‘一班‘,1001,1004,1007)
--二班数据
insert into student values(‘杜鹃‘,‘女‘,‘二班‘,1002,1005,1008)
insert into student values(‘白小飞‘,‘男‘,‘二班‘,1002,1005,1008)
insert into student values(‘刘小峰‘,‘男‘,‘二班‘,1002,1005,1008)
insert into student values(‘张强‘,‘男‘,‘二班‘,1002,1005,1008)
insert into student values(‘周莹莹‘,‘女‘,‘二班‘,1002,1005,1008)
--三班数据
insert into student values(‘赵晓红‘,‘女‘,‘三班‘,1003,1006,1009)
insert into student values(‘王国章‘,‘男‘,‘三班‘,1003,1006,1009)
insert into student values(‘张三更‘,‘男‘,‘三班‘,1003,1006,1009)
insert into student values(‘潘晓丽‘,‘女‘,‘三班‘,1003,1006,1009)
insert into student values(‘楚红红‘,‘女‘,‘三班‘,1003,1006,1009)

--表二:教师表        teacher
--教师编号:code        int    (主键)            从1001开始
--负责课程:lesson        char(10)(语文、数学、英语)
--年龄:age            int
--生日:birth        datetime
create table teacher
(
  tcode        int primary key identity(1001,1)not null,--主键
  tname      varchar(20),
  lesson        char(10),
  age            int,
  birth        datetime,
)
insert into teacher values(‘张斌‘,‘语文‘,30,1986-3-30)
insert into teacher values(‘齐红‘,‘语文‘,40,1976-9-20)
insert into teacher values(‘王雪丽‘,‘语文‘,25,1991-2-3)

insert into teacher values(‘崔刚‘,‘数学‘,36,1980-2-10)
insert into teacher values(‘余兵‘,‘数学‘,30,1986-6-30)
insert into teacher values(‘刘备‘,‘数学‘,50,1966-7-20)

insert into teacher values(‘张灿灿‘,‘英语‘,30,1986-5-5)
insert into teacher values(‘王丽丽‘,‘英语‘,26,1990-7-7)
insert into teacher values(‘张婷婷‘,‘英语‘,35,1981-6-3)

--表三:分数表        score
--学号:code        int        (学生学号的外键)
--语文分数:yufen        decimal(18,2)
--数学分数:shufen        decimal(18,2)
--英语分数:yingfen        decimal(18,2)

  create table    score
  (
    fcode    int    ,--外键
    yufen   decimal(18,2),
    shufen    decimal(18,2),
    yingfen    decimal(18,2),
)
--插入分数数据
--一班数据
insert into score values(10001,80,90,70)
insert into score values(10002,50,80,60)
insert into score values(10003,60,55,80)
insert into score values(10004,70,90,55)
insert into score values(10005,50,70,80)
--二班数据
insert into score values(10006,90,60,80)
insert into score values(10007,60,50,66)
insert into score values(10008,70,82,59)
insert into score values(10009,63,87,85)
insert into score values(10010,45,55,64)
--三班分数
insert into score values(10011,90,90,70)
insert into score values(10012,60,80,70)
insert into score values(10013,55,70,56)
insert into score values(10014,70,80,70)
insert into score values(10015,85,55,70)

--学生表数据:插入三个班的学生数据,每个班的相同科目的教师都是相同的
--至少每个班5个人

--教师表数据:语文教师,数学教师,英语教师分别三个,每一个对应一个班级

--分数表数据:学号对应的语文、数学、英语成绩
select * from student
select * from teacher
select * from score

--查询此次语文成绩最高的学生的信息
select * from student where scode=
(select top 1 fcode from score order by yufen desc)

--查询此次数学成绩最高的学生的信息
select * from student where scode=
(select top 1 fcode from score order by shufen desc)

--查询此次英语成绩最高的学生的信息
select * from student where scode=
(select top 1 fcode from score order by yingfen desc)

--查询此次语文成绩最低的学生的信息
select * from student where scode=
(select top 1 fcode from score order by yufen )

--查询此次数学成绩最低的学生的信息
select * from student where scode=
(select top 1 fcode from score order by shufen )

--查询此次英语成绩最低的学生的信息
select * from student where scode=
(select top 1 fcode from score order by yingfen)

--查询此次语文成绩最低的学生所任课教师的信息
select * from teacher where tcode=
(select yujiao from student where scode=
(select top 1 fcode from score order by yufen ))

--查询此次数学成绩最低的学生所任课教师的信息
select * from teacher where tcode=
(select shujiao from student where scode=
(select top 1 fcode from score order by shufen ))

--查询此次英语成绩最低的学生所任课教师的信息
select * from teacher where tcode=
(select yingjiao from student where scode=
(select top 1 fcode from score order by yingfen ))

--查询此次语文成绩最高的学生所任课教师的信息
select * from teacher where tcode=
(select yujiao from student where scode=
(select top 1 fcode from score order by yufen desc ))

--查询此次数学成绩最高的学生所任课教师的信息
select * from teacher where tcode=
(select shujiao from student where scode=
(select top 1 fcode from score order by shufen desc ))

--查询此次英语成绩最高的学生所任课教师的信息
select * from teacher where tcode=
(select yingjiao from student where scode=
(select top 1 fcode from score order by yingfen desc ))

--查询学生信息,将所有语文任课教师编号改为该科目的任课教师名字显示
select scode,name, sex, banji, (select tname from teacher where teacher.tcode=student.yujiao),shujiao,yingjiao from student

--查询学生信息,将所有任课教师编号改为该科目的任课教师名字显示
select scode,name, sex, banji,
(select tname from teacher where teacher.tcode=student.yujiao)as 语文老师,
(select tname from teacher where teacher.tcode=student.shujiao)as 数学老师,
(select tname from teacher where teacher.tcode=student.yingjiao) as 英语老师
from student

select scode, name,sex ,banji,yujiao,shujiao,yingjiao from student
join teacher on student.yujiao=teacher.tname
or student.yujiao=teacher.tname
or student.yujiao=teacher.tname

--查询各个学生的学号,姓名,语文分数,数学分数,英语分数,以及三门课里面每一门课的任课教师姓名
select scode,name,
(select yufen from score where score.fcode=student.scode ) as 语文成绩,
(select shufen from score where score.fcode=student.scode ) as 数学成绩,
(select yingfen from score where score.fcode=student.scode ) as 英语成绩,
(select tname from teacher where teacher.tcode=student.yujiao) as 语文老师,
(select tname from teacher where teacher.tcode=student.shujiao) as 数学老师,
(select tname from teacher where teacher.tcode=student.yingjiao) as 英语老师
from student

--查询每个班级里的语文最高分
--一班
select  top 1 yufen from score where fcode in
(select scode from student where banji=‘一班‘) order by yufen desc
--二班
select  top 1 yufen from score where fcode in
(select scode from student where banji=‘二班‘) order by yufen desc
--三班
select  top 1 yufen from score where fcode in
(select scode from student where banji=‘三班‘) order by yufen desc
--查询每个班级里的数学最高分
select  top 1 shufen from score where fcode in
(select scode from student where banji=‘一班‘) order by shufen desc
--二班
select  top 1 shufen from score where fcode in
(select scode from student where banji=‘二班‘) order by shufen desc
--三班
select  top 1 shufen from score where fcode in
(select scode from student where banji=‘三班‘) order by shufen desc
--查询每个班级里的英语最高分
select  top 1 yingfen from score where fcode in
(select scode from student where banji=‘一班‘) order by yingfen desc
--二班
select  top 1 yingfen from score where fcode in
(select scode from student where banji=‘二班‘) order by yingfen desc
--三班
select  top 1 yingfen from score where fcode in
(select scode from student where banji=‘三班‘) order by yingfen desc

--查看每个班的语文平均分
select AVG(yufen) from score where fcode in
(select scode from student where banji=‘一班‘)

select AVG(yufen) from score where fcode in
(select scode from student where banji=‘二班‘)

select AVG(yufen) from score where fcode in
(select scode from student where banji=‘三班‘)

--查询语文课程平均分最高的班级的语文教师的信息
declare @a decimal(18,2)
select @a=AVG(yufen) from score where fcode in
(select scode from student where banji=‘一班‘)
declare @b decimal(18,2)
select @b=AVG(yufen) from score where fcode in
(select scode from student where banji=‘二班‘)
declare @c decimal(18,2)
select @c=AVG(yufen) from score where fcode in
(select scode from student where banji=‘三班‘)
declare @jie varchar(20)
if @a>@b and @a>@c

   set  @jie =‘一班‘

else if @b>@a and @b>@c
   set @jie =‘二班‘

else if @c>@a and @c>@b
    set @jie =‘三班‘

    select * from teacher where tcode in
    (select yujiao from student where banji=@jie)

--查询数学课程平均分最高的班级的数学教师的信息
declare @a decimal(18,2)
select @a=AVG(shufen) from score where fcode in
(select scode from student where banji=‘一班‘)
declare @b decimal(18,2)
select @b=AVG(shufen) from score where fcode in
(select scode from student where banji=‘二班‘)
declare @c decimal(18,2)
select @c=AVG(shufen) from score where fcode in
(select scode from student where banji=‘三班‘)
declare @jie varchar(20)
if @a>@b and @a>@c

   set  @jie =‘一班‘

else if @b>@a and @b>@c
   set @jie =‘二班‘

else if @c>@a and @c>@b
    set @jie =‘三班‘

    select * from teacher where tcode in
    (select shujiao from student where banji=@jie)

--查询英语课程平均分最高的班级的英语教师的信息
declare @a decimal(18,2)
select @a=AVG(yingfen) from score where fcode in
(select scode from student where banji=‘一班‘)
declare @b decimal(18,2)
select @b=AVG(yingfen) from score where fcode in
(select scode from student where banji=‘二班‘)
declare @c decimal(18,2)
select @c=AVG(yingfen) from score where fcode in
(select scode from student where banji=‘三班‘)
declare @jie varchar(20)
if @a>@b and @a>@c

   set  @jie =‘一班‘

else if @b>@a and @b>@c
   set @jie =‘二班‘

else if @c>@a and @c>@b
    set @jie =‘三班‘

    select * from teacher where tcode in
    (select yingjiao from student where banji=@jie)

--求某一个教师教学水平是否达标,学生成绩大于等于80人数超过3个为达标
--刘备

  declare @laoshi decimal(18,2)
   select @laoshi=tcode from teacher where tname=‘刘备‘
   declare @renshu int
  select @renshu=COUNT(*) from score where fcode in(select scode from student where shujiao=@laoshi)
  and shufen>=80
  if @renshu>=3
     print ‘教学水平达标‘
  else
     print  ‘教学水平不达标‘
时间: 2024-10-15 21:07:30

子查询练习的相关文章

子查询

子查询返回单行多列: ANY三种用法: ALL两种用法:

SQL Server子查询填充DataSet时报500内部错误的解决办法

运行环境为Visual Studio 2010,数据库为SQL Server 2008. 执行下面SQL语句 SELECT SubsiteId, SubsiteTitle, count(CollectionID) CollectionNumber,count(LName) PlantNumber FROM (SELECT DISTINCT SubsiteId, SubsiteTitle, CollectionID, LName, CName FROM Cumplag_Garden_Plants

Mysql数据库理论基础之五--SELECT单多表查询、子查询、别名

一.简介 由MySQL AB公司开发,是最流行的开放源码SQL数据库管理系统,主要特点: 1.是一种数据库管理系统 2.是一种关联数据库管理系统 3.是一种开放源码软件,且有大量可用的共享MySQL软件 4.MySQL数据库服务器具有快速.可靠和易于使用的特点 5.MySQL服务器工作在客户端/服务器模式下,或嵌入式系统中 InnoDB存储引擎将InnoDB表保存在一个表空间内,该表空间可由数个文件创建.这样,表的大小就能超过单独文件的最大容量.表空间可包括原始磁盘分区,从而使得很大的表成为可能

前端学数据库之子查询

查询数据库,当查询条件比较复杂时,常常需要用到子查询.子查询(Subquery)是指出现在其他SQL语句内的SELECT子句.本文将详细介绍子查询 定义 子查询(Subquery)是指出现在其他SQL语句内的SELECT子句 SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2); 其中,SELECT * FROM t1,称为外层查询(Outer Query/Outer Statement),SELECT col2 FROM t2,称为子查询(Su

ThinkPHP3.2 SQL alias 子查询

SELECT info_key, info_value, info_status, edit_time FROM (SELECT * FROM `detail` WHERE login = '[email protected]' ORDER BY edit_time DESC  ) AS aaa GROUP BY info_key 希望通过Thinkphp实现基于alias的子查询,终于参考下面这个文章实现了. http://www.thinkphp.cn/update/122.html 如下是

sql的基础语句-单行函数,dual,数字函数,日期函数,表连接,集合运算,分组报表,单行子查询,多行子查询

3. 单行函数 3.1 转换函数 select ascii('A'),chr(65) from dual; select to_char(1243123),1231451 from dual;靠左边的就是字符串,靠右边的就是数字 select to_char(123512a121) from dual;   --错误的写法,没有引号表示数字,但是数字里面包含了字母,不合法的输入值 select to_number('123141211') from dual; select to_number(

mysql子查询慢的问题

当你在用explain工具查看sql语句的执行计划时,若select_type 字段中出现"DEPENDENT SUBQUERY"时,你要注意了,你已经掉入了mysql子查询慢的"坑"...下面我们来看一个具体的例子 有这样一条查询语句: SELECT gid,COUNT(id) as count FROM shop_goods g1 WHERE status =0 and gid IN (SELECT gid FROM shop_goods g2 WHERE si

Mysql——子查询

子查询的位置: select 中.from 后.where 中.group by 和order by 中无实用意义. 子查询分为如下几类: 1,标量子查询:返回单一值的标量,最简单的形式. 2,列子查询:返回的结果集是 N 行一列. 3,行子查询:返回的结果集是一行 N 列. 4,表子查询:返回的结果集是 N 行 N 列. 可以使用的操作符:= > < >= <= <> ANY IN SOME ALL EXISTS 标量子查询:是指子查询返回的是单一值的标量,如一个数字

彻底搞懂oracle的标量子查询

oracle标量子查询和自定义函数有时用起来比较方便,而且开发人员也经常使用,数据量小还无所谓,数据量大,往往存在性能问题. 以下测试帮助大家彻底搞懂标量子查询. SQL> create table a (id int,name varchar2(10)); Table created. SQL> create table b (id int,name varchar2(10)); Table created. SQL> insert into a values (1,'a1'); 1

SQL----关联子查询

在where子句中使用子查询时,该子查询的结果必须是单一的 所以在以不同商品为单位,对售价进行比较就要用到关联子查询 如下表格:Table_2 ID 商品 售价 1 苹果 15 2 猕猴桃 11 3 梨 22 4 梨 23 5 猕猴桃 23 6 梨 12 7 猕猴桃 10 8 猕猴桃 3 9 苹果 5 SELECT * FROM Table_2 AS S1 WHERE 售价>(SELECT AVG(售价) FROM Table_2 AS S2 WHERE S1.商品=S2.商品 /*结合条件一定