今天,我主要学习了子查询的内容。

create database lianxi0720

go

use lianxi0720
go
create table bumen
(
bcode int primary key,--部门编号
bname varchar(20), --部门名称
bceo varchar(20), --部门负责人
btel varchar(20) --部门电话
)
go
create table renyuan
(
code int primary key identity(10001,1),
name varchar(20),
sex varchar(10),
age int,
bc int
)
go

--设置好外间关系之后来插入数据
--先插入部门的数据
insert into bumen values(1001,'财务部','张三','1234567')
insert into bumen values(1002,'销售部','李四','2345678')
insert into bumen values(1003,'人事部','王五','3456789')
insert into bumen values(1004,'技术部','赵六','4567890')
go

select * from bumen
--插入人员表的信息
insert into renyuan values('张三','男',33,1001)
insert into renyuan values('李四','女',27,1002)
insert into renyuan values('王五','男',25,1003)
insert into renyuan values('赵六','女',24,1004)

insert into renyuan values('王祖蓝','男',38,1001)
insert into renyuan values('马蓉','女',33,1002)
insert into renyuan values('王宝强','男',36,1003)
insert into renyuan values('杨颖','女',28,1004)

insert into renyuan values('郑恺','男',29,1001)
insert into renyuan values('范冰冰','女',40,1002)
insert into renyuan values('张伟','男',30,1003)
insert into renyuan values('蔡依林','女',37,1004)

--查询年纪最大的人的部门名称
select MAX(age) from renyuan
select bc from renyuan where age=40
select bname from bumen where bcode=1002
--子查询
select bname from bumen where bcode=

(select bc from renyuan where age =
(select max(age) from renyuan )
)

--按照年龄排序后的前三个人的所有信息
select top 3 * from renyuan order by age
--按照年龄排序后的6/7/8名人员的所有信息
select top 3 * from renyuan where code not in
(select top 5 code from renyuan order by age)
order by age
--分页查询,要求 一页给显示5条数据
--第一页
select top 5 * from renyuan
--第二页
select top 5 * from renyuan where code not in(select top 5 code from renyuan)
--第三页
select top 5 * from renyuan where code not in(select top 10 code from renyuan)

--总共有几页????
select CEILING( COUNT(*)/5.0) from renyuan
--查询销售部里的年龄大于35岁的人的所有信息
select * from renyuan where code in
(select code from renyuan where age>35 and bc=
(select bcode from bumen where bname='销售部')
)

--查看所有人员信息,将部门编号替代为部门名称
select code , name, sex , age , (select bname from bumen where bumen.bcode= renyuan.bc)as 部门 from renyuan
--将每个人的主管添加上
select code , name, sex , age , (select bname from bumen where bumen.bcode= renyuan.bc)as 部门,
(select bceo from bumen where bumen.bcode= renyuan.bc) from renyuan

SQL 课程 子查询的更多相关文章

  1. SQL Fundamentals: 子查询 || 分析函数(PARTITION BY,ORDER BY, WINDOWING)

    SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...

  2. SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句

    SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...

  3. SQL Fundamentals: 子查询 || 行列转换(PIVOT,UNPIVOT,DECODE),设置数据层次(LEVEL...CONNECT BY)

    SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...

  4. SQL的子查询操作

    对于表中的每一个记录,我们有时候需要提取特殊的或者你需要的记录,要提前做一个表的筛选,之后再对你选出的记录做一个修改,此时你必须使用SQL的子查询操作.如:修改id=5的记录的strContent字段 ...

  5. C#构造方法(函数) C#方法重载 C#字段和属性 MUI实现上拉加载和下拉刷新 SVN常用功能介绍(二) SVN常用功能介绍(一) ASP.NET常用内置对象之——Server sql server——子查询 C#接口 字符串的本质 AJAX原生JavaScript写法

    C#构造方法(函数)   一.概括 1.通常创建一个对象的方法如图: 通过  Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初 ...

  6. SQL关联子查询

    SQL关联子查询执行顺序: 1.先取到主查询中的相关数据,一次取一行主查询的数据 2.然后传入子查询,进行子查询 3.最后做主查询where筛选,注意子查询的where条件同样需要加在主查询后 参考: ...

  7. SQL server 子查询、设置主键外键、变量及变量查询

    一.子查询 子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这 ...

  8. sql server 子查询 和exists使用

    概述 子查询的概念: 当一个查询是另一个查询的条件时,称之为子查询.子查询可以嵌套在主查询中所有位置,包括SELECT.FROM.WHERE.GROUP BY.HAVING.ORDER BY. 外面的 ...

  9. SQL 数据库 子查询、主外键

    子查询,又叫做嵌套查询. 将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询. 子查询有两种类型: 一种是只返回一个单值的子查询,这时它可以用在 ...

随机推荐

  1. android相关内容

    一: 前台进程: 前台的进程的优先级最高, 可见进程: android系统一般存在少量的可见进程. 服务进程: 没有用户界面, 后台进程: 一般存在较多的后台进程. 空进程: 不包括任何活跃组件的进程 ...

  2. Java定时任务器

    java定时任务,每天定时执行任务.以下是这个例子的全部代码. public class TimerManager { //时间间隔 private static final long PERIOD_ ...

  3. 在spring拦截器中response输出html标签到页面

    @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object ...

  4. Java List&Map简单初始化方法

    Java中List与Map初始化的一些写法 // InitCollections.java - sample of init collect package com.util; import java ...

  5. apache 修改连接数(转)

    1.先要修改最大连接数,必须了解Apache的MPM(Multi -Processing Modules,多道处理模块)Apache2.0中MPM分为3种(perfork.worker.event). ...

  6. thinkphp pdo 重写问题

    ThinkPHP3.2.3版本数据库驱动采用PDO完全重写,配置和使用上面也比之前版本更加灵活和强大,我们来了解下如何使用. 首先,3.2.3的数据库配置信息有所调整,完整的数据库设置包括: 复制代码 ...

  7. Using YARN with Cgroups testing in sparkml cluster

    部署服务器: sparkml 集群 ########### sparkml ########## sparkml-node1 # yarn resource manager sparkml-node2 ...

  8. libimobiledevice命令

    Mac 安装 1. 安装HomeBrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...

  9. Idea1.5使用Maven搭建Apache Spark1.6源码阅读环境

    1.插件安装,在Idea界面依次:File->settings->plugins,安装Maven 2.下载Spark1.6.2源码,这个在GitHub上下载,具体流程自己百度,很简单 3. ...

  10. 关于C++中的重定位

    "标准库定义了4个IO对象,处理输入时使用命名为cin的istream类型对象,这个对象也成为标准输入.处理输出时使用命名为cout的ostream类型对象,这个对象也称为标准输出.标准库还 ...