IN: Matching Several Keys
Sometimes you need a few rows and you know their keys. You could write one query per key. With IN, one query is enough.
Syntax
SELECT columns FROM table_name
WHERE key_column IN (value1, value2, ...);
Example: three employees at once
SELECT id, name FROM employees
WHERE id IN (9, 1, 5);
Output:
id | name
----+-------
1 | Alice
5 | Emma
9 | Mike
(3 rows)
The list says 9, 1, 5, but the rows come back as 1, 5, 9. With IN on the key, Cassandra returns the rows sorted by the key value, not in the order of your list. Notice that this is also not the hash order of a full SELECT, where Emma (id 5) comes first.
Keep the list short
For each value in the list, Cassandra does a separate lookup. Each lookup can go to a different node, and the coordinator has to wait for all of them before it can answer. A list of 3 or 10 keys is fine. A list of hundreds of keys makes one query do hundreds of reads all over the cluster. In that case, several smaller queries work better.
So use IN for a few keys that you already know.
In the exercise, get three employees with one IN query.