Finding outliers with IQR: quartiles and fences
An outlier can be a mistake or the most important event in the file. The IQR rule avoids the mean and standard deviation, so a few extreme values cannot drag the threshold along with them.
What quartiles are
Sort the values; the 25% point is Q1 and the 75% point is Q3. Their difference is the interquartile range, the spread of the middle half of the data, which extreme values barely touch.
Implementations differ in detail. QueryDeck places the rank at (n−1)×p and interpolates between neighbours, so a spreadsheet may differ in the last decimal. Record the method and the threshold together with any decision.
Computing the fences
The lower fence is Q1 − 1.5×IQR and the upper fence is Q3 + 1.5×IQR; values outside are flagged. The 1.5 is a convention, not a law — large datasets sometimes use 3.0.
For revenue with Q1 = 320,000 and Q3 = 1,250,000, the IQR is 930,000, the lower fence −1,075,000 and the upper fence 2,645,000. Orders above that upper fence are flagged.
WITH s AS (
SELECT revenue FROM orders WHERE revenue IS NOT NULL
), q AS (
SELECT
(SELECT revenue FROM s ORDER BY revenue LIMIT 1 OFFSET (SELECT COUNT(*)*1/4 FROM s)) AS q1,
(SELECT revenue FROM s ORDER BY revenue LIMIT 1 OFFSET (SELECT COUNT(*)*3/4 FROM s)) AS q3
)
SELECT ROUND(q1,0) AS q1, ROUND(q3,0) AS q3,
ROUND(q3-q1,0) AS iqr,
ROUND(q1-1.5*(q3-q1),0) AS lower_fence,
ROUND(q3+1.5*(q3-q1),0) AS upper_fence
FROM q;
When IQR helps and when it does not
For revenue, where a few large values are natural, IQR is steady: no handful of large orders can inflate a mean and blunt the threshold.
For heavily skewed data — mostly zero with a few large values — the lower fence falls below zero and left-tail outliers disappear. Log-transform the values or look at the top and bottom 1% as well.
| Rule | Strength | Caveat |
|---|---|---|
| 1.5×IQR | robust, no distribution assumption | lower tail lost when the fence is below zero |
| 3.0×IQR | very conservative | may flag nothing at all |
| top/bottom 1% | easy to explain | unstable in small samples |
| Z-score | ideal for normal data | insensitive in skewed data |
What to do with a flagged row
Open the row first. A unit error, a duplicate entry and a genuine large contract need three different responses. Keep the original, and if you exclude rows, record how many and why. QueryDeck highlights outliers; it never deletes them.
Keep a record of what you flagged
When a row is flagged, record the row, the value, the rule that caught it (IQR or Z) and the decision you made. Next month the judgement is reproducible. In QueryDeck you can inspect the highlighted rows and export the result as CSV to keep the list.
Look at several columns at once: a large revenue alongside its quantity and unit price usually shows whether it is a keying error or a genuine bulk order.
Questions
Do quartile formulas differ between tools?
Yes, interpolation details vary, so Q1 and Q3 can move slightly. When a row sits on the boundary, state the method and stay with one tool.
Should every outlier be removed?
No. It may be an error or the most important event. Do not delete before finding the cause.
Sources
Every query in this guide runs against the built-in sample data.
Open the lab