EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Hive Tutorial Joins in Hive
 

Joins in Hive

Priya Pedamkar
Article byPriya Pedamkar

Updated July 6, 2023

Joins in Hive

 

 

Introduction to Joins in Hive

Joins are used to retrieve various outputs using multiple tables by combining them based on particular columns. Now, for the tables to be in Hive, we are required to create the tables and load the data in each table. We are going to use two tables (customer and product) here for understanding the purpose.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Different Commands

Below are the commands for creating and loading the data in these tables:

For Customer Table: 6 rows

Create Command

Create an external table if not exists customer(id string, name string, city string)
row format delimited
fields terminated by ‘ ‘
location ‘/user/hive/warehouse/test.db/customer’
tblproperties (“skip.header.line.count”=”1”);

Load Command

Load data local in path ‘/home/cloudera/Customer_Neha.txt’ into table customer;

Customer Table Data

Customer Table Data

For Product table: 6 rows

Create Command

Create an external table if not exists product(Cust_Id string,
Product string, Price string)
row format delimited
fields terminated by ‘ ‘
location ‘/user/hive/warehouse/test.db/product’
tblproperties (“skip.header.line.count”=”1”);

Load command

Load data local path ‘/home/cloudera/Product_Neha.txt’ into table product;

Product Table Data

Product Table Data

Note: I am using “properties (“skip.header.line.count”=”1”)” to skip the first line of my data which is a header to me.

To check table schema, use the command “desc table name;”

product 2
Now, we have data in tables, Let’s play with it 😉

Types of Joins in Hive

Join- This will give the cross product of both the table’s data as output. As you can see, we have 6 rows in each table. So the output for Join will be 36 rows. The number of mappers-1. However, there no reduce to the operator is used.

Command

types of joins

Output:

join1 query output1

1. Full Join

Full Join without match condition will give the cross product of both tables.

Number of mappers-2

Number of reducer-1

This can be achieved using “Join” as well but with less number of mapper and reducer.

Full Join with match condition

All the rows will be joined from both tables. If rows are not matched in another table, then NULL will be populated in output (Observe Id-100,106). No rows are skipped.

Number of mappers-2

Number of reducer-1

Command

full join command

Output:

full join output

2. Inner Join

If the inner join is used without the “on” clause, it will give the cross product as the output. However, we are required to use the specific columns on which basis the join can be performed. The Id column from the customer table and Cust_id column from the product table are my specific columns. The output contains the rows where Id and Cust_Id matches. You can observe, Rows with Id-106 and Cust_Id-100 are skipped in output because they are not present in another table.

Command

Inner Join

Output:

inner join output

3. Left Join

All the rows from the left table are joined with matched rows from the right table. If the right table has rows with ids that are not present in the left table, then those rows will be skipped (Observe Cust_Id-100 in output). If the right table does not have rows with ids that are there in the left table, NULL will be populated in output (Observe Id-106 in output).

Number of Mapper-1

Number of reducer-0

Command

Left Join

Output:

Left Join output

4. Right Join

All the rows from the right table are matched with left table rows. If the left table does not have any row, then NULL will be populated (Observe Id 100). Rows from the left table will be skipped if that match is not found in the right table (Observe Id 106).

Number of Mapper-1

Number of reducer-0

Command

right join q

Output:

right join output

Conclusion

“Join” as the word suggests, can join two or more tables in the database. It is similar to joins in SQL. Joins are used to retrieve various outputs using multiple tables by combining them based on particular columns. Based on the requirement, it can be decided which join will work for you. For example, If you want to check what are the id’s present in the left table but not in the right table, you can simply use left join. Various optimizations can be performed in hive joins depending upon the complexity. Some of the examples are repartition joins, replication joins, and semi joins.

Recommended Articles

This is a guide to Joins in Hive. Here we discuss the basic concept, types of joins like full join, inner join, left join and right join in hive along with its command and output. You may also look at the following articles to learn more –

  1. Hive Commands
  2. External Table in Hive | Examples
  3. Features of Hive Alternatives
  4. Using ORDER BY Function in Hive
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW