Basically, there are 4 types of Hive Join. Such as:
The records common to the both tables will be retrieved by this inner join The simplest kind of join is the inner join, where each match in the input tables results in a row in the output.Consider the above said two tables
SELECT table1.col1, table2.col1
FROM table2
JOIN table1
ON (table1.matching_col = table2. matching_col);
students.id students.name students.city
1 ABC London
2 BCD Mumbai
3 CDE Bangalore
4 DEF Mumbai
5 EFG Bangalore
Table 2 (Dept):
dept.did dept.name dept.sid
101 Maths 1
102 Physics 2
103 Chem 3
> row format delimited
> fields terminated by '\t';
hive> select * from student ;
> row format delimited
> fields terminated by '\t';
hive> seelct * from dept ;
hive> desc student ;
OK
sid int
sname string
city string
Time taken: 0.071 seconds, Fetched: 3 row(s)
hive> desc dept ;
OK
did int
dname string
dsid int
Time taken: 0.072 seconds, Fetched: 3 row(s)
hive > select student.*,dept.*
> from student JOIN dept
> ON (student.sid = dept.dsid);
OK
1 ABC London 100 CS 1
1 ABC London 101 Maths 1
2 BCD Mumbai 102 Physics 2
3 CDE Bangalore 103 Chem 3
Example 2: Using Insert table method(only one record can be loaded into the table)
Table 2 (products):
Query:
ram 2
sita 4
raju 0
rani 3
vani 2
hive> select * from products ;
OK
2 football
4 cricketball
3 hat
1 bat
Left Outer Join
This type of join returns all rows from the left table even if there is no matching row in the right table. Table returns all rows from the left table and matching rows from right table. Unmatched right tables records will be NULL.
Syntax:
Example 1:
Query:
hive> select student.*,dept.*
> from student LEFT OUTER JOIN dept
> ON (student.sid = dept.dsid);
OK
1 ABC London 100 CS 1
1 ABC London 101 Maths 1
2 BCD Mumbai 102 Physics 2
3 CDE Bangalore 103 Chem 3
4 DEF Mumbai NULL NULL NULL
5 EFG Bangalore NULL NULL NULL
Query:
ram 2
sita 4
raju 0
rani 3
vani 2
hive> select * from products ;
OK
2 football
4 cricketball
3 hat
1 bat
hive> select sales.*,products.*
> from sales LEFT OUTER JOIN products
> ON ( sales.id = products.pid);
OK
ram 2 2 football
sita 4 4 cricketball
raju 0 NULL NULL
rani 3 3 hat
vani 2 2 football
Right Outer Join
It returns all rows from the right table even if there is no matching row in the left table. Table returns all rows from the right table and matching rows from left table. Unmatched left table records will be NULL.
Syntax:
Query:
hive> select student.*,dept.*
> from student RIGHT OUTER JOIN dept
> ON (student.sid = dept.dsid);
OK
1 ABC London 100 CS 1
1 ABC London 101 Maths 1
2 BCD Mumbai 102 Physics 2
3 CDE Bangalore 103 Chem 3
Query:
ram 2
sita 4
raju 0
rani 3
vani 2
hive> select * from products ;
OK
2 football
4 cricketball
3 hat
1 bat
hive> select sales.*,products.*
> from sales RIGHT OUTER JOIN products
> ON ( sales.id = products.pid);
OK
ram 2 2 football
vani 2 2 football
sita 4 4 cricketball
rani 3 3 hat
NULL NULL 1 bat
Full Outer Join
It returns all rows from the both tables that fulfill the JOIN condition. The unmatched rows from both tables will be returned as a NULL.
Syntax:
SELECT table1. col1, table1.col2, table2.col1, table2.col2
FROM table1
FULL OUTER JOIN table2 ON (table1.matching_col = table2.matching_col);
Results:
student_id student_name dept_id dept_name
1 ABC 100 CS
1 ABC 101 Maths
2 BCD 102 Physics
3 CDE 103 Chem
4 DEF NULL NULL
5 EFG NULL NULL
Example 2:
Query:
ram 2
sita 4
raju 0
rani 3
vani 2
hive> select * from products ;
OK
2 football
4 cricketball
3 hat
1 bat
No comments:
Post a Comment