Design a SQL query to detect anomalies in a dataset.

Instruction: Using PostgreSQL transactions(transaction_id integer, transaction_amount double precision), identify candidate outliers with continuous quartiles and 1.5 IQR fences. Use an explicit accepted domain of -1e100 to +1e100 inclusive; separately report NULL, non-finite and out-of-domain amounts. Define empty data and fence boundaries, and explain why a flag is not proof of bad data.

Context: Candidates need to demonstrate their ability to use SQL for data quality checks and basic data analysis, identifying potential issues in datasets.

Updated

Official answer

Define the population and metric first. For PostgreSQL transactions(transaction_id integer, transaction_amount double precision), this example accepts only non-NULL amounts between -1e100 and +1e100 inclusive. This deliberately bounded demonstration domain keeps double-precision interpolation and fence arithmetic away from overflow; it is not a universal monetary limit. The following uses continuous quartiles of that accepted population and flags values outside 1.5 IQR fences:

WITH clean AS (
  SELECT * FROM transactions
  WHERE transaction_amount BETWEEN -1e100::double precision
                               AND 1e100::double precision
), quartiles AS (
  SELECT percentile_cont(0.25) WITHIN GROUP (ORDER BY transaction_amount) AS q1,
         percentile_cont(0.75) WITHIN GROUP (ORDER BY transaction_amount) AS q3
  FROM clean
)
SELECT t.transaction_id, t.transaction_amount
FROM clean AS t CROSS JOIN quartiles AS q
WHERE t.transaction_amount < q.q1 - 1.5 * (q.q3 - q.q1)
   OR t.transaction_amount > q.q3 + 1.5 * (q.q3 - q.q1)
ORDER BY t.transaction_id;

For amounts 1, 2, 3, 4 and 100, Q1=2 and Q3=4, so the fences are -1 and 7 and only 100 is flagged. Values exactly on a fence are not flagged. Empty/all-invalid input yields no candidates; zero IQR and small samples need explicit interpretation. Report all excluded values separately rather than treating them as statistical outliers:

SELECT transaction_id, transaction_amount
FROM transactions
WHERE transaction_amount IS NULL
   OR NOT (transaction_amount BETWEEN -1e100::double precision
                                  AND 1e100::double precision)
ORDER BY transaction_id;

This includes NULL, infinities, NaN and finite amounts outside the stated domain. For example, -1e308 and +1e308 are excluded for separate review, not classified with these IQR fences. If those values are legitimate for the application, use an appropriate numeric representation and validated overflow-aware quantile method; do not silently apply this bounded-population answer to the full domain.

These are review candidates, not proof of fraud or erroneous data. Different segments, seasonality, skew and multiple metrics may require different reference distributions. For predictive monitoring, derive thresholds from an appropriate historical baseline rather than leaking future observations into the decision.

Related Questions