Solved

Subquery vs Join

Posted on 2014-09-10
6
136 Views
Last Modified: 2014-09-10
Can someone please explain why these produce different results:

SELECT s1.category_ID
FROM
	 (SELECT TOP(10) *
	  FROM tblRequestLines) s1
	  

SELECT s1.category_id
FROM tblRequestLines s1
	INNER JOIN 
		(SELECT
		 TOP(10) * 
		 FROM tblRequestLines) s2
	ON s1.ID = s2.ID

Open in new window

0
Comment
Question by:James Elliott
  • 2
  • 2
  • 2
6 Comments
 
LVL 47

Expert Comment

by:Vitor Montalvão
ID: 40314272
Because you don't have an ORDER BY clause.
TOP and BOTTOM keywords should work always with an ORDER BY clause otherwise you have the risk to get different results as you could saw by your own experience.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40314281
that's the main reason, but also that with the JOIN, if the ID has "duplicates", it will do a cross-tab of the results...
0
 
LVL 12

Author Comment

by:James Elliott
ID: 40314368
In terms of solution, then the Order By clause has solved my problem, but it doesn't really explain why the results are different. Surely they should be the same?


SELECT TOP(10) * FROM Table

ColumnA ---- ColumnB
1                       10
2                       9
3                       8
4                       7
5                       6
6                       5
7                       4
8                       3
9                       2
10                     1

SELECT s1.ColumnB FROM (SELECT TOP(10) * FROM Table) s1

I expect to give me

10
9
8
7
6
5
4
3
2
1

but instead, gives me

1
2
3
4
5
6
7
8
9
10

Open in new window


Does the primary query modify the execution of the bracketed subquery?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 47

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
ID: 40314396
It will try to return records sorted by a clustered index. If no clustered index is found, I think it will sort by the first column in the SELECT clause and by default in Ascending order.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 40314399
unless you specify ORDER BY, a query will never have any guaranteed "order".
neither the order of insertion, nor the order of the values nor any other order.
in practice, the DBMS engine will choose some "execution plan", and starts to fetch the data from the data pages behind.
it may even return data from pages that are (still) in memory, to avoid the disk reads...
0
 
LVL 12

Author Closing Comment

by:James Elliott
ID: 40314485
Thanks both
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql 2014,  lock limit 5 30
T-SQL: "HAVING CASE" Clause 1 23
convert null in sql server 12 31
How to construct an if else statement from existing code 6 19
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question