Home > sql > Eastern challenge

Eastern challenge

April 9th, 2009

I just wanted to give to my readers a bit fun for Eastern…

Using the well known EMP table, retrieve the name of the job that is the less common…

The solution must be a one-liner SELECT statement (max 80 chars) :)

Have fun!

Bookmark and Share

  1. April 9th, 2009 at 15:45 | #1

    So this is wrong, 85 characters:

    SELECT*FROM(SELECT job,COUNT(*)FROM emp GROUP BY job ORDER BY COUNT(*))WHERE ROWNUM=1

  2. April 9th, 2009 at 16:06 | #2

    76 must be your objective …

  3. April 9th, 2009 at 16:10 | #3

    Still 85 chars:

    SELECT*FROM(SELECT job,RANK()OVER(ORDER BY COUNT(*))r FROM emp GROUP BY job)WHERE r=1

  4. April 9th, 2009 at 16:14 | #4

    OK… 67… but it does print it out 5 times… once for each job :-)

    SELECT FIRST_VALUE(job)OVER(ORDER BY COUNT(*))FROM emp GROUP BY job

  5. April 9th, 2009 at 16:39 | #5

    select min(job)keep(dense_rank first order by count(*))from emp group by job

  6. April 9th, 2009 at 16:50 | #6

    well done all!
    I first add Sten solution in mind, but Colin first solution could be a 77
    SELECT*FROM(SELECT job FROM emp GROUP BY job ORDER BY COUNT(*))WHERE ROWNUM=1

    and Colin first_value could be a 78 by adding unique
    SELECT unique FIRST_VALUE(job)OVER(ORDER BY COUNT(*))FROM lsc_emp GROUP BY job

    (unique is a rare synonym of distinct, but shorter to type :) )

  7. April 10th, 2009 at 00:20 | #7

    SELECT job FROM emp GROUP BY job having sum(1)<=all(select sum(1)from emp GROUP BY job)

  8. April 10th, 2009 at 08:11 | #8

    yeah, sum(1) !!!

    I should have used it too, making sten solution two chars smaller :)

  9. April 12th, 2009 at 09:56 | #9

    nice and great fun !

    but the question is wrong (at least, there is something missing):
    what, if there is more than one jobs that are ‘the less common’ ?

    sokrates > select job from emp;

    JOB
    ———
    bunny
    bunny
    easter
    easter

    4 rows selected.

    which of them should the query return ?
    all ?
    a randomly chosen ?
    none ?

    happy easterday to you !

  10. April 12th, 2009 at 10:19 | #10

    agree. Well, I said : “Using the well known EMP table” so the solution could be SELECT ‘PRESIDENT’ FROM DUAL :)

  1. No trackbacks yet.