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. shell脚本中向hive动态分区插入数据

    在hive上建表与普通分区表创建方法一样: CREATE TABLE `dwa_m_user_association_circle`( `device_number` string, `oppo_nu ...

  2. 爬虫 - 解析库之Beautiful Soup

    了解Beautiful Soup 中文文档: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 ...

  3. HTML页面 js返回上一页

    <input type="button" name="Submit" onclick="javascript:history.back(-1); ...

  4. LeetCode 1099. Two Sum Less Than K

    原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and inte ...

  5. Tensorflow细节-P212-循环神经网络

    本节的循环神经网络一图足以说明 import numpy as np X = [1, 2] state = [0.0, 0.0] # 定义RNN的参数 # 以下两个本来是像这样分开的,但是在运算时合并 ...

  6. 单页面应用程序(SPA)

    一.概念 ①在一个页面上实现网站的大部分功能,就是单页面应用程序,是一种常见的网页开发模式. ②整个网站就只有一个Html文件,每次在切换页面时,不需要请求服务器,只要通过本地的js来切换即可.这样可 ...

  7. 18年今日头条笔试第一题题解:球迷(fans)

    其实本题是加强版,原数据是100*100的,老师为了尊重我们的智商加成了3000*3000并进行了字符串处理…… 上原题~ 球迷 [问题描述] 一个球场C的球迷看台可容纳M*N个球迷.官方想统计一共有 ...

  8. mysql upper() 函数

    mysql> select upper(" cdcdcd"); +------------------+ | upper(" cdcdcd") | +-- ...

  9. Beta冲刺(4/5)

    队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 讨论校园百科究竟如何实现,并分配了任务 提交记录(全组共用) 接下来的计划 加快校园百科的进度 准备Beta版本的汇报 还 ...

  10. QML学习(三)——<QML命名规范>

    QML对象声明 QML对象特性一般使用下面的顺序进行构造: id 属性声明 信号声明 JavaScript函数 对象属性 子对象 状态 状态切换 为了获取更好的可读性,建议在不同部分之间添加一个空行. ...