473,473 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sub Query and join Difference

111 New Member
hi,

In these two Query, for join we have to use DISTINCT but for SubQuery no need to use distinct.



SELECT DISTINCT PM.PropertyID, PM.PropertyName FROM PropertyMaster PM
JOIN PropertyManager PMgr ON PM.PropertyID = PMgr.PropertyID
JOIN CompanyRegistrationMaster CRM ON PMgr.CompanyID = CRM.CompanyID
WHERE Master = @CompanyID

SELECT PropertyName, PropertyID FROM PropertyMaster WHERE PropertyID IN
(SELECT PropertyID FROM PropertyManager WHERE CompanyID IN
(SELECT companyid FROM CompanyRegistrationMaster
WHERE master = 47))

Anybody explain me what is the difference

Thanks
Apr 18 '08 #1
2 1974
deepuv04
227 Recognized Expert New Member
hi,

In these two Query, for join we have to use DISTINCT but for SubQuery no need to use distinct.



SELECT DISTINCT PM.PropertyID, PM.PropertyName FROM PropertyMaster PM
JOIN PropertyManager PMgr ON PM.PropertyID = PMgr.PropertyID
JOIN CompanyRegistrationMaster CRM ON PMgr.CompanyID = CRM.CompanyID
WHERE Master = @CompanyID

SELECT PropertyName, PropertyID FROM PropertyMaster WHERE PropertyID IN
(SELECT PropertyID FROM PropertyManager WHERE CompanyID IN
(SELECT companyid FROM CompanyRegistrationMaster
WHERE master = 47))

Anybody explain me what is the difference

Thanks
Hi,
I think in the join we are maping a relation between two tables and retriving the data based on that relations. here the relation is one to many, and in join you get a record as many times it appears in the chaild table.
In subquery we are retriving the data from a single table where the given condition is satisfied. in this case the record exists in the table only once.

this what i am thinking. any experts comments please....

thanks
Apr 18 '08 #2
ck9663
2,878 Recognized Expert Specialist
hi,

In these two Query, for join we have to use DISTINCT but for SubQuery no need to use distinct.



SELECT DISTINCT PM.PropertyID, PM.PropertyName FROM PropertyMaster PM
JOIN PropertyManager PMgr ON PM.PropertyID = PMgr.PropertyID
JOIN CompanyRegistrationMaster CRM ON PMgr.CompanyID = CRM.CompanyID
WHERE Master = @CompanyID

SELECT PropertyName, PropertyID FROM PropertyMaster WHERE PropertyID IN
(SELECT PropertyID FROM PropertyManager WHERE CompanyID IN
(SELECT companyid FROM CompanyRegistrationMaster
WHERE master = 47))

Anybody explain me what is the difference

Thanks

Join pairs every row on the left to the record on the right. The "pairing" would depend if you're trying to get all rows that have pairs or all the ones on the left and the "paired" ones or all the ones on the right and the "paired" ones or everything.

Your subquery checks for existence. Although a WHERE can be used to join multiple tables, your query just check if an argument exists on the other tables.

-- CK
Apr 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Doug Reese | last post by:
hello, i have what seems to me a very common operation i'm performing. i need to find the balance on an invoice. i was not having any problems until the production server was upgraded to mysql...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
2
by: Mike Leahy | last post by:
Hello all, This question is related to updating tables - is there any way to calculate or update the values in a column in a table to the values in a field produced by a query result? An...
15
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows...
2
by: schoultzy | last post by:
Hello Everyone, This is probably a simple fix so be kind when/if you reply. The query below retrieves information for individuals based on a column named ATTRIB_DEF, and assorted other columns;...
30
by: Paul H | last post by:
I seem to end up with loads of append and update queries just because it's quick and easy to build queries or make a new ones based on an existing query. But I end up with loads of queries with...
5
by: Bob Bridges | last post by:
Start with two tables, parent records in one and child records in the other, a one-to-many relationship. Create a select statement joining the two. Display the query in datasheet mode. When I...
6
by: Darren | last post by:
I'm trying to build a query but struggling on how to achieve the linking. For examples, I have a table and a view similar to this.. Table: Accounts ACC_REF Char(3), DESCRIPTION VARCHAR(30),...
4
by: dsdevonsomer | last post by:
Hello, I have one simple query joining two tables with left outer join on 3 fields and using MIN on two fields. These two tables have lot of data about 3 mil in total. I am trying to migrate db...
4
by: Andrus | last post by:
DLinq objects: class Item { string ItemCode {get;set;} // primary key } class Stock { // primary key is (ItemCode,StockId) string ItemCode {get;set;} string Quantity { get; set; } string...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.