Difficulty: Beginner
Estimated Time: 10-15 minutes

Just starting out with Cassandra? A great place to start is learning about CQL CRUD operations!

In this scenario, we'll learn about the following CQL commands:


CQL looks a lot like SQL, so this is going to be easy! Let's get started!

Check out our datastax.com/dev site, where you can find many more resources to help you succeed with Apache Cassandra™.

In this scenario, you learned about the following commands:


Having successfully completed this scenario, you have a working understanding of CQL.

For more learning, try the Cassandra Fundamentals learning series on datastax.com/dev.

Was this helpful? Please give this scenario a rating (click on the stars above).

Have questions or more feedback? Send us a message.

Want a free 5 GB Cassandra database in the cloud to continue experimenting? Sign up and launch your database with a few clicks at astra.datastax.com!

Try It Out: CQL CRUD Operations in Apache Cassandra™

Step 1 of 4

The CQL INSERT Command

The C in CRUD stands for Create. In CQL we use the INSERT command to create rows.

Let's start by:

  • Creating a keyspace named user_management
  • Setting user_management as the default keyspace
  • Creating a table we can work with named users

Execute the following:

// Create the user_management keyspace CREATE KEYSPACE user_management WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }; // Set user_management as the default keyspace USE user_management; // Create the example users table CREATE TABLE users ( last_name text, first_name text, email text, address text, PRIMARY KEY((last_name), first_name, email) );

Here are the highlights of this table:

  • Four text columns: last_name, first_name, email and address
  • The partition key is the last_name column
  • The first_name and email columns are clustering columns
  • The address column is a payload column - payload columns just go along for the ride

Let's put a row in the table with the following values:

Column Name Value
last_name Smith
first_name Alex
email [email protected]
address 123 Main st.

Here's the CQL statement to add this row to the table:

INSERT INTO users (last_name, first_name, email, address) VALUES( 'Smith', 'Alex', '[email protected]', '123 Main st.');

To see the results of the statement, let's retrieve all the rows in the table:

SELECT * FROM users;

NOTE: The previous unconstrained query requires a full table scan. We would not advise using this type of query on production databases because these queries can impact performance. However, for educational purposes on small tables, it's OK.

You see the row you created. Now that you've seen the INSERT command, try it out. Insert a row with the following values:

Column Name Value
last_name Smith
first_name Bailey
email [email protected]
address 234 Elm st.
Solution INSERT INTO users (last_name, first_name, email, address) VALUES( 'Smith', 'Bailey', '[email protected]', '234 Elm st.');

Retrieve all the rows from the users table to verify your insert worked as expected.

Solution SELECT * FROM users;

ProTip: You can use the tab key for command completion and to show you command options. Also, you can scroll through the cqlsh command history using the up/down arrow keys.

Cassandra supports sparse columns, which means non-primary key columns don't have to have a value. Try creating a row with the following values (note the missing address column):

Column Name Value
last_name Jones
first_name Chris
email [email protected]
Solution INSERT INTO users (last_name, first_name, email) VALUES( 'Jones', 'Chris', '[email protected]');

Now, verify the contents of the table again to see how Cassandra displays sparse columns.

Solution SELECT * FROM users;

Congratulations! You know how to insert rows using CQL!