Hive Built-In Operators

There are four types of built-in operators available in Hive -

  1. Relational
  2. Arithmetic
  3. Logical
  4. Complex Operators

Relational Operators –

Relational Operators are used to compare the operands. Relational Operators generate a TRUE or FALSE value depending on condition.

Relational OperatorDescription
A = BTRUE if expression A is equivalent to expression B. Otherwise FALSE
A != BTRUE if expression A is not equivalent to expression B. Otherwise FALSE
A < BTRUE if expression A is less than expression B. Otherwise FALSE
A <= BTRUE if expression A is less than or equal to expression B. Otherwise FALSE
A > BTRUE if expression A is greater than expression B. Otherwise FALSE
A >= BTRUE if expression A is greater than or equal to expression B. Otherwise FALSE
A IS NULLTRUE if expression A evaluates to NULL. Otherwise FALSE
A IS NOT NULLFALSE if expression A evaluates to NULL. Otherwise TRUE
A LIKE BTRUE if string A matches the SQL simple regular expression B. Otherwise FALSE.
A RLIKE BNULL if A or B is NULL, TRUE if any (possibly empty) substring of A matches the Java regular expression B. Otherwise FALSE.
A REGEXP BNULL if A or B is NULL, TRUE if any (possibly empty) substring of A matches the Java regular expression B. Otherwise FALSE.
Example -
SELECT * FROM orders WHERE order_amount >= 1000;

This query will give you all the orders where the amount is greater than or equal to 1000.

Arithmetic Operators –

Arithmetic operators help you perform calculations directly inside your Hive queries. We can use them to add, subtract, multiply, or divide values in your tables.

Arithmetic OperatorsDescription
A + BGives the result of adding A and B.
A - BGives the result of subtracting B from A.
A * BGives the result of multiplying A and B.
A / BGives the result of dividing B from A.
A % BGives the reminder resulting from dividing A by B.
A & BGives the result of bitwise AND of A and B.
A | BGives the result of bitwise OR of A and B.
A ^ BGives the result of bitwise XOR of A and B.
~AGives the result of bitwise NOT of A.
Example -
SELECT item_price * quantity AS total_price FROM sales_data;

This calculates the total price by multiplying the item price with the quantity for each sale.

Logical Operators –

Logical operators are used to create logical expressions. All of them return boolean TRUE or FALSE based on the values in operands.

Logical OperatorsDescription
A AND BTRUE if both A and B are TRUE, otherwise FALSE
A && BSame as A AND B
A OR BTRUE if either A or B or both are TRUE, otherwise FALSE
A || BSame as A OR B
NOT ATRUE if A is FALSE, otherwise FALSE
!ASame as NOT A
Example -
SELECT * FROM employee WHERE department = 'Sales' AND salary > 5000;

This query fetches employees who are in the Sales department and have a salary above 5000.

SELECT * FROM customer WHERE city = 'Delhi' OR city = 'Mumbai';

This selects customers who live in either Delhi or Mumbai.

Complex Operators –

Complex operators provide tools to access elements in Complex Types.

OperatorOperand typesDescription
A[n]A is an Array and n is an int"Returns the nth element in the array A. The first element has index 0 "
M[key]M is a Map and key has type Kreturns the value corresponding to the key in the map
S.xS is a structreturns the x field of S