HiveQL – Select-Joins

  • Post author:
  • Post category:Hive
  • Post comments:2 Comments

JOIN is a clause that is used for combining specific fields from two tables by using values common to each one. It is used to combine records from two or more tables in the database.

Syntax

join_table:

   table_reference JOIN table_factor [join_condition]
   | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference
   join_condition
   | table_reference LEFT SEMI JOIN table_reference join_condition
   | table_reference CROSS JOIN table_reference [join_condition]

Example

We will use the following two tables in this chapter. Consider the following table named CUSTOMERS..

+----+----------+-----+-----------+----------+ 
| ID | NAME     | AGE | ADDRESS   | SALARY   | 
+----+----------+-----+-----------+----------+ 
| 1  | zafrul   | 32  | Ahmedabad | 2000.00  |  
| 2  | debi     | 25  | Delhi     | 1500.00  |  
| 3  | subrat   | 23  | Kota      | 2000.00  | 
| 4  | jay      | 25  | Mumbai    | 6500.00  | 
| 5  | ajit     | 27  | Bhopal    | 8500.00  | 
| 6  | satya    | 22  | MP        | 4500.00  | 
| 7  | subu     | 24  | Indore    | 10000.00 | 
+----+----------+-----+-----------+----------+

Consider another table ORDERS as follows:

+-----+---------------------+-------------+--------+ 
|OID  | DATE                | CUSTOMER_ID | AMOUNT | 
+-----+---------------------+-------------+--------+ 
| 102 | 2009-10-08 00:00:00 |           3 | 3000   | 
| 100 | 2009-10-08 00:00:00 |           3 | 1500   | 
| 101 | 2009-11-20 00:00:00 |           2 | 1560   | 
| 103 | 2008-05-20 00:00:00 |           4 | 2060   | 
+-----+---------------------+-------------+--------+

There are different types of joins given as follows:

  • JOIN
  • LEFT OUTER JOIN
  • RIGHT OUTER JOIN
  • FULL OUTER JOIN

JOIN

JOIN clause is used to combine and retrieve the records from multiple tables. JOIN is same as OUTER JOIN in SQL. A JOIN condition is to be raised using the primary keys and foreign keys of the tables.

The following query executes JOIN on the CUSTOMER and ORDER tables, and retrieves the records:

hive> SELECT c.ID, c.NAME, c.AGE, o.AMOUNT 
FROM CUSTOMERS c JOIN ORDERS o 
ON (c.ID = o.CUSTOMER_ID);

On successful execution of the query, you get to see the following response:

+----+----------+-----+--------+ 
| ID | NAME     | AGE | AMOUNT | 
+----+----------+-----+--------+ 
| 3  | subrat   | 23  | 3000   | 
| 3  | subrat   | 23  | 1500   | 
| 2  | debi     | 25  | 1560   | 
| 4  | jay      | 25  | 2060   | 
+----+----------+-----+--------+

LEFT OUTER JOIN

The HiveQL LEFT OUTER JOIN returns all the rows from the left table, even if there are no matches in the right table. This means, if the ON clause matches 0 (zero) records in the right table, the JOIN still returns a row in the result, but with NULL in each column from the right table.

A LEFT JOIN returns all the values from the left table, plus the matched values from the right table, or NULL in case of no matching JOIN predicate.

The following query demonstrates LEFT OUTER JOIN between CUSTOMER and ORDER tables:

hive> SELECT c.ID, c.NAME, o.AMOUNT, o.DATE 
FROM CUSTOMERS c 
LEFT OUTER JOIN ORDERS o 
ON (c.ID = o.CUSTOMER_ID);

On successful execution of the query, you get to see the following response:

+----+----------+--------+---------------------+ 
| ID | NAME     | AMOUNT | DATE                | 
+----+----------+--------+---------------------+ 
| 1  | zafrul   | NULL   | NULL                | 
| 2  | debi     | 1560   | 2009-11-20 00:00:00 | 
| 3  | subrat   | 3000   | 2009-10-08 00:00:00 | 
| 3  | subrat   | 1500   | 2009-10-08 00:00:00 | 
| 4  | jay      | 2060   | 2008-05-20 00:00:00 | 
| 5  | ajit     | NULL   | NULL                | 
| 6  | satya    | NULL   | NULL                | 
| 7  | subu     | NULL   | NULL                | 
+----+----------+--------+---------------------+

RIGHT OUTER JOIN

The HiveQL RIGHT OUTER JOIN returns all the rows from the right table, even if there are no matches in the left table. If the ON clause matches 0 (zero) records in the left table, the JOIN still returns a row in the result, but with NULL in each column from the left table.

A RIGHT JOIN returns all the values from the right table, plus the matched values from the left table, or NULL in case of no matching join predicate.

The following query demonstrates RIGHT OUTER JOIN between the CUSTOMER and ORDER tables.notranslate”> hive> SELECT c.ID, c.NAME, o.AMOUNT, o.DATE FROM CUSTOMERS c RIGHT OUTER JOIN ORDERS o ON (c.ID = o.CUSTOMER_ID);

On successful execution of the query, you get to see the following response:

+------+----------+--------+---------------------+ 
| ID   | NAME     | AMOUNT | DATE                | 
+------+----------+--------+---------------------+ 
| 3    | subrat   | 3000   | 2009-10-08 00:00:00 | 
| 3    | subrat   | 1500   | 2009-10-08 00:00:00 | 
| 2    | debi     | 1560   | 2009-11-20 00:00:00 | 
| 4    | jay      | 2060   | 2008-05-20 00:00:00 | 
+------+----------+--------+---------------------+

FULL OUTER JOIN

The HiveQL FULL OUTER JOIN combines the records of both the left and the right outer tables that fulfil the JOIN condition. The joined table contains either all the records from both the tables, or fills in NULL values for missing matches on either side.

The following query demonstrates FULL OUTER JOIN between CUSTOMER and ORDER tables:

hive> SELECT c.ID, c.NAME, o.AMOUNT, o.DATE 
FROM CUSTOMERS c 
FULL OUTER JOIN ORDERS o 
ON (c.ID = o.CUSTOMER_ID);

On successful execution of the query, you get to see the following response:

+------+----------+--------+---------------------+ 
| ID   | NAME     | AMOUNT | DATE                | 
+------+----------+--------+---------------------+ 
| 1    | Ramesh   | NULL   | NULL                | 
| 2    | debi     | 1560   | 2009-11-20 00:00:00 | 
| 3    | subrat   | 3000   | 2009-10-08 00:00:00 | 
| 3    | subrat   | 1500   | 2009-10-08 00:00:00 | 
| 4    | jay      | 2060   | 2008-05-20 00:00:00 | 
| 5    | ajit     | NULL   | NULL                | 
| 6    | satya    | NULL   | NULL                |
| 7    | subun    | NULL   | NULL                |  
| 3    | subrat   | 3000   | 2009-10-08 00:00:00 | 
| 3    | subrat   | 1500   | 2009-10-08 00:00:00 | 
| 2    | debi     | 1560   | 2009-11-20 00:00:00 | 
| 4    | jay      | 2060   | 2008-05-20 00:00:00 | 
+------+----------+--------+---------------------+

This Post Has 2 Comments

  1. marketing

    You actually make it seem so easy with your presentation but I find this
    matter to be actually something that I think I would never
    understand. It seems too complicated and very broad for me.
    I’m looking forward for your next post, I will try to get the hang of it!

  2. S K S

    Thank you for your appriciation comment

Leave a Reply