Open In App

Arranging clustering column in descending order in Cassandra

Last Updated : 15 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads