473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Howto use joins instead of subqueries?

TH
Hi,

Trying to get a grip on the "join" thing :)
Up until now, I allways used this kinda method:

"(select t1.a from t1 where t1.b in (select t2.ab from t2 where t2.b=0))"

How can this be accomplished using joins? And if you have the time please
explain the "bits" :)

Thank you.

--
TH
Jul 20 '05 #1
3 8085
select t1.a from t1 where t1.b in (select t2.ab from t2 where t2.b=0

Using joins it would be something like this

select t1.a from t1 join t2 on t1.b = t2.ab
where t2.b = 0

This would assume an inner join so all that would be returned would be
t1.a where there was a match between t1.b and t2.ab.

Example: If I created two simple tables:

create table table1 (ikey int)
create table table2 (ikey int)

and populated them

insert into table1 select '55' union all select '56' union all select
'57'
insert into table2 select '55' union all select '56' union all select
'57' union all select '58'

Now I can write some join statements:

select a.ikey from table1 a join table2 b on a.ikey = b.ikey

notice I am able to use an alias for the table by using it in the from
statement (NOTE: if you use an alias you have to be consistent, you can
not refer back to the table name elsewhere in the query)

You will note that the above query only returned 3 rows, not the fourth
row we inserted into table2 becuase there was no match in table1. If you
wanted to see all values in table2 you could write a right join:

select a.ikey as Tbl1_Key, b.ikey as Tbl2_Key
from table1 a right join table2 b on a.ikey = b.ikey

This will return all matching rows and any rows in table 2 (because it
is on your right in the from clause) You will see a null value for the
'58' for table 2 as there is no match in table 1.

To get more thorough explanation of joins, check out BOL (books-online)
documentation that comes with SQL Server.

HTH
Ray Higdon MCSE, MCDBA, CCNA

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #2
TH
Very nice description.
Thanks!

--
TH
Pffmppppmppffpmmpp fmpmfpmpp mpmppfmfm
mmpmppmpfppfpffmpp pffmpppfmpmfffmmffpppmfm...
<http://www.namesuppressed.com/kenny/>
Jul 20 '05 #3
JOIN has better performance than sub-query.

For your instance, you can get even better performance than JOIN if you
query like this:

select t1.a from t1
where exists ( select t2.ab from t2 where t2.b=0 and t2.ab = t1.b )

if t1 and t2 are very large tables, you will benifit a lot. Otherwise,
forget it, no difference.

George S. Z

"TH" <th_at_cogito.greatdanedk> wrote in message
news:3f***********************@dtext02.news.tele.d k...
Very nice description.
Thanks!

--
TH
Pffmppppmppffpmmpp fmpmfpmpp mpmppfmfm
mmpmppmpfppfpffmpp pffmpppfmpmfffmmffpppmfm...
<http://www.namesuppressed.com/kenny/>

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Patrick Crowley | last post by:
I'm creating a tool to browse a database of movie listings. The browser pulls up 25 results at a time, and you can page through them using 'Next' and 'Prev' tools. Pretty basic stuff. Here are...
0
by: Vijay | last post by:
Hello friends, I have a problem with one of the JOIN query here. Below is a brief description of the problem. tablename : test RecordId EffectiveDate othertableid value -------- ...
2
by: Joel | last post by:
Hi, Table Oelsls contains all products bought (history) table Invt contains the products to sell (Inventory) I'm trying with no success to list all products in Oelsls table (field name in both...
6
by: J Belly | last post by:
Hi, all: This is probably a simple problem, but, as an SQL newbie, I'm having a little trouble understanding multi-joins and subqueries. I have the following tables and columns: ...
4
by: Scott Marquardt | last post by:
My SQL acumen stems from just a couple courses, and everything since from the trenches. Fun + angst over time. I'm needing some advice on joins. Though I understand the basics, I'm having...
0
by: Joerg Ammann | last post by:
hi, os: aix 4.3.3 DB2: version 7 FP3 we are using a federated DB setup, datasource and fed-Db are both V7FP3 (in fact they are on the same server) and are having massiv performance problems....
1
by: lizandra | last post by:
Greetings, I am a newbie, I have been working to extract data from a basic sales db and trying to decide when I should use joins and when I should use subqueries. Much of what I read online says...
5
by: Bogdan | last post by:
Hi, I have a stored procedure that uses JOINs to return columns from multiple tables. I also have another stored proc that that takes a series of params and updates multiple tables. I used the...
1
by: jghouse | last post by:
Everyone, I am just starting my work with subqueries and I am a little lost on how to handle the syntax on one part. I have a query that will contain some subqueries for the purpose of counting...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.