LIMIT: Getting Fewer Rows

LIMIT sets the largest number of rows a query can return.

Syntax

SELECT columns FROM table_name
LIMIT number;

Example: only 3 employees

SELECT name FROM employees LIMIT 3;

Output:

 name
-------
  Emma
  Jack
 Sarah

(3 rows)

Which 3 rows do you get? Cassandra reads rows in its usual hash order and stops when it has enough. So you get the first 3 rows of the full result. Emma, Jack and Sarah are also the first three names when you run SELECT * FROM employees;. LIMIT does not sort anything. It only cuts the result short.

Why LIMIT matters in Cassandra

Cassandra tables are often very large, with millions or billions of rows. A SELECT with no WHERE and no LIMIT on such a table asks every node to send back all of its data. That is slow, and it puts a heavy load on the cluster. When you only want to look at some sample rows, add a LIMIT. It is a good habit from your first day.

In the exercise, show the names of the first five employees.