CQL: Your First Query
You talk to Cassandra with CQL, the Cassandra Query Language. If you have seen SQL before, CQL will look almost the same. It uses the same keywords, like SELECT, INSERT, UPDATE and DELETE. Every statement ends with a semicolon ;.
People often type CQL into a tool called cqlsh (the CQL shell). In this course you don't need to install anything. The editor runs your CQL on a real Cassandra database. A keyspace is already set up for you (you will learn about keyspaces in the next lesson).
Reading from a table
This query reads every row from a table called employees:
SELECT * FROM employees;
Output:
id | age | department | gender | name
----+-----+------------+--------+-------
5 | 26 | IT | female | Emma
10 | 33 | Finance | male | Jack
11 | 28 | IT | female | Sarah
1 | 28 | HR | female | Alice
8 | 29 | Sales | male | Henry
2 | 32 | IT | male | Bob
4 | 30 | Marketing | male | David
7 | 31 | Finance | female | Grace
6 | 27 | Sales | male | Frank
9 | 24 | IT | male | Mike
12 | 30 | Sales | female | Lucy
3 | 25 | Sales | male | John
(12 rows)
Here are the parts of the query:
+--------+ +---+ +------+ +-----------+ +-+
| SELECT | -> | * | -> | FROM | -> | employees | -> |;|
+--------+ +---+ +------+ +-----------+ +-+
| | | | |
Read All From Table End of
data columns where name statement
The last line, (12 rows), tells you how many rows came back.
Why are the rows in a strange order?
Look at the id column: 5, 10, 11, 1 and so on. This is not the order in which the rows were added, and it is not sorted. Cassandra stores rows by the hash of their key (the number you met in the last lesson), not by the key itself. The order looks random, but it is the same every time you run the query. You will learn why in the Primary Keys section.
For now, remember this: don't expect rows to come back in any particular order in Cassandra.
Now write the query yourself. The employees table is ready for you below.