SELECT: Reading Data
SELECT reads data from a table. You already used SELECT * to get every column. Most of the time you only need a few columns, so you name them.
Syntax
SELECT column1, column2, ... FROM table_name;
Example: only the columns you need
SELECT name, age FROM employees;
Output:
name | age
-------+-----
Emma | 26
Jack | 33
Sarah | 28
Alice | 28
Henry | 29
Bob | 32
David | 30
Grace | 31
Frank | 27
Mike | 24
Lucy | 30
John | 25
(12 rows)
You get the same 12 rows, but only two columns. Asking only for what you need is a good habit in Cassandra. Less data has to travel from the nodes back to you.
Renaming a result column with AS
AS gives a column a new name in the result. The table itself does not change.
SELECT name AS employee_name FROM employees;
Output:
employee_name
---------------
Emma
Jack
Sarah
Alice
Henry
Bob
David
Grace
Frank
Mike
Lucy
John
(12 rows)
Rows still come in hash order
Look at the order of the rows: Emma, Jack, Sarah, Alice and so on. It is the same order as in your first query. Cassandra returns rows in the order of the hash of their key, not by name or by id. You can sort rows in Cassandra, but only in a way that is planned when the table is created. The Primary Keys section shows how.
No joins
CQL has no joins. A SELECT always reads from exactly one table. In a relational database you might join three tables when you run a query. In Cassandra you create one table that already holds everything the query needs. You will see this idea many times in this course.
In the exercise, show only two columns of the employees table.