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. YII2 更新数据不成功

    起因: CLI模式,定时任务.同步其他系统中的DB数据,通过视图的方式. 历程: 原脚本已经写好,实在已经有的基础上修改,增加新的字段. 加了字段后,执行,但始终不成功,表里记录的utime也是能更新 ...

  2. MyBatis框架的基本要素-核心接口和类的作用范围

    通过上面运行案例-查询用户表中的记录数. 非集成环境下的最佳作用域范围: SqlSessionFactoryBuilder 用过即丢,推荐作用域在方法体内. SqlSessionFactory 最佳作 ...

  3. java 线程安全(初级)

    创建和启动Java线程 Java线程是个对象,和其他任何的Java对象一样.线程是类的实例java.lang.Thread,或该类的子类的实例.除了对象之外,java线程还可以执行代码. 创建和启动线 ...

  4. tensorflow2.0 学习(三)

    用tensorflow2.0 版回顾了一下mnist的学习 代码如下,感觉这个版本下的mnist学习更简洁,更方便 关于tensorflow的基础知识,这里就不更新了,用到什么就到网上取搜索相关的知识 ...

  5. BZOJ 5469: [FJOI2018]领导集团问题 dp+线段树合并

    在 dp 问题中,如果发现可以用后缀最大值来进行转移的话可以考虑去查分这个后缀最大值. 这样的话可以用差分的方式来方便地进行维护 ~ #include <bits/stdc++.h> #d ...

  6. 关于H5判定区域里面滑动到底部,加载更多的总结

    1.如何判定H5中滑动到底部,然后加载更多的功能实现. 思路:我们需要设定一个固定高度的盒子,然后我们利用scroll来监听滚动,当scrollTop(滚动的距离) + clientHeight(页面 ...

  7. pgloader 学习(九) pg 2 pg 使用with 参数控制同步逻辑

    pgloader 支持比较丰富的配置参数,同时默认数据在同步的时候是会进行索.schema 以及数据的同步对于实际我们可能存在需要进行控制,我们可以通过with 参数方便的处理 参考配置 load 文 ...

  8. circus web console 依赖tornado>3.2 无法访问的bug

    circus web console 是一个很不错的web 监控circus 工具,但是对于高版本一直存在一个bug 信息如下 Traceback (most recent call last): F ...

  9. 控制论模型&心流模型&波模型

    1.控制论模型 这是对设定的目标,通过多次输入和输出,反馈调节,最终达成目标的方法.广泛运用于自然科学与社会科学中.反馈的周期长短决定了调节精度的大小以及达到目标的速度.反馈结果与目标背离的立即纠正, ...

  10. HAVING 搜索条件在进行分组操作之后应用

    HAVING 搜索条件在进行分组操作之后应用: 如:查询帖子访问量大于15的用户id: select t.user_id,u.name,sum(count_view) from t_topic t l ...