?
Solved

Subquery vs Join

Posted on 2014-09-10
6
Medium Priority
?
140 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 51

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 143

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 51

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 1000 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Suggested Courses

762 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