Results 1 to 7 of 7

Thread: Fetch Records

  1. #1
    Junior Member
    Join Date
    Mar 2006
    Answers
    1

    Fetch Records

    Dear All,

    Is it possible if I want to fetch record like below
    From EMP Table there are deptno 10,11,12,13....20
    I want to fetch First Dept like 13 then sorted ascending order
    like

    Deptno
    13
    10
    11
    12
    14
    15
    16
    17
    18
    19
    20

    Is it possible?

    Thanks in Advance.
    Saswata


  2. #2
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: Fetch Records

    Quote Originally Posted by dsaswata View Post
    Dear All,

    Is it possible if I want to fetch record like below
    From EMP Table there are deptno 10,11,12,13....20
    I want to fetch First Dept like 13 then sorted ascending order
    like

    Deptno
    13
    10
    11
    12
    14
    15
    16
    17
    18
    19
    20

    Is it possible?

    Thanks in Advance.
    Saswata
    Open two cursors.
    In first cursor where clause select records related to deptno 13 only .
    In Second cursor where clause specify the condition deptno <> 13.


  3. #3
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: Fetch Records

    select distinct deptno from emp where deptno = 13
    union all
    select distinct deptno from emp where deptno<>13;


  4. #4
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: Fetch Records

    First fetch the the desired first record .

    then the other cecords and use order by in the second.

    combine the both using Union.


  5. #5
    Junior Member
    Join Date
    Feb 2007
    Answers
    26

    Thumbs up Re: Fetch Records

    hey there,
    Well the answer given above is the good answer i mean that will help u out
    select deptno from employee where deptno=13
    union
    select deptno from employee where deptno <>13

    If the order by clause will be used alongwith i think it will message for an error.

    Moreover by default the records fetched are in sorted manner.

    all the best


  6. #6
    Moderator
    Join Date
    Jun 2007
    Answers
    2,074

    Re: Fetch Records

    I don't think that will throw an error if ORDER BY is added to the second part of the query at the end.


  7. #7
    Expert Member
    Join Date
    Sep 2007
    Answers
    697

    Re: Fetch Records

    You can use order by clause in the second
    select deptno from emp where deptno = 20
    union
    select deptno from emp where deptno <> 20 order by deptno;

    but what is the use?

    whether you specify order by or not , union first sorts records, then merge the records and finally eliminates duplicate records.
    Using union you can't display output like this
    20
    10
    30
    use union all.
    Further here two queries return mutually exclusive data. In such cases it is always advisable to use union all which gives better performance than union.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact