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. element ui 中的 resetFields() 报错'resetFields' of undefined

    每次做各种form表单时,首先要注意的是初始化,但是刚开始若没有仔细看文档,则会自己写个方法将数据设置为空,但是这样就会出现一个问题,表单内存在各种验证,假如是一个弹框内有form表单,弹框出现就执行 ...

  2. JVM JDK1.8 以后的新特性 VisualVM的安装使用

    一.JVM在新版本的改进更新以及相关知识 1.JVM在新版本的改进更新 图中可以看到运行时常量池是放在方法区的 1.1对比: JDK 1.6 及以往的 JDK 版本中,Java 类信息.常量池.静态变 ...

  3. 黑马2017年java就业班全套视频教程

    黑马程序员培训班 黑马2017年java就业班全套视频教程 ava学习路线图.pptx等多个文件   - 2019-07-20 10:03     老师分享的资料 - 2019-07-20 10:03 ...

  4. LeetCode 1079. Letter Tile Possibilities

    原题链接在这里:https://leetcode.com/problems/letter-tile-possibilities/ 题目: You have a set of tiles, where ...

  5. edgedb 开发环境运行

    以下是一篇来自官方的edgedb 开发环境搭建说明,实际上我以前自己也摸索过一个,基本方法一样,一些是官方的做一个 简单的记录 预备工具 GNU make version 3.80 or newer; ...

  6. Break关键字和Continue关键字

    1.Break关键字 在循环体内,只要代码遇到break,程序立马结束当前循环. 当前循环指的是break语句所在的循环体.(直接跳到大括号外) // 例1: 1到10,第一是数字是2的倍数,控制台输 ...

  7. QBXT 2017GoKing problems 补完计划

    10.11 Updata : 烦死了...麻烦死了...不补了..就这些吧 20171001 上: 100 + 90 + 90 = 280 = rank 8 T1 /* T1 从最大的数开始倒着枚举 ...

  8. c语言博客作业03--循环结构

    0.展示PTA总分 1.本章学习总结 1.1学习内容总结 循环语句 for语句: for( 表达式1; 表达式2; 表达式3 ) { // 需要执行的语句; } 其执行过程是:表达式 1 首先执行且只 ...

  9. 让你的shell更体贴

    使用shell的时候很多,特别是拉幕式终端,使用时更加方便,同时可以利用 echo "Did you know that:" ;whatis $(ls /bin | shuf -n ...

  10. Linux+Apache环境下安装SSL证书

    一.安装证书 (温馨提示:安装证书前请先备份您需要修改的服务器配置文件) 1.确认证书文件及证书路径.  例证书文件为:zzidc.com.jks,放置目录为Tomcat的conf目录下.  2.配置 ...