GeekInterview.com
Series: Subject:
Question: 30 of 234

Display Middle Record

How to display middle record in a given table?
Asked by: shameem_chandu | Member Since Oct-2009 | Asked on: Oct 20th, 2009

View all questions by shameem_chandu

Showing Answers 1 - 6 of 6 Answers
taaznyonker

Answered On : Mar 31st, 2010

View all answers by taaznyonker

BELOW example will give the middle row of the table(not sorted).

eg:

SELECT * FROM EMP WHERE ROWNUM <=(SELECT COUNT(1)/2 FROM EMP)
MINUS
SELECT * FROM EMP WHERE ROWNUM <(SELECT COUNT(1)/2 FROM EMP)

  
Login to rate this answer.
taaznyonker

Answered On : Mar 31st, 2010

View all answers by taaznyonker

Below query will help you

SELECT * FROM EMP WHERE ROWNUM <=(SELECT COUNT(1)/2 FROM EMP)
MINUS
SELECT * FROM EMP WHERE ROWNUM <=(SELECT COUNT(1)/2 FROM EMP)

Yes  1 User has rated as useful.
  
Login to rate this answer.

Answered On : Aug 11th, 2011

The following query works as follows: If the table has ten records, it will display the 5th and 6th record and if it has some 11 records, will display 6th record alone.

Code
  1. SELECT * FROM table_name WHERE ROWNUM <=
  2. (SELECT CASE MOD(COUNT(1),2)
  3. WHEN 0 THEN(COUNT(1)/2) + 1
  4. ELSE ROUND(COUNT(1)/2) END FROM table_name)
  5. MINUS
  6. SELECT * FROM table_name
  7. WHERE ROWNUM < (SELECT (COUNT(1)/2) FROM table_name)

  
Login to rate this answer.
sampra

Answered On : Mar 6th, 2012

View all answers by sampra

SELECT * FROM EMP WHERE ROWNUM <=(SELECT COUNT(1)/2 FROM EMP)
MINUS
SELECT * FROM EMP WHERE ROWNUM >=(SELECT COUNT(1)/2 FROM EMP)

  
Login to rate this answer.
Nazeera Jaffar

Answered On : Sep 26th, 2012

The below code returns the rows from 50-74

Code
  1. TO SELECT the middle rows:
  2.         SELECT * FROM TABLE offset 50 rows fetch next 25 rows only
  3.              

  
Login to rate this answer.
Nazeera Jaffar

Answered On : Sep 26th, 2012

The below code selects the exact middle row from the table

Code
  1. SELECT * FROM TABLE WHERE rownum=trunc(SELECT count(*)/2 FROM TABLE)

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.