Page 1 of 1

how we delete 3rd row from one table

Posted: Sun Aug 21, 2011 3:20 am
by rahul.boju
please let me know the answer thanks in advance :?

Posted: Sun Aug 21, 2011 4:35 am
by priyadarshikunal
interview question??

Posted: Sun Aug 21, 2011 8:00 am
by chulett
It's a trick question! :shock:

Posted: Sun Aug 21, 2011 10:07 am
by pandeesh
In which database?

Please let us know, how you will select only the 3rd row from your table?

Posted: Sun Aug 21, 2011 10:15 am
by chulett
Technically, your first question is irrelevant. :wink:

Posted: Sun Aug 21, 2011 10:36 am
by pandeesh
chulett wrote:Technically, your first question is irrelevant. :wink:


My guess is, if it's sql server database and there's a identity column(which is in sequence) , then we can make use of the below query:

Code: Select all


DELETE FROM table_name WHERE IDENTITY_COLUMN=3

That's why i have raised that question.

Thanks

Posted: Sun Aug 21, 2011 10:53 am
by chulett
Still, doesn't matter. Having an "identity column" (or any other surrogate value) with a 3 in it doesn't make it "the third row". It's not sequential media, after all.

That's not to say that based on very specific conditions and/or sort orders, you couldn't identify the third record in the result set. But in the table? No such thing. Like I said, a trick question. And they've already specified it is an Oracle database.

Posted: Sun Aug 21, 2011 4:13 pm
by ray.wurlod
There's no such thing, for a user, as the third row in a table. The mechanism by which rows are stored in a table, are entirely the province of the database server.

Posted: Mon Aug 22, 2011 2:55 am
by priyadarshikunal
Third row in a table as such doesn't mean anything, however a question should be asked back like, on what basis you identify which is the third row. like based on date or some other sort key.. then rank() function comes in handy to get the third row.

But still there is no answer to the question asked in first post as it is.

Posted: Mon Aug 22, 2011 4:24 am
by chandra.shekhar@tcs.com
Let KEY_1 is ur PK(or any distinct column) and ABCD is your table,
then this might help

delete from ABCD where key_1 in
(select key_1 from (
select key_1 from ABCD
fetch first 3 rows only)
order by 1 desc
fetch first row only)

@everybody
For every Question there's an answer. :P

Posted: Mon Aug 22, 2011 4:34 am
by pandeesh
Chandra ,

based on some assumptions, you have prepared the query(PK,order by desc).
But the owner of this question din't provide all those details.

Posted: Mon Aug 22, 2011 4:39 am
by chandra.shekhar@tcs.com
@pandeesh
I know what you are trying to say, but the bottomline is that the query works. :)

Posted: Mon Aug 22, 2011 5:58 am
by ray.wurlod
... but does not necessarily delete the third row from the table.

Posted: Mon Aug 22, 2011 6:19 am
by chulett
priyadarshikunal wrote:But still there is no answer to the question asked in first post as it is.
Ah... but there is, and it has been answered. :wink:

Posted: Mon Aug 22, 2011 3:30 pm
by priyadarshikunal
seems tit for tat kind of solution... you didn't give me the condition, i deleted the 3rd row encountered. You can't blame me if you didn't wanted to delete that record. :wink: