不懂装懂

第一篇

<meta charset="utf-8">

<article class="_2rhmJa">

  • 1.列出至少有一个员工的所有部门。
SELECT
    d.DNAME
FROM
    emp e,
    dept d
WHERE
    e.DEPTNO = d.DEPTNO
GROUP BY
    d.DNAME
HAVING
    COUNT(d.DEPTNO) >= 1

SQL> select dname from dept where deptno in(select deptno from emp); 
DNAME
--------------
RESEARCH
SALES
ACCOUNTING
--------或--------
SQL> select dname from dept where deptno in(select deptno from emp group by deptno having count(deptno) >=1); 
DNAME
--------------
ACCOUNTING
RESEARCH
SALES

  • 2.列出薪金比“SMITH”多的所有员工。
SELECT
    *
FROM
    emp e
WHERE
    e.SAL > (
        SELECT
            sal
        FROM
            emp e
        WHERE
            e.ENAME = 'SMITH'
    )

SQL> select * from emp where sal > (select sal from emp where ename = 'SMITH');

EMPNO ENAME      JOB         MGR HIREDATE          SAL      COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
 7499 ALLEN      SALESMAN   7698 1981-2-20     1600.00    300.00     30
 7521 WARD       SALESMAN   7698 1981-2-22     1250.00    500.00     30
 7566 JONES      MANAGER    7839 1981-4-2      2975.00               20
 7654 MARTIN     SALESMAN   7698 1981-9-28     1250.00   1400.00     30
 7698 BLAKE      MANAGER    7839 1981-5-1      2850.00               30
 7782 CLARK      MANAGER    7839 1981-6-9      2450.00               10
 7788 SCOTT      ANALYST    7566 1987-4-19     4000.00               20
 7839 KING       PRESIDENT       1981-11-17    5000.00               10
 7844 TURNER     SALESMAN   7698 1981-9-8      1500.00      0.00     30
 7876 ADAMS      CLERK      7788 1987-5-23     1100.00               20
 7900 JAMES      CLERK      7698 1981-12-3      950.00               30
 7902 FORD       ANALYST    7566 1981-12-3     3000.00               20
 7934 MILLER     CLERK      7782 1982-1-23     1300.00               10
  102 EricHu     Developer  1455 2011-5-26 1   5500.00     14.00     10
  104 huyong     PM         1455 2011-5-26 1   5500.00     14.00     10
  105 WANGJING   Developer  1455 2011-5-26 1   5500.00     14.00     10
 16 rows selected

  • 3.列出所有员工的姓名及其直接上级的姓名。
SELECT
    w.ENAME '员工',
    m.ENAME '上级'
FROM
    emp w,
    emp m
WHERE
    m.EMPNO = w.MGR

SQL> select a.ename,(select ename from emp b where b.empno=a.mgr) as boss_name from emp a; 
ENAME      BOSS_NAME
---------- ----------
SMITH      FORD
ALLEN      BLAKE
WARD       BLAKE
JONES      KING
MARTIN     BLAKE
BLAKE      KING
CLARK      KING
SCOTT      JONES
KING       
TURNER     BLAKE
ADAMS      SCOTT
JAMES      BLAKE
FORD       JONES
MILLER     CLARK
EricHu     
huyong     
WANGJING    
17 rows selected

  • 4.列出受雇日期早于其直接上级的所有员工。
SELECT
    w.ename
FROM
    emp w
WHERE
    w.hiredate < (
        SELECT
            hiredate
        FROM
            emp m
        WHERE
            m.empno = w.mgr
    );

SQL> select a.ename from emp a where a.hiredate<(select hiredate from emp b where b.empno=a.mgr); 
ENAME
----------
SMITH
ALLEN
WARD
JONES
BLAKE
CLARK 
6 rows selected

  • 5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
SELECT
    d.dname,
    e.empno,
    e.ename,
    e.job,
    e.mgr,
    e.hiredate,
    e.sal,
    e.deptno
FROM
    dept d
LEFT JOIN emp e ON d.deptno = e.deptno;

SQL> select a.dname,b.empno,b.ename,b.job,b.mgr,b.hiredate,b.sal,b.deptno
  2  from dept a left join emp b on a.deptno=b.deptno;

DNAME          EMPNO ENAME      JOB         MGR HIREDATE          SAL DEPTNO
-------------- ----- ---------- --------- ----- ----------- --------- ------
RESEARCH        7369 SMITH      CLERK      7902 1980-12-17     800.00     20
SALES           7499 ALLEN      SALESMAN   7698 1981-2-20     1600.00     30
SALES           7521 WARD       SALESMAN   7698 1981-2-22     1250.00     30
RESEARCH        7566 JONES      MANAGER    7839 1981-4-2      2975.00     20
SALES           7654 MARTIN     SALESMAN   7698 1981-9-28     1250.00     30
SALES           7698 BLAKE      MANAGER    7839 1981-5-1      2850.00     30
ACCOUNTING      7782 CLARK      MANAGER    7839 1981-6-9      2450.00     10
RESEARCH        7788 SCOTT      ANALYST    7566 1987-4-19     4000.00     20
ACCOUNTING      7839 KING       PRESIDENT       1981-11-17    5000.00     10
SALES           7844 TURNER     SALESMAN   7698 1981-9-8      1500.00     30
RESEARCH        7876 ADAMS      CLERK      7788 1987-5-23     1100.00     20
SALES           7900 JAMES      CLERK      7698 1981-12-3      950.00     30
RESEARCH        7902 FORD       ANALYST    7566 1981-12-3     3000.00     20
ACCOUNTING      7934 MILLER     CLERK      7782 1982-1-23     1300.00     10
ACCOUNTING       102 EricHu     Developer  1455 2011-5-26 1   5500.00     10
ACCOUNTING       104 huyong     PM         1455 2011-5-26 1   5500.00     10
ACCOUNTING       105 WANGJING   Developer  1455 2011-5-26 1   5500.00     10
50abc                                                                 
OPERATIONS                                                            
Developer                                                          

20 rows selected

  • 6.列出所有“CLERK”(办事员)的姓名及其部门名称。
SELECT
    e.ename,
    d.dname
FROM
    emp e,
    dept d
WHERE
    e.DEPTNO = d.DEPTNO
AND e.JOB = 'CLERK'

SQL> select a.ename,b.dname from emp a join dept b on a.deptno=b.deptno and a.job='CLERK'; 
ENAME      DNAME
---------- --------------
SMITH      RESEARCH
ADAMS      RESEARCH
JAMES      SALES
MILLER     ACCOUNTING

  • 7.列出最低薪金大于1500的各种工作。
SELECT DISTINCT
    JOB
FROM
    emp
WHERE
    SAL > 1500

SQL> select distinct job as HighSalJob from emp group by job having min(sal)>1500; 
HIGHSALJOB
----------
ANALYST
Developer
MANAGER
PM
PRESIDENT

  • 8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
SELECT
    ENAME
FROM
    emp
WHERE
    DEPTNO = (
        SELECT
            DEPTNO
        FROM
            dept
        WHERE
            dname = 'SALES'
    )

SQL> select ename from emp where deptno=(select deptno from dept where dname='SALES'); 
ENAME
----------
ALLEN
WARD
MARTIN
BLAKE
TURNER
JAMES 
6 rows selected

  • 9.列出薪金高于公司平均薪金的所有员工。
SELECT
    ENAME
FROM
    emp
WHERE
    SAL > (SELECT AVG(SAL) FROM emp)

SQL> select ename from emp where sal>(select avg(sal) from emp); 
ENAME
----------
JONES
BLAKE
SCOTT
KING
FORD
EricHu
huyong
WANGJING 
8 rows selected

  • 10.列出与“SCOTT”从事相同工作的所有员工。
SELECT
    ENAME
FROM
    emp
WHERE
    JOB = (
        SELECT
            JOB
        FROM
            emp
        WHERE
            ENAME = 'SCOTT'
    )

SQL> select ename from emp where job=(select job from emp where ename='SCOTT');
 ENAME
----------
SCOTT
FORD

  • 11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。
SELECT
    a.ENAME,
    a.SAL
FROM
    emp a
WHERE
    a.SAL IN (
        SELECT
            b.SAL
        FROM
            emp b
        WHERE
            b.DEPTNO = 30
    )
-- AND a.DEPTNO <> 30

SQL> select a.ename,a.sal from emp a where a.sal in (select b.sal
  2  from emp b where b.deptno=30) and a.deptno<>30; 
ENAME            SAL

  • 12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。
SELECT
    ENAME,
    SAL
FROM
    emp
WHERE
    SAL > (
        SELECT
            MAX(SAL)
        FROM
            emp
        WHERE
            DEPTNO = 30
    )

SQL> select ename,sal from emp where sal>(select max(sal) from emp where deptno=30); 
ENAME            SAL
---------- ---------
JONES        2975.00
SCOTT        4000.00
KING         5000.00
FORD         3000.00
EricHu       5500.00
huyong       5500.00
WANGJING     5500.00 
7 rows selected

  • 13.列出在每个部门工作的员工数量、平均工资和平均服务期限。
SELECT
    (
        SELECT
            d.dname
        FROM
            dept d
        WHERE
            e.deptno = d.deptno
    ) AS deptname,
    count(e.deptno) AS deptcount,
    avg(e.sal) AS deptavgsal
FROM
    emp e
GROUP BY
    e.deptno;

SQL> select (select b.dname from dept b where a.deptno=b.deptno) as deptname ,count(deptno) as deptcount,avg(sal) as deptavgsal
  2  from emp a group by deptno; 
DEPTNAME        DEPTCOUNT DEPTAVGSAL
-------------- ---------- ----------
ACCOUNTING              6 4208.33333
RESEARCH                5       2375
SALES                   6 1566.66666

  • 14.列出所有员工的姓名、部门名称和工资。
SELECT
    e.ENAME,
    d.DNAME,
    e.SAL
FROM
    emp e,
    dept d
WHERE
    d.DEPTNO = e.DEPTNO

SQL> select a.ename,(select b.dname from dept b where b.deptno=a.deptno) as deptname,sal from emp a; 

ENAME      DEPTNAME             SAL
---------- -------------- ---------
SMITH      RESEARCH          800.00
ALLEN      SALES            1600.00
WARD       SALES            1250.00
JONES      RESEARCH         2975.00
MARTIN     SALES            1250.00
BLAKE      SALES            2850.00
CLARK      ACCOUNTING       2450.00
SCOTT      RESEARCH         4000.00
KING       ACCOUNTING       5000.00
TURNER     SALES            1500.00
ADAMS      RESEARCH         1100.00
JAMES      SALES             950.00
FORD       RESEARCH         3000.00
MILLER     ACCOUNTING       1300.00
EricHu     ACCOUNTING       5500.00
huyong     ACCOUNTING       5500.00
WANGJING   ACCOUNTING       5500.00

17 rows selected

  • 15.列出所有部门的详细信息和部门人数。
SELECT DISTINCT
    d.DEPTNO,
    d.DNAME,
    d.LOC,
    (
        SELECT
            COUNT(e.DEPTNO)
        FROM
            emp e
        WHERE
            e.DEPTNO = d.DEPTNO
        GROUP BY
            e.DEPTNO
    ) AS bumenrenshu
FROM
    emp e,
    dept d

SQL> select a.deptno,a.dname,a.loc,(select count(deptno) from emp b where b.deptno=a.deptno group by b.deptno) as deptcount from dept a; 
DEPTNO DNAME          LOC            DEPTCOUNT
------ -------------- ------------- ----------
    10 ACCOUNTING     NEW YORK               6
    20 RESEARCH       DALLAS                 5
    30 SALES          CHICAGO                6
    40 OPERATIONS     BOSTON        
    50 50abc          50def         
    60 Developer      HaiKou     

6 rows selected

  • 16.列出各种工作的最低工资。
SELECT DISTINCT
    JOB,
    MIN(SAL)
FROM
    emp
GROUP BY
    JOB

SQL> select job,avg(sal) from emp group by job;

JOB         AVG(SAL)
--------- ----------
ANALYST         3500
CLERK         1037.5
Developer       5500
MANAGER   2758.33333
PM              5500
PRESIDENT       5000
SALESMAN        1400

7 rows selected

  • 17.列出各个部门的MANAGER(经理)的最低薪金。
SELECT DISTINCT
    DEPTNO,
    MIN(SAL)
FROM
    emp
WHERE
    JOB = 'MANAGER'
GROUP BY
    DEPTNO

SQL> select deptno,min(sal) from emp where job='MANAGER' group by deptno;

DEPTNO   MIN(SAL)
------ ----------
    10       2450
    20       2975
    30       2850

  • 18.列出所有员工的年工资,按年薪从低到高排序。
SELECT DISTINCT
    ENAME,
    SAL * 12 + IFNULL(COMM, 0)
FROM
    emp
ORDER BY
    SAL * 12 + IFNULL(COMM, 0)

SQL> select ename,(sal+nvl(comm,0))*12 as salpersal from emp order by salpersal;

ENAME       SALPERSAL
---------- ----------
SMITH            9600
JAMES           11400
ADAMS           13200
MILLER          15600
TURNER          18000
WARD            21000
ALLEN           22800
CLARK           29400
MARTIN          31800
BLAKE           34200
JONES           35700
FORD            36000
SCOTT           48000
KING            60000
EricHu          66168
huyong          66168
WANGJING        66168

17 rows selected

第二篇

<meta charset="utf-8">

    1. 找出EMP表中的姓名(ENAME)第三个字母是A 的员工姓名。
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '__A%';

ENAME
----------
ADAMS
BLAKE
CLARK

    1. 找出EMP表员工名字中含有A 和N的员工姓名。
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '%A%' AND ENAME LIKE '%N%';

ENAME
----------
ALLEN
MARTIN
WANGJING
--------或--------
SQL> SELECT ENAME FROM SCOTT.EMP WHERE ENAME LIKE '%A%N%';

ENAME
----------
ALLEN
MARTIN
WANGJING

    1. 找出所有有佣金的员工,列出姓名、工资、佣金,显示结果按工资从小到大,佣金从大到小。
SQL> SELECT ENAME,SAL + COMM AS WAGE,COMM
  2  FROM SCOTT.EMP
  3  ORDER BY WAGE,COMM DESC;

ENAME            WAGE      COMM
---------- ---------- ---------
TURNER           1500      0.00
WARD             1750    500.00
ALLEN            1900    300.00
MARTIN           2650   1400.00
EricHu           5514     14.00
WANGJING         5514     14.00
huyong           5514     14.00
SMITH                 
JONES                 
JAMES                 
MILLER                
FORD                  
ADAMS                 
BLAKE                 
CLARK                 
SCOTT                 
KING                  

17 rows selected   

    1. 列出部门编号为20的所有职位。
SQL> SELECT DISTINCT JOB FROM EMP WHERE DEPTNO = 20;

JOB
---------
ANALYST
CLERK
MANAGER

    1. 列出不属于SALES 的部门。
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME <> 'SALES';

DEPTNO DNAME          LOC
------ -------------- -------------
    10 ACCOUNTING     NEW YORK
    20 RESEARCH       DALLAS
    40 OPERATIONS     BOSTON
    50 50abc          50def
    60 Developer      HaiKou
   110 信息科         海口

6 rows selected

--或者:
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME != 'SALES';
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME NOT IN('SALES');
SQL> SELECT DISTINCT * FROM SCOTT.DEPT WHERE DNAME NOT LIKE 'SALES';

    1. 显示工资不在1000 到1500 之间的员工信息:名字、工资,按工资从大到小排序。
SQL> SELECT ENAME,SAL + COMM AS WAGE FROM SCOTT.EMP
  2  WHERE SAL + COMM NOT BETWEEN 1000 AND 1500
  3  ORDER BY WAGE DESC;

ENAME            WAGE
---------- ----------
EricHu           5514
huyong           5514
WANGJING         5514
MARTIN           2650
ALLEN            1900
WARD             1750

6 rows selected
--或者
SQL> SELECT ENAME,SAL + COMM AS WAGE FROM SCOTT.EMP
  2  WHERE SAL + COMM < 1000 OR SAL + COMM > 1500
  3  ORDER BY WAGE DESC;

ENAME            WAGE
---------- ----------
EricHu           5514
huyong           5514
WANGJING         5514
MARTIN           2650
ALLEN            1900
WARD             1750

6 rows selected

    1. 显示职位为MANAGER 和SALESMAN,年薪在15000 和20000 之间的员工的信息:名字、职位、年薪。
SQL> SELECT ENAME 姓名,JOB 职位,(SAL + COMM) * 12 AS 年薪
  2  FROM SCOTT.EMP
  3  WHERE (SAL + COMM) * 12 BETWEEN 15000 AND 20000
  4  AND JOB IN('MANAGER','SALESMAN');

姓名       职位              年薪
---------- --------- ----------
TURNER     SALESMAN       18000

    1. 说明以下两条SQL语句的输出结果:
      SELECT EMPNO,COMM FROM EMP WHERE COMM IS NULL;
      SELECT EMPNO,COMM FROM EMP WHERE COMM = NULL;
SQL> SELECT EMPNO,COMM FROM EMP WHERE COMM IS NULL;

EMPNO      COMM
----- ---------
 7369 
 7566 
 7698 
 7782 
 7788 
 7839 
 7876 
 7900 
 7902 
 7934 

10 rows selected

---------------------------------------------------------------
SQL> SELECT EMPNO,COMM FROM EMP WHERE COMM = NULL;

EMPNO      COMM
----- ---------

--说明:IS NULL是判断某个字段是否为空,为空并不等价于为空字符串或为数字0;
--而 =NULL 是判断某个值是否等于 NULL,NULL = NULL和NULL <> NULL都为 FALSE。

    1. 让SELECT 语句的输出结果为
      SELECT * FROM SALGRADE;
      SELECT * FROM BONUS;
      SELECT * FROM EMP;
      SELECT * FROM DEPT;
      ……
      列出当前用户有多少张数据表,结果集中存在多少条记录。
SQL> SELECT 'SELECT * FROM '||TABLE_NAME||';' FROM USER_TABLES;

'SELECT*FROM'||TABLE_NAME||';'
---------------------------------------------
SELECT * FROM BONUS;
SELECT * FROM EMP;
SELECT * FROM DEPT;
--......等等,在此不列出。

    1. 判断SELECT ENAME,SAL FROM EMP WHERE SAL > '1500'是否抱错,为什么?
SQL> SELECT ENAME,SAL FROM EMP WHERE SAL > '1500';

ENAME            SAL
---------- ---------
ALLEN        1600.00
JONES        2975.00
BLAKE        2850.00
CLARK        2450.00
SCOTT        4000.00
KING         5000.00
FORD         3000.00
EricHu       5500.00
huyong       5500.00
WANGJING     5500.00

10 rows selected

SQL> SELECT ENAME,SAL FROM EMP WHERE SAL > 1500;

ENAME            SAL
---------- ---------
ALLEN        1600.00
JONES        2975.00
BLAKE        2850.00
CLARK        2450.00
SCOTT        4000.00
KING         5000.00
FORD         3000.00
EricHu       5500.00
huyong       5500.00
WANGJING     5500.00

10 rows selected
--说明不会抱错,这儿存在隐式数据类型的。

第三篇

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,163评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,301评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,089评论 0 352
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,093评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,110评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,079评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,005评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,840评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,278评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,497评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,667评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,394评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,980评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,628评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,649评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,548评论 2 352