Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Common Interview Question

Author  Topic 

DarkCloudInc
Starting Member

10 Posts

Posted - 2006-05-11 : 12:57:38
In my experience of going through plenty of interviews, I have came across a question that I am reminded of by someone's signature.

The common question in this instance is:
What is the advantage of a cursor over a stored procedure? Vice Versa?

I could only, in my so few experiences, have answered that stored procedure consumes less system resources than a cursor. The advantage a cursor has is record by record examination and looping capabilities.

Needless to say that whenever asked those questions, I haven't landed the position. So I'm wondering, what is the more appropriate answer?

Kristen
Test

22859 Posts

Posted - 2006-05-11 : 13:10:34
"The common question in this instance is"

Really? It seems like a daft question to me:

"What is the advantage of a cursor over a stored procedure? Vice Versa?"

Well ... you could put a cursor in a SProc, or you could use one without an SProc [dynamic SQL]. So I can't see how one can replace the other - an Sproc might encapsulate a Cursor.

If I was asked that question I would ask what they were looking for because they are not substitutes for each other.

If its a trick question then I'm surprised more than one person is asking it, so they are probably just reading the questions off a canned list - which is probably on the internet somewhere!

Kristen
Go to Top of Page

DarkCloudInc
Starting Member

10 Posts

Posted - 2006-05-11 : 13:23:44
I admit that I'm still a bit young and inexperienced as I am about to reach one year after graduating from college this summer with only contract jobs under my belt.

I guess it shows how little experience. But like out of 20 interviews that I have had in my last round of unemployment, about 2-3 of them asked; however, those that asked were permanant full-time positions.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-05-11 : 13:43:45
Its still a daft question, IMHO!

I reckon the people who asked that interview question know so little that its a good thing you didn't get a job there!

Kristen
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-05-11 : 13:58:48
The answer is that is sql server, cursors have a very limited use and cause poor performance.

Cursors mostly only ever need to be used when a call to an existing process needs to be done which is either a stored procedure or some external process.

In my experience that is, maybe, 1% of the time.

If this is not sql server, then the answer is going to be different


Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

DarkCloudInc
Starting Member

10 Posts

Posted - 2006-05-11 : 14:08:04
I suppose its one of those questions that would've required me to extract more information concerning the context.

Thanks all for your responses.

Well, while I'm here... is it normal to come into a company that doesn't have an available schema or documentation of databases?
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-05-11 : 15:03:28
why whatever do you mean?

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

DarkCloudInc
Starting Member

10 Posts

Posted - 2006-05-11 : 15:27:14
Not being able to detect if the question is asked in wit or sincerity, I will answer.

This is coming from a very limited experience of about two companies, but both companies had either no schema of their databases available. The documentation was fair in the last company. In this company, there is close to no schema or documenation available on the databases.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-05-11 : 15:58:21
I've seen both....the ones without good documentatio are usually a disaster...which is good if you are an hourly consultant and bad if you're an employee

As one consultant said to me, "chaos means money"




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

TimS
Posting Yak Master

198 Posts

Posted - 2006-05-11 : 16:38:10
I wish I had documentation where I work, at least for the time being, I am involved in a project for which I really really wish I was getting a written list of requirements, but I know it a pipe dream.

I wish that I could see what good documentation looks like so I could do some myself. But, I followed the standard set by my former boss which was to only lightly comment the stored procedures.

Tim S
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-05-12 : 01:32:49
"lightly comment the stored procedures"

Sounds alright - no point commenting anything obvious (to someone "skilled in the art").

A descriptive block at the top, and a change history, and comments for things which are non obvious and, in long Sprocs, some "section header" comments is enough IMO.

Kristen
Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2006-05-12 : 01:48:41
Sounds like a trick question.
Anybody trying to compare them would fail, as it shows a major lack of understanding.

I might try using it myself next time



Damian
"A foolish consistency is the hobgoblin of little minds." - Emerson
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-05-12 : 02:02:00
"Anybody trying to compare them would fail, as it shows a major lack of understanding"

Ah .... NOW I see why its a popular question

"I might try using it myself next time"

Hehehe ... good idea!

Kristen
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2006-05-12 : 10:27:00
I prefer to ONLY use Cursors...they are very tasty



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
Go to Top of Page

druer
Constraint Violating Yak Guru

314 Posts

Posted - 2006-05-12 : 10:45:22
I think that the question is probably a good one. It will clearly weed out those that have "heard/read" about them, and those that actually have experience with them. If they even try to respond and offer an answer, not realizing they are not mutually exclusive, as the interviewer you can tune them out and start thinking about what your plans for the weekend. When their mouth stops moving, you ask the next question or end the interview.

Hope it helps,
Dalton

Blessings aren't so much a matter of "if they come" but "are you noticing them."
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-05-12 : 11:13:26
It's just a matter of religion.
People who use cursors should be branded, beaten severely ostracised from society and (it's immaterial as to whether the cursor would be the faster solution).
These people are probably the type who have a social life, girl friends and such.
Strange creatures.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

DarkCloudInc
Starting Member

10 Posts

Posted - 2006-05-12 : 12:27:13
Wow.... well, this is certainly a heads up. Thanks for your feedback.

Seeing as I'm still entry to junior-ish level, is it to be expected that this question will come up at an entry level database position?

Out of sincere curiousity, how many years of experience in sql do all of you have that had posted?
Go to Top of Page

twhelan1
Yak Posting Veteran

71 Posts

Posted - 2006-05-12 : 13:13:35
Seems like I recall cursors being quite common in Oracle code... The thing is, there are very few cases I can think of where a cursor is preferable (read the only way possible) to perform a task, even in Oracle. Maybe it's just that Oracle programmers tend to like to do things the hard way.

I interviewed for an Oracle data analyst postion years ago, and they actually sat me down at a terminal with some common data manipulation tasks and asked me to write the SQL to handle those tasks. When I was finished the interviewer said I didn't answer some questions correct because his solution used cursors. I told him mine was correct and worked, moreover it didn't use cursors and would outperform his... needless to say I didn't get that job...

~Travis
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2006-05-12 : 13:15:18
If you want to try this, the reply that I personally would give is "Cursors? They suck. Set-based operations rule. Do you put sugar in your coffee with tweezers or a spoon?"

See how they react to that. And make them actually answer it.

And if you happen to be interviewed by a programmer/developer/DBA who thinks cursors are good, walk out. Don't even excuse yourself, just leave. You don't want to work in that shop.
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2006-05-12 : 13:18:49
"is it to be expected that this question will come up at an entry level database position"

The issues to me would be whether you can do (i.e. "think in terms of") Set Based coding, or only sequential / loop based coding.

The first is much MUCH better, but hard to find in programmers-converted-to-database-coders, and in general requires more experience before you find it comes naturally.

Have a look at the Best Split Functions and see if you can easily understand the set-based ones!

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=Best%20split%20functions

Kristen
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-05-12 : 14:55:36
"What is the advantage of a cursor over a stored procedure? Vice Versa?"


Just tell the interviewer it's a stupid question.

You can execute stored procedures from a cursor loop, and you can declare cursors in a stored procedure, so it really isn't a matter of one approach or the other. It makes no more sense than asking what is the advantage of driving over listening to the radio.

You might offer to discuss the advantages of set based processing over row at a time cursor processing.







CODO ERGO SUM
Go to Top of Page
    Next Page

- Advertisement -