Real-Time Fraud Detection with SQL: A Stream Processing Approach
Real-time fraud detection flags suspicious transactions within milliseconds — before they settle — by analyzing patterns across streaming data. With a streaming database like RisingWave, fraud detection rules are SQL materialized views that continuously evaluate every transaction against velocity checks, behavioral anomalies, and risk scores, all queryable via PostgreSQL protocol.
Why Streaming Matters for Fraud
Batch fraud detection (running hourly) misses fraud in progress. A fraudster making 10 transactions in 5 minutes across 5 countries won't be caught until the next batch run — by then, the money is gone.
Streaming fraud detection evaluates every transaction in real time:
CREATE MATERIALIZED VIEW fraud_signals AS
SELECT user_id,
COUNT(*) FILTER (WHERE txn_time > NOW() - INTERVAL '5 minutes') as txns_5min,
COUNT(DISTINCT country) FILTER (WHERE txn_time > NOW() - INTERVAL '1 hour') as countries_1h,
SUM(amount) FILTER (WHERE txn_time > NOW() - INTERVAL '1 hour') as total_1h,
MAX(amount) / NULLIF(AVG(amount), 0) as amount_spike_ratio
FROM transactions GROUP BY user_id;
CREATE MATERIALIZED VIEW fraud_alerts AS
SELECT * FROM fraud_signals
WHERE txns_5min > 5 OR countries_1h > 3 OR amount_spike_ratio > 10;
Key Fraud Detection Patterns
| Pattern | SQL Logic | What It Catches |
| Velocity | COUNT in time window | Rapid-fire transactions |
| Geo-anomaly | COUNT DISTINCT country | Multi-country in short time |
| Amount spike | MAX/AVG ratio | Unusually large transactions |
| New device | LEFT JOIN device history | First-time device usage |
| Time anomaly | EXTRACT(HOUR) | Transactions at unusual hours |
Frequently Asked Questions
Can SQL handle real-time fraud detection?
Yes. SQL materialized views in a streaming database evaluate every transaction against fraud rules within milliseconds. For simple rule-based detection (velocity, geo-anomaly, amount spikes), SQL is sufficient. For ML-based scoring, combine SQL features with an external model via UDFs.
What latency is needed for fraud detection?
Payment processors require fraud decisions within 100-200ms of the transaction. RisingWave materialized views update within sub-100ms, making SQL-based fraud detection fast enough for most payment flows.

