Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

database

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pls tell me to find out second highest element from the database using sql.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
select SECOND_HIGHEST from DATABASE.
Don't blame me, this is a Meaningless Drivel after all.
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. Select the highest element.
2. Delete it.
3. Select the highest element.
4. Insert the record you deleted in the step #2.

The record selected in step #3 should give you the second highest element.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mani Ram:

The record selected in step #3 should give you the second highest element.



Unfortunately you've changed the result by measuring it. What was once the second highest element is now the highest!
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Ess:


Unfortunately you've changed the result by measuring it. What was once the second highest element is now the highest!



But I have nullified the changes by inserting the record again in step #4.
At the moment when I picked up the record, it was the highest. But at the end of step #4, what I have in hand is the second highest!
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You either know where the highest value is, or how fast it is moving, but not both.
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Silliness like this may work.

select max( pen.yourField )
from yourTable pen
where pen.yourField != ( select max( ult.yourField )
from yourTable ult )

Use at your own risk.
[ September 07, 2007: Message edited by: Michael Matola ]
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...

I will tell you something that you might find useful, I have faced such questions in interviews

The best way to handle questions in SQL is what my teacher told me long back to think in terms of sets.

The answer to your question might be something like

select * from table as tb where 2=(select count(distinct col) from table where tb.col<=col)

Just reflect on this query and have fun ( If you cannot understand then google for it)
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mani Ram:


But I have nullified the changes by inserting the record again in step #4.
At the moment when I picked up the record, it was the highest. But at the end of step #4, what I have in hand is the second highest!



*shakes fist* I should know better than to post before my morning coffee!
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amit Saste:
pls tell me to find out second highest element from the database using sql.


In the event this is a serious question, please use the JDBC forum up top.

And my joking answer: use SQL to return all the elements. Find highest. Find second highest.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeanne Boyarsky:
And my joking answer: use SQL to return all the elements. Find highest. Find second highest.



If you read them all starting from the bottom, the second last one is the second one.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Bartender
Posts: 1158
20
Mac OS X IntelliJ IDE Oracle Spring VI Editor Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey with the SQL



I got this result set back:

"The heaviest element known to science was recently identified by
physicists.

The element, tentatively named Administratium, has no protons or electrons
and thus has an atomic weight of 0. However, it does have one neutron, 125
assistant neutrons, 745 vice-neutrons and 111 assistant vice-neutrons, for
an atomic number of 312. The 312 particles are held together by a force
that involves the continuous exchange of meson-like particles called morons.

Since it has no electrons, Administratium is inert. However, it can be
detected chemically, as it impedes every action with which it comes in
contact. According to the discoverers, one reaction that normally requires
less than one second, was extended to four days by the presence of a minute
amount of Administratium.

Administratium has a half-life of approximately three years, at which time
it does not actually decay, but instead undergoes a reorganization in which
assistant neutrons, vice-neutrons, and assistant vice-neutrons exchange
places. Some studies suggest that its atomic mass actually increases in
each reorganization.

Research at other laboratories indicates that Administratium occurs
naturally in the atmosphere. It tends to concentrate at certain points,
such as government agencies, large corporations, universities and churches, and can usually be found in the newest, best-appointed and best-maintained
buildings.

Scientists point out that Administratium is known to be toxic at any level
of concentration and can easily destroy any productive reaction where it is
allowed to accumulate. Attempts are being made to determine how
Administratium can be controlled to prevent irreversible damage, but
results to date are not promising."

Clever these database things - arn't they...
 
Bartender
Posts: 1205
22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In MS SQL Server 2005:


This is pretty flexible as well. If you want the eighth, ninth and tenth highest records, change the TOP(1) to TOP(3) and the TOP(2) to TOP(10).

Granted, it's not efficient, but it works.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Amit Saste:
pls tell me to find out second highest element from the database using sql.



Pls go and find out second highest element from the database using sql.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't there a aggreate function for doing the same.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this query, works in DB2

SELECT MAX(COL_NAME) FROM TABLE_NAME
WHERE COL_NAME < ( SELECT MAX(COL_NAME) FROM TABLE_NAME)

-Thanks
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic