Results 1 to 3 of 3

Thread: sql questions

  1. #1
    Join Date
    May 2006
    Posts
    3

    sql questions

    how to write sql to list departments for which no employeees exists?

    and also

    finding most recently hired employee in each department.order by hiredate.

  2. #2
    Join Date
    May 2006
    Posts
    1

    Reply for the query

    select deptno from dept where deptno not in (select deptno from emp)

  3. #3
    Join Date
    Mar 2003
    Posts
    468
    SQL> create table dept (deptno number, deptname char(10));
    SQL> insert into dept values (1,'one');
    SQL> insert into dept values (2, 'two');
    SQL> insert into dept values (3, 'three');

    SQL> create table emp (empid number, ename char(10), deptno number, hiredate date);
    SQL> insert into emp values (10,'john',1,sysdate-10);
    SQL> insert into emp values (11,'mary',1,sysdate);
    SQL> insert into emp values (12,'alex',2,sysdate-5);

    As prior post shows for 'not employees in a department'
    SQL> select deptno from dept where deptno not in (select deptno from emp);
    DEPTNO
    ----------
    3

    For the most recently hired employee in each department, order by hiredate

    select a.deptno, b.deptname, a.empid, a.ename, a.hiredate
    from emp a, dept b
    where a.deptno = b.deptno
    and a.hiredate = (select max(c.hiredate) from emp c
    where c.deptno = a.deptno)
    order by a.hiredate;

    DEPTNO DEPTNAME EMPID ENAME HIREDATE
    ---------- ---------- ---------- ---------- ---------
    2 two 12 alex 29-APR-06
    1 one 11 mary 04-MAY-06

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •