✅ Day 9: Advanced Queries & Aggregations
๐ฏ Objective:
Learn how to perform advanced queries, use aggregation functions, and understand ALLOW FILTERING in Cassandra.
๐ 1. Aggregation Functions in Cassandra
Cassandra supports limited aggregation functions:
| Function | Description |
|---|---|
COUNT(*) |
Count rows |
MAX(column) |
Maximum value in a column |
MIN(column) |
Minimum value in a column |
AVG() |
❌ Not supported natively |
SUM() |
❌ Not supported |
WRITETIME() |
Shows write timestamp of a column |
TTL() |
Shows remaining TTL of a column |
๐งช 2. Example: Count Records
SELECT COUNT(*) FROM test_lab.employee;
๐ 3. Example: MIN / MAX
SELECT MIN(joined_on), MAX(joined_on) FROM test_lab.employee;
⏰ 4. WRITETIME and TTL
SELECT WRITETIME(department), TTL(department)
FROM test_lab.employee
WHERE emp_id = <UUID>;
⚠️ 5. Using ALLOW FILTERING
Cassandra requires partition key for most queries. If not used, you must add ALLOW FILTERING.
➤ Example:
SELECT * FROM test_lab.employee
WHERE department = 'Finance' ALLOW FILTERING;
⚠️ Use
ALLOW FILTERINGonly for testing or small datasets. It can hurt performance.
๐ 6. Pagination with LIMIT and OFFSET (Simulated)
Cassandra doesn’t support OFFSET, but you can simulate paging:
SELECT * FROM test_lab.employee LIMIT 5;
- For real pagination, use
token(partition_key)with a cursor-like method in application logic.
๐งช Day 9 Lab Tasks
- Use
COUNT,MIN,MAXonemployeeandexam_scorestables - Check
WRITETIMEandTTLon any column - Run a query using
ALLOW FILTERINGon a non-key column - Try paginating with
LIMIT
✅ Checklist
| Task | Done |
|---|---|
Used COUNT, MIN, MAX functions |
✅ |
Fetched WRITETIME and TTL for a column |
✅ |
Executed query with ALLOW FILTERING |
✅ |
Tested result limiting with LIMIT |
✅ |
Tags:
Cassandra