News:

New Wiki available at http://wiki.gstwins.com -Check it out or contribute today!

Main Menu

SQL help...

Started by The Buddha, October 26, 2004, 09:07:25 AM

Previous topic - Next topic

The Buddha

OK this is waaaaaay off topic... but I post it in Odds and ends .... and might not get the answer... after I get the answer... I'll move it there.
Now this is a famous interview question...
From a column in a table how to find the second highest value.
There is 2 ways atleast... one is the nested query... which I know... I am looking for the other way... because the third highest, fourth highest, fifth etc... need more and more nesting with the nested query method... the other method is better.
Cool.
Srinath.
-----------------------------------------------------------------
I run a business based on other people's junk.
-----------------------------------------------------------------

cozy

in Sql Server:
SELECT top 2
FROM table
ORDER BY descending

You could prolly translate top 2  into other databse talk (maybe?)
**No matter where you go, There you are.**
2001 Ducati M750 Metallic

The Buddha

Oooooh... OK that will give me the first 2..... K we are getting somewhere... if I wanted just the second highest... The Db doesn't matter cos I have worked in all 6 (oracle, Sql server, sybase, informix, db2, teradata,and heck soem my SQL as well) I just wanna be SQL ready... not a clue what the actual env is going to be... cos I dont have a definete position yet...  :x
Cool.
Srinath.
-----------------------------------------------------------------
I run a business based on other people's junk.
-----------------------------------------------------------------

John Bates

You're definitely off my topic. :guns:

:lol:  :lol:  :lol:  :lol:
----------------------------------------------------
Bikes don't leak oil, they mark their territory.  (Joerg)
----------------------------------------------------


2002 Harley Sportster XLH883 with V&H Straight Shots
Prior owner of 1992 GS500E stock
Fairfield County, OH
USA

Phobos

Depending on the database type you could do the following:

SELECT * from table_name_here order by what_you_want_to_order_by limit 1, 1


The limit 1, 1 means you want to start on the first record and show the next  one.  It might be limit 2, 1 too.. can't remember.

This WON'T work in MS ACCESS or SQL SERVER, but most other SQL databases should.


- Phobos
-Phobos
2002 GS500E - BT45 Rear Wheel.

bluel39

I cannot test these but I would guess the following (I am bored):

generically this might work:

SELECT COLUMNNAME FROM TABLE t
WHERE N=(SELECT COUNT(*) FROM TABLE WHERE
t.COLUMNNAME < COLUMNNAME)

where N is 2 or whatever..  Mega inefficient though

In oracle you could use rownum:

SELECT DISTINCT COLUMNNAME FROM TABLE
WHERE ROWNUM = 2
ORDER BY COLUMNNAME DESC

I think in SQLServer you could do this though it might need a groupby clause rather than distinct:

SELECT MIN(COLUMNNAME)
FROM
(
SELECT DISTINCT TOP 2 COLUMNNAME
FROM TABLE
ORDER BY COLUMNNAME DESC
)

But I expect there are better ways... :? :?

TheGoodGuy

god that brings back memories...

SQL can be fun when your writing small statements, a massive 100+ line statement.. now that's a Buddha Loves You!
'01 GS500. Mods: Katana Shock, Progessive Springs, BobB's V&H  Advancer Clone, JeffD's LED tail lights & LED licence plate bolt running lights, flanders superbike bars, magnet under the bike. Recent mods: Rejet with 20/62.5/145, 3 shims on needle, K&N Lunch box.

crash

SELECT peon
FROM your_group
QUERY 'second largest number in table <table_name>'

:thumb:
* The opinions expressed in this post are those of th%&*L{P(^W@#^)*(Sasdfjkl;=235kawel;...............

2001 GS500
1996 Olds Cutlass Ciera - DEAD =(

The Buddha

Bluel39... Hey my post brought out the geek in you... in 17 months you have 8 posts, and this is one... I feel proud...
Oracle ...funny you mention that... at an interview at oracle they asked me and I tossed the nested query at them, and the interviewer asked me if I know about rownum... I thought she was referring to row ID...cos the position was in the CRM dev group, and the whole underlying premise of CRM is a reliable way to use row ID to select...and dang that lead me down the wrong path... BTWI didn't get the job... and a while later I tired that row num query in Db2... and it failed...
Cool.
Srinath.
-----------------------------------------------------------------
I run a business based on other people's junk.
-----------------------------------------------------------------

The Buddha

And your first query was the answer I was looking for... Thanks. keep the ideas comming ... I have seen that in an interview the first question you face... you pretty much knock it out of the park and the next questions promptly become non issues... they dont even ask it...
Cool.
Srinath.
PS: tommorow I'll drag this topic off to odds and ends... where it belongs but he I got bluel39 out from hiding...
-----------------------------------------------------------------
I run a business based on other people's junk.
-----------------------------------------------------------------

bluel39

Glad to be of help...

SMF spam blocked by CleanTalk