Site icon DataFlair

Impala OFFSET Clause – 5 Min Tutorial

Impala OFFSET Clause - 5 Min Tutorial

Impala OFFSET Clause - 5 Min Tutorial

In our last tutorial, we discussed Limit Clause and Having Clause. Now, we will discuss Impala Offset Clause, used to decide from where the output should be considered in Impala. Moreover, we will learn introduction, syntax, and example of Impala Offset clause, to understand it well.

So, let’s start Impala Offset Clause.

Impala OFFSET Clause

Basically, in a SELECT query, this clause causes the result set to start some number of rows after the logical first item. However, OFFSET 0 produces the same result as leaving out the OFFSET clause since the result set is numbered starting from zero.

Also, make sure always use this clause in combination with ORDER BY. Hence it is clear which item should be first, second, and so on. Whereas LIMIT covers a bounded range, such as items 0-9, 100-199, and so on.

So, we can combine a LIMIT clause with an OFFSET clause to produce a small result set that is different from a top-N query, in Impala 1.2.1 and higher.

Further, let’s understand this with an example. In order to return items 11 through 20 with simulate “paged” results, we will use this technique.

Moreover, use this technique only for compatibility in cases where you cannot rewrite the application logic since Impala queries typically involve substantial amounts of I/O.

However, let’s define it in simple words. Since we very well know the rows in the resultset of a select query starts from 0. So, to decide from where the output should be considered we use the offset clause.

For example,  the result will be as usual if we choose the offset as 0. In the same way, the result starts from the fourth row if we choose the offset as 4.

A Syntax of Offset Clause in Impala

So, the syntax for using Impala OFFSET Clause is-

select data from table_name Group BY col_name;

Impala Offset Clause Example 

Lets take an example of Impala offset clause,
Basically, to understand, let’s suppose we have a table named Employees in the database my_db. Further, its contents are −

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

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

Fetched 9 row(s) in 0.51s
Further, using limit and order by clauses, here we can arrange the records in the table in the ascending order of their id’s and limit the number of records to 4.

Query: select * from Employees order by id limit 4

id name age address salary
1 shubham 32 delhi 20000
2 monika 25 mumbai 15000
3 kajal 27 alirajpur 40000
4 revti 25 indore 35000

Fetched 4 row(s) in 0.64s
Moreover, here is an example of the offset clause. Basically, we are getting the records in the Employees table in the order of their id’s and printing the first four rows starting from the 0th row.

[quickstart.cloudera:21000] > select * from Employees order by id limit 4 offset 0;
So, we get the following result, on executing, the above query.

Query: select * from Employees order by id limit 4 offset 0

id name age address salary
1 shubham 32 delhi 20000
2 monika 25 mumbai 15000
3 kajal 27 alirajpur 40000
4 revti 25 indore 35000

Fetched 4 row(s) in 0.62s
Also, we can get four records from the Employees table starting from the row having offset 5.

[quickstart.cloudera:21000] > select * from Employees order by id limit 4 offset 5;
Query: select * from Employees order by id limit 4 offset 5

6 mehul 22 hyderabad 32000
7 Vaishnavi 25 Goa 23000
8 Rishabh 22 chennai 31000
9 Pulkit 23 Gandhi nagar 28000


Fetched 4 row(s) in 0.52s
So, this was all about Impala Offset Clause. Hope you like our explanation.

Conclusion

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

Exit mobile version