http://topic.csdn.net/u/20110315/13/346cbf15-2320-4b82-9fa6-8d9b61bf58d6.html?seed=1678314076&r=72162439
看了这个帖子,里面 有人这样写语句没明白这样可以实现要求
有张表testtab,字段有id, name, salary。 salary每个人都不一样。现要求用标准SQL查找出salary第三高的记录(一条)。
注意是标准SQL,不能使用PL/SQL,不能使用rownum!这是道面试题,号称让95%的应试者倒下的超难题!挑战吧!第一个做出来的人给全分!
解决语句如下几条:
select a.* from tb2 a where exists (select * from tb2 where salary>a.salary having count(*)=2)
这语句是意思是?可以第三高记录?select empno ,ename ,sal from emp a
where exists(select 1 from emp b where b.sal>a.sal having count(*)=2);select a.empno ,a.ename ,a.sal,count(*) from emp a,emp b
where a.sal<b.sal
group by a.empno ,a.ename ,a.sal
having count(*)=2;
这语句是意思是?可以第三高记录?

解决方案 »

  1.   

    select empno ,ename ,sal from emp a
    where exists(select 1 from emp b where b.sal>a.sal having count(*)=2);select a.empno ,a.ename ,a.sal,count(*) from emp a,emp b
    where a.sal<b.sal
    group by a.empno ,a.ename ,a.sal
      

  2.   

    select a.* from tb2 a where exists (select * from tb2 where salary>a.salary having count(*)=2)
    这个就是查出 比某个工资大的工资有两个的工资
    就是如果  张三的工资是第三高,所以会有两个人比他的工资高,只有他是才符合  有两人工资比他高
      

  3.   


    select a.empno ,a.ename ,a.sal,count(*) from emp a,emp b
    where a.sal<b.sal
    group by a.empno ,a.ename ,a.sal
    having count(*)=2;
    这条语句的执行过程是怎么样的?
      

  4.   


    SQL> select empno,ename,sal from emp order by sal desc;
     
                    EMPNO ENAME            SAL
    --------------------- ---------- ---------
                     8839 KING         6050.00
                     8788 SCOTT        3640.00
                     8902 FORD         3630.00
                     8566 JONES        3599.75
                     8698 BLAKE        3448.50
                     8782 CLARK        2964.50
                     8499 ALLEN        1936.00
                     8844 TURNER       1815.00
                     8934 MILLER       1573.00
                     8521 WARD         1512.50
                     8654 MARTIN       1512.50
                     8876 ADAMS        1331.00
                     8900 JAMES        1149.50
                     8369 SMITH         970.00
     
    14 rows selected
     
    SQL> 
    SQL> select empno ,ename ,sal from emp a
      2  where exists(select 1 from emp b where b.sal>a.sal having count(*)=2);
     
                    EMPNO ENAME            SAL
    --------------------- ---------- ---------
                     8902 FORD         3630.00
     
    SQL> 
    SQL> select a.empno ,a.ename ,a.sal,count(*) from emp a,emp b
      2  where a.sal<b.sal
      3  group by a.empno ,a.ename ,a.sal
      4  having count(*)=2;
     
                    EMPNO ENAME            SAL   COUNT(*)
    --------------------- ---------- --------- ----------
                     8902 FORD         3630.00          2
     
    SQL> 谁给我 稍微讲下上面 两条语句 的执行过程,怎么能得出这一条记录的?
      

  5.   

    上面两个sql,一个利用子查询,一个利用表连接.
    子查询相当于外层将数据作为参数传递到了内层子查询.
    表连接应该更好理解些.你把group by去掉再想想就比较容易明白了
      

  6.   

    SELECT a.empno aempno,
           a.ename aename,
           a.sal   asal,
           b.empno bempno,
           b.ename bename,
           b.sal   bsal
      FROM emp a, emp b
     WHERE a.sal < b.sal
      

  7.   

    select empno,ename,sal
    from (
    select empno,ename,sal,(select count(*) tttt from scott.emp a where a.sal>b.sal) tttt from scott.emp b order by sal desc
    ) c
    where c.tttt=3 
      

  8.   

    salary每个人都不一样 --->>>>>这是前提
      

  9.   


    SQL> select a.empno ,a.ename ,a.sal,count(*) from emp a,emp b
      2  where a.sal<b.sal
      3  group by a.empno ,a.ename ,a.sal
      4  having count(*)=2;上面这条语句是先做迪卡尔积,再根据a.sal<b.sal选择出91条记录(1+2+...+13),再做投影,选出a.empno ,a.ename ,a.sal, 然后做分组最后对分组的数据进行having选择并计算出count(*),得出结果。SQL> select empno ,ename ,sal from emp a
      2  where exists(select 1 from emp b where b.sal>a.sal having count(*)=2);这条语句问LZ一下, having 印象中好像不能直接更在where后面,难道在exists的子查询中可以用吗?还是我弄错了? 本机中没有测试环境,还请lz确认一下。谢谢。
      

  10.   

    在sqlplus下输入set autotrace on就可以看到语句的执行顺序了。