tags:

views:

180

answers:

6

Got into a heated debate (all caps sentences were involved) with a bunch of friends

In SQL,

create table test(
    id int,
    code varchar(10),
    name varchar(30)
)
insert into test values (1,'BE','BENGALOORU')
insert into test values (2,' CH','CHENNAI')
insert into test values (3,' DE','DELHI')
insert into test values (4,'MU','MUMBAI')
select name from test where code in ('BE','CH','DE','MU')
drop table test

What is the result of this query ?

a. BENGALOORU, CHENNAI, DELHI, MUMBAI
b. BENGALOORU, MUMBAI
c. BENGALOORU
d. BENGALOORU, CHENNAI, DELHI 
e. None of above options since there is no order by
+3  A: 
mysql> select name from test where code in ('BE','CH','DE','MU');
+------------+
| name       |
+------------+
| BENGALOORU | 
| MUMBAI     | 
+------------+
2 rows in set (0.00 sec)

but in truth, the ordering is arbitrary and depends on the underlying implementation of the db store. tables are not inherently ordered. if you want a specific ordering, use an ORDER BY clause.

Igor
+2  A: 

The result of the query should be (b) but anyone who argues against (e) is very brave - if there is no 'order by' then the order is undefined, and it is an unordered set.

Sounds like a silly argument to have. If you want an order, specify an 'order by' clause.

edit: Answer (e) is very poorly worded. It should state 'the result is undefined and it not guaranteed to be any of the above'. It is incorrect to state 'none of the above', because it's quite likely to result in (b), even if it's not true that it will 'always' be (b).

Kirk Broadhurst
And yet you argue against (e) :-) I haven'y downvoted anyone here but I need to stress that (e) is the correct answer. There is absolutely *no* guarantee that a DBMS will deliver (b) for this query.
paxdiablo
No I don't argue against (e), I'm just saying that if you run the presented query that's what you will likely get out. I guarantee that you won't receive a error stating 'YOU MUST SPECIFY AN ORDER!' I just ran it and got answer (b), therefore (e) - which states that (b) is not correct - must be false.
Kirk Broadhurst
edited my answer to reflect this. It's really a garbage question and any argument is only caused by poor communication and semantics.
Kirk Broadhurst
@Kirk: Answer (e) is the only answer that allows for a nondeterministic order of results. That's why it's the best answer of the options given. Imagine another DBMS comes along; it may never return answer (b), since the specifically correct answer is "either BENGALOORU, MUMBAI or MUMBAI, BENGALOORU". The answer corresponding to that is (e).
Jeff Meatball Yang
@Jeff - so is answer (e) correct? I posit that none of the answers are correct.
Kirk Broadhurst
A: 

The sensible answer is (b).

This is because those two rows do have code in ('BE', 'CH', 'DE','MU'), so those two will return true; thus the rows will be selected. The extra spaces in the codes for Chennai and Delhi mean that it's not an exact match, as the comments below rightly point out.

Technically, you are not guaranteed to have (b) as the answer, as database results are not guaranteed to be in any order unless you specify an "order by". But if you're after the set of results, you'll get (b). If order of results is important to you, then (e) is more "correct".

Smashery
You're missing the ' CH' and ' DE' gotcha.
Kirk Broadhurst
this is incorrect. the in construct is equivalent to '=' - meaning exact equality.
Igor
Right you both are - thanks! Fixed now.
Smashery
+1  A: 

e. The order of returned results only depends on the order of insertion where there has been no fragmentation.

We saw this in a large database where we got by for years on reports without sorting on the RecirdId (which was am Identity field). For years, the results came back in numerical order without an order by clause. However, one day the reports started coming out in a goofy order.

We added an Order By clause AND created jobs to rebuild the indexes more frequently.

David Stratton
"The order of returned results only depends on the order of insertion" - SQL guarantees no such thing.
paxdiablo
Yeah, you're right. I should have known better than to phrase it like that.
David Stratton
+3  A: 

The inserted order will not necessarily be the order in the result set.

The answer COULD be b., but nothing in the SQL specification for SELECT guarantees it (without ORDER BY clause, the order is undefined).

The best answer is e.

Jeff Meatball Yang
+1 for being definite about (e). See all my other voluminous comments for details :-)
paxdiablo
wow, someone down-voted me without an explanation. I hope that person will come forward with a reason.
Jeff Meatball Yang
+2  A: 

I'd edit this into one of the exisiting answers, but alas, not enough reputation ...

I think that everybody agrees on the following:

  1. The result set consists of the two values in answer (b) in no particular order.
  2. The SQL standard does not dictate any order that these two values are returned from the statement. So (e) is the formally correct answer.
  3. There seems to be some evidence that there is logic to the order a database would return the values, either order of insertion or a primary key "index". That means that most of the time, the result returned will be deterministic, including possibly (b).
  4. As noted in (2), this is not guaranteed, so if you want an order or rely on it, use an order by clause.

Hope this sums all the answers up.

IronGoofy
Well, I *do* have the rep, so I'm correcting your typos and voting you up. This is a good summary.
paxdiablo