January 29, 2009 at 9:24 am
I was asked in interview to write T-SQL statement to retrieve a single entry at row #6 and column #2 from a table without looking
January 29, 2009 at 9:25 am
Do you know how to write a Select statement?
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
January 29, 2009 at 9:33 am
Can u kindly post the t-sql statement instead of asking question. Mr. genius.
January 29, 2009 at 9:55 am
the interview question was a test of your basic knowledge on how to query for data.
you failed.
If you can't answer it, it would show that you have no experience in even writing a query.
insulting people who are trying to help will not get you anything.
if they gave you the table name and column2's name,
I can think of at least two ways to get the 6th row of any table, one using row_number() function and the other using nested TOP() functions. , three ways if the "6th row" mean WHERE the table had an identity field, and they meant 6 row mean thatfield = 6
read up on those, and practice. One of the best things you can do is copy and paste code you see here in teh forums, and make them work so you understand them.
if they did not give you the column name,
to get the column 2, without knowing the name of the column, would require dynamic sql and then either of the two methods above.
Lowell
January 29, 2009 at 10:14 am
Lowell (1/29/2009)
... insulting people who are trying to help will not get you anything...
here here
Ok Mr Grumpy:
-- Get a sample of records from a system table for Mr G
SELECT TOP 10 * FROM sys.objects ORDER BY object_id
-- Return the second row
WITH SomeTable AS (
SELECT*,
ROW_NUMBER() OVER ( ORDER BY object_id ) AS RowNumber
FROMsys.objects )
SELECT*
FROMSomeTable
WHERERowNumber = 2
Please note that this is for sql 2005.
Max
January 29, 2009 at 10:57 am
Hi Guys,
This is an Interesting post. Even I am fresher, I would like to know the solution.
If any one the query plz post here.
Thank you
No Body Is Perfect In This Imperfect World
January 29, 2009 at 11:02 am
syed_jafri786 (1/29/2009)
Can u kindly post the t-sql statement instead of asking question. Mr. genius.
Until you became rude and sarcastic, yes, I could do that. Now, I seem to have lost the ability. Sorry.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
January 29, 2009 at 11:17 am
Guys,
I got it. Max, Thanks a lot. Learned a lot about row_number() great.
Thanks
No Body Is Perfect In This Imperfect World
January 29, 2009 at 12:03 pm
declare @tbname as varchar(128)
declare @column as varchar(128)
set @tbname='your table name'
select @column=name from sys.columns where object_name(object_id)=@tbname and column_id=2
exec ('select '+@column+' from (select top 6 row_number() over( order by getdate()) as row,'
+@column+' from '+@tbname+') t where row=6')
February 2, 2009 at 6:29 pm
Thanks a lot Max. The reason I got mad was that he ask me whether I know select statement or not. Ofcourse I know select statement but it is not necessary I can write any query. Any way sorry for what I wrote.
February 2, 2009 at 6:48 pm
Thanks a lot Max. The reason I got mad was that he ask me whether I know select statement or not. Ofcourse I know select statement. It is like if someone ask you to teach him parallel parking and instead of giving instruction you ask him whether he knows driving or not.
February 2, 2009 at 7:08 pm
Np, you probably just hit G's funnny bone, hope the site helps you to grow your sql skills, it certainly has mine.
Max
February 2, 2009 at 7:31 pm
Max (1/29/2009)
Lowell (1/29/2009)
... insulting people who are trying to help will not get you anything...here here
... but you still answered the question, didn't you? 😉 If it were directed at you, would you still have answered?
--Jeff Moden
Change is inevitable... Change for the better is not.
February 2, 2009 at 7:51 pm
The real answer to this question should have been "Since data is not guaranteed to be in any order in a table or query result, and ORDER BY would be necessary to determine what row # 6 would be... How do you want it sorted? Also, the order of columns in a database table should never be important because the order may change. What is the name of column #2?" 😉
THAT would be the answer I'd be looking for to separate the Men from the Boys...
--Jeff Moden
Change is inevitable... Change for the better is not.
February 2, 2009 at 7:53 pm
GSquared (1/29/2009)
Do you know how to write a Select statement?
Heh... bad day, Gus? 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply