Site icon DataFlair

Introduction to Impala UNION Clause with Example

Introduction to Impala UNION Clause with Example

Introduction to Impala UNION Clause with Example

While it comes to combine the results of two queries in Impala, we use Impala UNION Clause. There is much more to learn about Impala UNION Clause. So, let’s learn about it from this article. Apart from its introduction, it includes its syntax, type as well as its example, to understand it well.

So, let’s start Impala UNION Clause tutorial.

Introduction to Impala UNION Clause

Basically, in order to combine the result sets of multiple queries, we use the Impala UNION clause. Although, the result sets are combined by default as if the DISTINCT operator was applied.
In other words, to combine the results of two queries we the Impala Union clause.

Syntax

So, the syntax for using Impala UNION Clause is-

query_1 UNION [DISTINCT | ALL] query_2

Usage

We can say, UNION DISTINCT and the UNION keyword by itself is similar. Always,  prefer UNION ALL where practical, because eliminating duplicates can be a memory-intensive process for a large result set.

Especially, where the duplicate values are acceptable or when you know the different queries in the union will not produce any duplicates.

However,  in Impala 1.4 and higher, we do not need the LIMIT clause while an ORDER BY clause applies to a UNION ALL or UNION query.

Moreover, turn the UNION query into a subquery, SELECT from the subquery, and put the ORDER BY clause at the end, outside the subquery, in order to make the ORDER BY and LIMIT clauses apply to the entire result set.

Example

Impala UNION Clause Example,
Let’s suppose we have a table named Students in the database my_db. Its contents are−

[quickstart.cloudera:21000] > select * from Students;
Query: select * from Students

id name age address salary
1 shubham 32 delhi 20000
9 Pulkit 23 Gandhi nagar 28000
2 monika 25 mumbai 15000
4 revti 25 indore 35000
7 Vaishnavi 25 Goa 23000
6 mehul 22 hyderabad 32000
8 Rishabh 22 chennai 31000
5 shreyash 23 pune 30000
3 kajal 27 alirajpur 40000

Fetched 9 row(s) in 0.59s
Similarly,  assume we have another table named Users. Its contents are −

[quickstart.cloudera:21000] > select * from Users;
Query: select * from Users

id name age address salary
3 vishal 54 Banglore 55000
2 Shubham 44 Banglore 50000
4 Mansi 64 kolkata 60000
1 Ankur 34 kolkata 40000


Fetched 4 row(s) in 0.59s
So, here is an example of the Impala union clause. Basically,  using the UNION clause, we arrange the records in both tables in the order of their id’s and limit their number by 3 using two separate queries and joining these queries.

[quickstart.cloudera:21000] > select * from Students order by id limit 3
union select * from Users order by id limit 3;
Hence, we get the following output, on executing the above query.

Query: select * from Students order by id limit 3 union select
  * from Users order by id limit 3

id name Age Address Salary
2 monika 25 mumbai 15000
3 vishal 54 Banglore 55000
1 Ankur 34 kolkata 40000
2 Shubham 44 Banglore 50000
3 kajal 27 alirajpur 40000
1 shubham 32 Delhi 20000


Fetched 6 row(s) in 3.11s

Conclusion

As a result, we have seen the whole concept of Impala UNION Clause. Still, if any doubt occurs, feel free to ask in the comment section.

Exit mobile version