Union Vs. Joins

Wat is the basic difference between a Union and a Join??
Rgds,
Chandru
[83 byte] By [chandru_244] at [2007-11-20 1:30:41]
# 1 Re: Union Vs. Joins
Union : Combines the results of two or more queries into a single result set consisting of all the rows belonging to all queries in the union.

Join : Using Joins you can retrieve data from multiple tables based on the logical relationship between the tables.
Shuja Ali at 2007-11-9 13:44:13 >
# 2 Re: Union Vs. Joins
The question does not make sense. A JOIN is very different from a UNION. They are completely different concepts/constructs. Read up on each in details from books online/articles and you would understand what you are asking about.
exterminator at 2007-11-9 13:45:13 >
# 3 Re: Union Vs. Joins
Wat is the basic difference between a Union and a Join??

Rgds,
Chandru


suppose you have 2 tables, and we will represent them with # symbol:

# and #

if we UNION them together it makes the results long and thin:
#
#

if they are joined, the result is short and fat:
##

can your imagination see more of the difference now?

use UNION when you must add more results to this result. e.g. you have a query that returns 3 males info, and you want to add on 4 girls to make that up to 7 people, you union them together:

SELECT * FROM males
UNION
SELECT * FROM females

1, john, saggitarius
2, matthew, aries
3, luke, leo
4, cindy, aries
5, selma, saggitarius
6, britney, leo
7, christina, aries

JOIN is used to stick sets of related results together

SELECT * FROM males JOIN females USING(star_sign)
1, john, saggitarius, 5, selma, saggitarius
2, matthew, aries, 4, cindy, aries
2, matthew, aries, 7, christina, aries
3, luke, leo, 6, britney, leo

can you see the difference? one is long and thin, the other short and wide
cjard at 2007-11-9 13:46:12 >
# 4 Re: Union Vs. Joins
suppose you have 2 tables, and we will represent them with # symbol:

# and #

if we UNION them together it makes the results long and thin:
#
#

if they are joined, the result is short and fat:
##

Hey!!!!

This's the funniest and the clearest explanation about the difference between UNION and JOIN that I've ever seen!!!
It's the best.

Congratulation Mr. Cjard :).

PS. take a look at "Location" in the up-left corner of your posts: if you are English, you'll come from UNITED KINGDOM, not UNTIED KINGDOM, isn'it? :):)
davide++ at 2007-11-9 13:47:19 >
# 5 Re: Union Vs. Joins
:D

I occasionally find myself teaching people things and I try to make it relevant to something they know - the UNION/JOIN thing was something I had to teach a while back and it got a good response from the guys who were non database (they can just about manage select * from x where y) so i thought i'd use it again in the future..

As for my location.. i did once consider writing untied kinkdom but then, being a moderator, I have a somewhat more professional designation and purpose so it seemed a little too tongue in cheek. I left it as untied.. my little rebellion:)
..
cjard at 2007-11-9 13:48:18 >
# 6 Re: Union Vs. Joins
Hi, newbie in SQL here. I have a question regarding Union

There's 2 tables Sales History and Purchase History which I'd like to join

1)Table 1: Sales History

field:Item field:Sales Amt
A $10
B $10

2) Table 2: Purchase History

field: Item field:Purchase Amt
A $10
C $10

If I do UNION, result is

field:Item field:Sales Amt_Purchase Amt
A $10
A $10
B $10
C $10

How can I make it to look like this;

field:Item field:Sales Amt field:Purchase Amt
A $10
A $10
B $10
C $10

Appreciate all your help, thank you in advance.
ode2eba at 2007-11-9 13:49:19 >
# 7 Re: Union Vs. Joins
Hi all.

Your question isn't clear for me...
If you want to know what is the "table origin" for each record selected using the UNION, you should write something like this:

SELECT Item,
Sales_Amt AS Amt
'SALES' AS Origin
FROM sales_history
UNION ALL
SELECT Item,
Purchase_Amt AS Amt
'PURCHASE' AS Origin
FROM Purchase_history

You'll get

Item Amt Origin
A $10 SALES
A $10 PURCHASE
B $10 SALES
C $10 PURCHASE

I hope this will help you
davide++ at 2007-11-9 13:50:23 >
# 8 Re: Union Vs. Joins
Thanks davide..

Your suggestion might work, I will try it first.
How do I save the view statement when this error appears:
View definition includes no output columns or includes no items in the FROM clause.
ode2eba at 2007-11-9 13:51:16 >
# 9 Re: Union Vs. Joins
Hi all

What database are you using?
In any case you cannot save your view unless it's right.
Probably you've forgot to set the fields list in the SELECT clause, or table's name in the FROM clause.
davide++ at 2007-11-9 13:52:25 >
# 10 Re: Union Vs. Joins
Im using MS SQL Server 2000. When I run the statement it shows out the data fine, only when saving the statement this error appears.

Thanks again
ode2eba at 2007-11-9 13:53:26 >
# 11 Re: Union Vs. Joins
anybody?
ode2eba at 2007-11-9 13:54:25 >
# 12 Re: Union Vs. Joins
Hi, newbie in SQL here. I have a question regarding Union

There's 2 tables Sales History and Purchase History which I'd like to join

If you want to join, you JOIN, not union. If you want to union, you UNION, not join

Please dont say you have a question about UNION because you want to JOIN. Its like saying you have a question about SAILING to america because you want to FLY on there on a plane.

How can I make it to look like this;

field:Item field:Sales Amt field:Purchase Amt
A $10
A $10
B $10
C $10

Appreciate all your help, thank you in advance.

(
SELECT
Item,
SalesAmt,
NULL as PurchaseAmt
FROM
Sales

UNION ALL

SELECT
Item,
NULL as SalesAmt,
PurchaseAmt
FROM
Purchases
)
ORDER BY Item

See what we did? Make each of our 2 column tables into fake 3 columns, and put data in one column or the other, not the same!
cjard at 2007-11-9 13:55:23 >
# 13 Re: Union Vs. Joins
Hi all.

Your question isn't clear for me...
It wasnt clear for me until i pressed QUOTE reply.

OP doesnt seem to realise his nicely formatted post is lost and all values look like a single column..
cjard at 2007-11-9 13:56:19 >
# 14 Re: Union Vs. Joins
That's what I was looking for, thank you.. You both helped a lot..
ode2eba at 2007-11-9 13:57:25 >