Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Arranging clustering column in descending order in Cassandra

  • Last Updated : 15 Jan, 2020

Perquisite – Cassandra
In this article, we are going to discuss how we can arrange table data and how we can arrange clustering column in descending order in a table.

To arrange clustering column in descending order first, we are going to create a table with clustering column.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 

First, we will see the results without using order by clause.
Let’s have a look.



CREATE TABLE Emp_track (
  emp_no int,
  dept text,
  name text,
  PRIMARY KEY (dept, emp_no)
); 

Now, we are going to insert some data into the table.
Let’s have a look.

insert into Emp_track(emp_no, dept, name) values (101, 'database', 'Ashish'); 
insert into Emp_track(emp_no, dept, name) values (102, 'database', 'rana'); 
insert into Emp_track(emp_no, dept, name) values (103, 'database', 'zishan'); 
insert into Emp_track(emp_no, dept, name) values (104, 'database', 'abi'); 
insert into Emp_track(emp_no, dept, name) values (105, 'database', 'kartikey');  

Now, to see the results used the following CQL query given below.

select * 
from Emp_track; 

Output:

Now, here we are going to using order by clause.
Let’s have a look.

CREATE TABLE Emp_track (
  emp_no int,
  dept text,
  name text,
  PRIMARY KEY (dept, emp_no)
)
WITH CLUSTERING ORDER BY (emp_no desc); 

Here, we will see emp_no is a clustering column and to arrange it in descending order we will use order by clause at the time of table creation.

Now, we are going to insert some data into the table.
Let’s have a look.

insert into Emp_track(emp_no, dept, name) values (101, 'database', 'Ashish'); 
insert into Emp_track(emp_no, dept, name) values (102, 'database', 'rana'); 
insert into Emp_track(emp_no, dept, name) values (103, 'database', 'zishan'); 
insert into Emp_track(emp_no, dept, name) values (104, 'database', 'abi'); 
insert into Emp_track(emp_no, dept, name) values (105, 'database', 'kartikey'); 

Now, to see the results used the following CQL query given below.

select * 
from Emp_track; 

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :