In Join, matching rows are joined side-by-side to make the
result table but whereas in Union rows are joined one-below
the other to make the result table.
Union : The union operator combines the results of two or
more queries into a single result set. But no.of columns
must match in both/all the queries (and also the order)
which are used for union.
Union -- returns with no duplicate rows
Union all -- retruns with duplicate rows (No. of rows
returned = No. of rows in Query1 + No. of rows in Query 2)
Union is a set operator.
You cannot use the union operator within a create view
statement.
You cannot use the union operator on text and image columns.
You cannot use the for browse clause in statements
involving the union operator.
Joins: Joins are used to extract information from more than
one table based on the related column/coloums (PK and RFK)
any no. of rows can be retrived based on matching matching
colums. Different types of joins are Inner join=equi join=
join, outer join(Right outer join/right join, Left outer
join/ left join), Cross join, and full outer join/outer join
The SQL JOIN clause is used whenever we have to select data
from 2 or more tables.
To be able to use SQL JOIN clause to extract data from 2
(or more) tables, we need a relationship between certain
columns in these tables.
union:
The purpose of the SQL UNION command is to combine the
results of two queries together. In this respect, UNION is
somewhat similar to JOIN in that they are both used to
related information from multiple tables. One restriction
of UNION is that all corresponding columns need to be of
the same data type. Also, when using UNION, only distinct
values are selected (similar to SELECT DISTINCT).
A Join is used for displaying columns with the same or
different names from different tables. The output displayed
will have all the columns shown individually. i.e. The
columns will be aligned next to each other.
UNION:
------
The UNION set operator is used for combining data from two
tables which have columns with the same datatype.
When a UNION is performed the data from both tables will be
collected in a single column having the same datatype.
Table t2
manufacturer_id manufacturer
1 ABC Gmbh
2 DEF Co KG
Now for performing a JOIN type the query shown below
SELECT articleno, article, manufacturer
FROM t1 JOIN t2 ON (t1.manufacturer_id =
t2.manufacturer_id);
articelno article manufacturer
1 hammer ABC GmbH
2 screwdriver DEF Co KG
That is a join.
UNION means that you have to tables or resultset with the
same amount and type of columns and you add this to
tables/resultsets together. Look at this example:
I have the table like this
S.No Name ID
01 Xyz 123
I want the result as
01Xyz123
How to write the query to retrieve the entire row data in a
single column?
IN A TABLE HAVE ONE COLUMN PRIMARY KEY..IT WILL NOT ALLOWS
NULL VALUES AND DUPLICATE VALUES..INSTEAD OF PRIMARY KEY
WHY CANT WE USE UNIQUE AND NOT NULL.THESE TWO ALSO DOESNT
ACCEPT NULL VALUES IN NOT NULL AND UNIQUE DOESNT ACCEPT
DUPLICATE VALUES?
SO WHAT IS THE DIFEERENCE BETWEEN(UNIQUE,NOT NULL) AND
PRIMARY KEY??????