How to Limit returned rows in Cassandra - Cassandra / CQL Tutorial

How to Limit returned rows in Cassandra Query Language

You can use LIMIT option in query to control the number of rows you want to get. 

Syntax:

CQLSH:techbrotherstutorials>SELECT column1, 
       column2, 
       ...FROM tablename LIMIT numberofrow;

 

Example:

Let's create employee table by using below CQL Statement.

CQLSH:techbrotherstutorials>CREATE TABLE employee 
             ( 
                          employeeid INT , 
                          fname TEXT, 
                          lname TEXT, 
                          address TEXT, 
                          age TINYINT, 
                          PRIMARY KEY((employeeid,fname),lname) 
             ) 
             WITH clustering 
ORDER BY     ( 
                          lname DESC);

Insert sample data by using CQL insert statement

INSERT INTO employee 
            (employeeid,fname,lname,address,age) 
            VALUES (1,'John','Doe','ABC Address',23);
INSERT INTO employee 
            (employeeid,fname,lname,address,age) 
            VALUES (2,'Robert','Ladson',' Address',40);
INSERT INTO employee 
            (employeeid,fname,lname,address,age) 
            VALUES (3,'M','Raza','New Address',38);
            INSERT INTO employee 
            (employeeid,fname,lname,address,age) 
            VALUES (3,'M','Ali','New Address',38);  

 

Now if we want to get only 2 records then we can use below statement. Cassandra is going to return first 2 records.

CQLSH:techbrotherstutorials>SELECT * 
FROM   employee LIMIT 2;

 

Output

 employeeid | fname | lname | address     | age
------------+-------+-------+-------------+-----
       
  3 |     M |  Raza | New Address |  38
          3 |     M |   Ali | New Address |  38

1 comment: