Click here to Skip to main content
14,920,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,
As we know subquery take time in execution. What is the best way to remove use of subquery?

I have 2 table one is Table1 another is Table2. There is one to many mapping like Table2 has multiple record for a single Table1 record . i want to get a single row for each table 1 row.

For example if Table 1 has a row with id: 1 and Table 2 has 3 rows with reference id 1.

i don't want to get 3 rows, when i will apply join then there will be 3 rows as result, I want to get first record only from table 2 with a Table1 row. How can i achieve this?
Updated 23-May-15 1:16am
OriginalGriff 23-May-15 7:00am
We couldn't even begin to say.
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
We have no idea what your query is, does, or what subqueries it might use!
Use the "Improve question" widget to edit your question and provide better information.
Michael_Davies 23-May-15 13:23pm
Perhaps setting LIMIT 1 in the right place. As we can not see your SQL can not rightly say..
ZurdoDev 23-May-15 16:34pm
The only way to do it without derived table (sub query) is to put something in your join statement to limit just 1 record per table 1.

Derived table is the right way to go. If it's slow, optimize that but don't try to change methods.

1 solution

Try the below if it is ok.

Select * from Table1 as T1 inner join (Select Top 1 * From Table2 Where Id = 1 Order by Id Desc) as T2 on T1.Id = T2.Id
amit_83 26-May-15 2:12am
Hi willington,
It does not resolve the issue but its much much help full to get an idea. That will resolve my issue.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900