EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Hive Tutorial Joins in Hive
Secondary Sidebar
Hive Tutorial
  • Basics
    • Hive JDBC Driver
    • What is a Hive
    • Hive Architecture
    • Hive Installation
    • How To Install Hive
    • Hive Versions
    • Hive Commands
    • Hive Data Types
    • Hive Built-in Functions
    • Hive Function
    • Hive String Functions
    • Date Functions in Hive
    • Hive Table
    • Hive Drop Table
    • Hive Show Tables
    • Hive Group By
    • Hive Order By
    • Hive Cluster By
    • Joins in Hive
    • Hive Inner Join
    • Map Join in Hive
    • Hive nvl
    • Hive UDF
    • Dynamic Partitioning in Hive
    • HiveQL
    • HiveQL Queries
    • HiveQL Group By
    • Partitioning in Hive
    • Bucketing in Hive
    • Views in Hive
    • Indexes in Hive
    • External Table in Hive
    • Hive TimeStamp
    • Hive Database
    • Hive Interview Questions
    • Hive insert into

Related Courses

Hive Certification Course

Hadoop Course Training

All in One Data Science Course

Joins in Hive

By Priya PedamkarPriya Pedamkar

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.

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”);

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,584 ratings)

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
Popular Course in this category
Hive Training (2 Courses, 5+ Projects)
  2 Online Courses |  5 Hands-on Projects |  25+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Hadoop Training Program (20 Courses, 14+ Projects, 4 Quizzes)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more