Solved

Subquery vs Join

Posted on 2014-09-10
6
134 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 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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]
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 45

Assisted Solution

by:Vitor Montalvão
Vitor Montalvão earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks both
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now