1
**SQL多表连查**
1
2
3
4
5
6
7
8
--查询员工和部门信息
select * from emp e,dept d where e.deptno=d.deptno
--查询员工姓名,部门名称
select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno
--查询所有员工姓名,部门名称
select e.*, d.dname from emp e,dept d where e.deptno=d.deptno
--查询工资大于3000的员工姓名,工资和部门名称
select e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno and e.sal>=3000
1
2
3
4
                          --非等值查询
--查询公司工资等级
select * from emp e,salgrands where e.sal<=s.hisal and e.sal>s.losal
select * from salagrade
1
2
3
4
5
                              --外链接
--左外连接
select*from emp e,dept d where e.deptno=d.deptno(+)
--右外连接
select*from emp e,dept d where e.deptno(+)=d.deptno
1
2
3
4
5
6
                       --自连接
--查询员工姓名和经理姓名
select e1.ename ,e2.ename from emp e1,emp e2 where e1.mgr=e2.deptno
--查询员工姓名、经理姓名和其他经理姓名
select e1.ename,e2.ename,e3.ename from emp e1,emp e2, emp e3
where e1.mgr=e2.empno and e2.empno=e3.empno
1
**以上为92版仅在面试出现**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
                      --SQL99   表连接
--交叉连接   cross join
select *from emp e cross join dept d
--自然连接      natural join
select *from emp natural join dept
--using
select e.ename,deptno from emp e join dept d using(deptno)
--on 自定义连接
select *from emp e join dept d
on e.deptno=d.deptno
select *from emp e1 join emp e2 on e1.mgr=e2.empno
--查询员工的姓名、经理及其经理的名字
select e1.ename,e2.ename,e3.ename from emp e1 join emp e2 on e1.mgr=e2.empno join emp e3 on e2.mgr=e3.empno
--SQL99  inner join  两边都合法的数据
select*from emp e inner join dept d on e.deptno=d.deptno
--left join 以左表为主
select*from emp e left join dept d on e.deptno=d.deptno
--right join 以右表为主
select*from emp e right join dept d on e.deptno=d.deptno
--full join 全连 取两个表的所有数据
select*from emp e full join dept d on e.deptno=d.deptno
----查询员工的姓名、经理及其经理的名字和部门名字
select e1.ename, d.dname, e2.ename, d2.dname
  from emp e1
  left join dept d
    on e1.deptno = d.deptno
  left join emp e2
    on e2.mgr = e2.empno
  left join dept d2
    on d2.deptno = d2.deptno
1
**子查询**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
--比CLARK工资高的人
select SAL from emp where ename='CLARK'
select *from emp where sal>(select SAL from emp where ename='CLARK')
--查询工资高于平均工资的雇员姓名和工资
select avg(sal) from emp
select ename,sal from emp where sal>(select avg(sal) from emp )
--查询同SCOTT同部门的且工资比他低的员工的名字和工资
select deptno from emp where ename='SCOTT'
select sal from emp where ename='SCOTT'
select ename, sal
  from emp
 where deptno = (select deptno from emp where ename = 'SCOTT')
   and sal < (select sal from emp where ename = 'SCOTT')
--查询和'SMITH','SCOTT','CLARK'同一个部门的员工姓名
select distinct deptno from emp where ename in('SMITH','SCOTT','CLARK')
select *
  from emp
 where deptno in (select distinct deptno
                    from emp
                   where ename in ('SMITH', 'SCOTT', 'CLARK'))
--查询和'SMITH', 'SCOTT', 'CLARK'同一个部门并不包含他们三个的员工姓名
   and ename not in ('SMITH', 'SCOTT', 'CLARK')
--查询工资最高的员工名字和工资
select ename,sal from emp where sal=(select max(sal)from emp )
--查询职务和'SCOTT'相同但是比'SCOTT'雇佣时间早的雇员信息
select * from emp
 where job = (select job from emp where ename = 'SCOTT')
   and hiredate < (select hiredate from emp where ename = 'SCOTT')
--查询工资比'SCOTT'高或者雇佣时间比'SCOTT'早的雇员编号和姓名
select empno,ename from emp
 where job = (select job from emp where ename = 'SCOTT')
   and hiredate < (select hiredate from emp where ename = 'SCOTT')
1
**多行子查询**
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
--查询工资低于任何一个“CLERK”的工资的雇员信息
select*from emp where sal all (select sal from emp where job = 'SALESMAN')
--查询部门20中职务同部门10的雇员一样的雇员信息。
select *from emp where job in (select job from emp where deptno=10)and deptno=20
--查询在雇员中有哪些人是经理人
select *
  from emp
 where empno in (select distinct mgr
                   from emp
                  where mgr is not null
                     or mgr != '')
--找出部门编号为20的所有员工中收入最高的职员
select ename
  from emp
 where sal = (select max(sal) from emp where deptno = 20)
   and deptno = 20
</any(select>

sql数据库查询相关操作,SQL的应用——SQL多表连查、子查询、多行子查询的更多相关文章

  1. PHP对MySQL数据库的相关操作

    一.Apache服务器的安装 <1>安装版(计算机相关专业所用软件---百度云链接下载)-直接install<2>非安装版(https://www.apachehaus.com ...

  2. SQL SERVER: 合并相关操作(Union,Except,Intersect)

    SQL SERVER: 合并相关操作(Union,Except,Intersect) use tempdb create table tempTable1 (id int primary key id ...

  3. python操作mysql数据库的相关操作实例

    python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称 ...

  4. SQL数据库的基础操作

    一,认识SQL数据库 美国Microsoft公司推出的一种关系型数据库系统.SQLServer是一个可扩展的.高性能的.为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有 ...

  5. SQL数据库的一些操作

    --以 MySQL为例 //登陆 mysql -u root -p //创建一个名为test_lib的数据库 CREATE DATABASE test_lib //删除一个名为test_lib的数据库 ...

  6. SQL数据库问题 解释一下下面的代码 sql 存储过程学习

    SQL数据库问题 解释一下下面的代码 2008-08-13 11:30wssqyl2000 | 分类:数据库DB | 浏览1154次 use mastergocreate proc killspid( ...

  7. Android下的SQLite数据库的相关操作及AndroidTestCase测试

    一:创建数据库 package com.itcode.mysqlite; import android.content.Context; import android.database.sqlite. ...

  8. MySql数据库的相关操作

    SQL(Structred Query Language)结构化查询语言:和数据库交互的语言,进行数据库管理的语言. 一.数据库的操作: 1.查询所有数据库: show databases; 2.创建 ...

  9. 十、K3 WISE 开发插件《SQL Profiler跟踪单据操作时产生的SQL语句》

    =================================== 目录: 1.查询帐套的数据库DBID 2.配置需要跟踪数据库的DBID 3.配置跟踪参数 4.跟踪进行 5.分析跟踪语句 === ...

随机推荐

  1. python面试题&练习题之嵌套循环

    1.打印如下结果: 1*5=5 2*10=20 3*15=45 ... 10*50=500 for i in range(1,11): print(str(i)+'x'+str((i*5))+'='+ ...

  2. 学习:c++指向指针的指针(多级间接寻址)

    指向指针的指针是一种多级间接寻址的形式,或者说是一个指针链.通常,一个指针包含一个变量的地址.当我们定义一个指向指针的指针时,第一个指针包含了第二个指针的地址,第二个指针指向包含实际值的位置. 当一个 ...

  3. 数据库 Hash Join的定义,原理,算法,成本,模式和位图

    Hash Join只能用于相等连接,且只能在CBO优化器模式下.相对于nested loop join,hash join更适合处理大型结果集       Hash Join的执行计划第1个是hash ...

  4. 又一款dump文件观察工具---MiniDumpView

    简介 MiniDumpView实用程序可用于显示minidump中数据流的内容.特别是,可以显示以下信息: 操作系统和CPU信息 进程信息(进程ID和时间) 模块列表(包含每个模块的详细信息) 线程列 ...

  5. apisix 基于openresty 的api 网关

    apisix 是由openresty 团队开发并开源的微服务api gateway,还不错,官方文档也比较全,同时这个也是一个不错的学习openresty 的项目 以下为来自官方的架构图 插件加载 插 ...

  6. 4、spark streaming+kafka

    一.Receiver模式 1. receiver模式原理图 在SparkStreaming程序运行起来后,Executor中会有receiver tasks接收kafka推送过来的数据.数据会被持久化 ...

  7. mysql e的n次幂exp()

    mysql> ); +-------------------+ | exp() | +-------------------+ | 2.718281828459045 | +---------- ...

  8. 记录python循环引用带来的MemoryError错误解决

    在以前的python中,没有遇到过这样的错误,上来之后还是很蒙蔽的,问了几个业内的人,他们 都给我说让我改服务器里面配置的东西, 因为是要给大家用的,服务器要保证长久运行,不能临时去修改这个,导致在大 ...

  9. Linux系统学习(一)一Linux介绍

    一.Linux初识 1.1 Linux是什么 Linux是一种自由和开放源码的类UNIX操作系统,使用Linux内核.目前存在着许多不同的Linux发行版,可安装在各种各样的电脑硬件设备,从手机.平板 ...

  10. 安装与配置HSDIS与JITWatch

    本作者的系统: 操作系统版本及位数可通过uname -a命令查看,如下: Linux ubuntu 3.13.0-32-generic #57~precise1-Ubuntu SMP Tue Jul ...