Query Language Snippets

All Snippets
Redis Combined
Use HSET and HGET for hash operations.
HSET user:1 name "Alice" age 30
HGETALL user:1
Redis Combined
Use ZADD and ZRANGE for sorted set operations.
ZADD mysortedset 1 "member1"
ZRANGE mysortedset 0 -1 WITHSCORES
MongoDB Combined
Aggregate documents with $group, $sort, $match, etc.
db.orders.aggregate([
  { $match: { status: "complete" } },
  { $group: { _id: "$customerId", total: { $sum: "$amount" } } },
  { $sort: { total: -1 } },
  { $limit: 5 }
]);
MongoDB Combined
Use transactions for multi-document ACID operations.
const session = db.getMongo().startSession();
session.startTransaction();
try {
  db.accounts.updateOne({ _id: "A" }, { $inc: { balance: -100 } }, { session });
  db.accounts.updateOne({ _id: "B" }, { $inc: { balance: +100 } }, { session });
  session.commitTransaction();
} catch (e) {
  session.abortTransaction();
}
session.endSession();
Redis Combined
Use LPUSH and LRANGE for list operations.
LPUSH mylist "item1"
LRANGE mylist 0 -1
Redis Combined
Use SADD and SMEMBERS for set operations.
SADD myset "member1"
SMEMBERS myset
SQL Combined
Combine results with UNION ALL (includes duplicates).
SELECT name FROM table1 UNION ALL SELECT name FROM table2;
SQL Combined
Combine results with UNION (removes duplicates).
SELECT name FROM table1 UNION SELECT name FROM table2;
Cypher Combined
Use MERGE to create or match a node.
MERGE (n:Person {name: 'Alice'})
RETURN n;
MongoDB Combined
Use $unwind to deconstruct an array field.
db.orders.aggregate([
  { $unwind: "$items" },
  { $group: { _id: "$items.product", total: { $sum: "$items.quantity" } } }
]);
SQL Combined
Use a transaction (BEGIN, COMMIT, ROLLBACK).
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
-- In case of error:
ROLLBACK;
Python Combined
SQLite3 basic CRUD operations.
import sqlite3
conn = sqlite3.connect('test.db')
c = conn.cursor()
c.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('Alice', 30))
conn.commit()
c.execute('SELECT * FROM users WHERE age > ?', (25,))
print(c.fetchall())
conn.close()