Explain and fix AI-generated SQL
- State the grain, what one output row means, before you read any SQL
- Explain a query clause by clause in the order it runs, saying what one row is after each step
- Build a fixture small enough to answer by hand, with a row aimed at each kind of bug
- Catch a count of rows where the question counts people, a join that repeats rows, and a WHERE that turns a LEFT JOIN into an inner join
- Handle NULLs in comparisons and sums, integer division and the ends of date ranges, in SQLite and PostgreSQL
- Test generated SQL without an answer key, and know when to rewrite it instead of patching it
Four generated queries that run and are wrong
One senior data scientist's report from an OpenAI loop describes this round: the interviewer hands over modular AI-generated SQL or Python and asks what its logic is and what's wrong with it (1 report). Another OpenAI data scientist report, from September 2026, lists 20 to 25 minutes of hands-on SQL in its live technical screen (1 report). Each is a single report, but 20 minutes isn't long, so the method has to be quick. Our version of the practice reads like this:
An assistant wrote four queries against
users(user_id, signup_date, country)andevents(user_id, event, event_date): daily active users, day-7 retention, conversion by country, and the users who never purchased. Each one runs and returns a tidy table. For each, tell me what it was meant to compute, what it actually computes, and fix it.
The practice comes in four levels, one query each. Level 1 is DAILY_ACTIVE_USERS, level 2 DAY7_RETENTION, level 3 CONVERSION_BY_COUNTRY, and level 4 NEVER_PURCHASED, where the events table holds a guest purchase with no user. Each has one bug that raises no error.
You don't fix them yet. The lesson builds a routine first: read the question back, read the query in the order it runs, and check it on a fixture you can answer by hand. Then it takes the bug classes one at a time: counts, joins, NULLs, integer division and dates. At the end you fix all four queries, level by level, in an interview simulation with a 40-minute timer.
This lesson builds on the SQL window functions lesson, which wrote queries from a blank file. Here the query is already written, and the skill is reading it.
The bugs fail on meaning, so run the same six steps every time
Generated SQL fails in one particular way, and the routine follows from it.
Generated SQL runs and returns a tidy table. Where do the bugs worth asking about fail?
Write down what one output row means before you read any SQL
The routine's first step happens before you look at any SQL.
What do you write down before you read a line of the generated SQL?
Read the clauses in the order they run, and say what one row is after each
The second step reads the query itself, but not from the top down.
Which runs first: SELECT, WHERE or FROM?
A fixture is a handful of rows you can answer by hand
The third step is a fixture, and this lesson uses one throughout.
Why does the fixture give Ana two March orders?
COUNT(*), COUNT(x) and COUNT(DISTINCT x) count three different things
The first bug class is the count itself.
Five orders, two with a promo. What's COUNT(promo)?
Run the March query on the fixture, and check Austin by hand
Now run the March query from Part 1 on the fixture.
The March query counts COUNT(*) per city. In Austin, Ana ordered twice in March and Ben once. What does it report for Austin?
Joining orders to item lines repeats each order once per line
A join can change what one row is too.
Join orders to their item lines, then SUM(o.total_cents). Order 10 has two item lines. What happens to its 2500?
Count rows before and after a join to spot a fan-out without the answer
You can see a fan-out before you know the right answer.
Five orders join to six item lines. What does that row count tell you, with no answer key?
Sum the item lines to one row per order before the join
The fix changes the many side's grain before the join.
A quick patch keeps the item join and writes SUM(DISTINCT o.total_cents). Austin's orders are 2500, 1800 and 1800. What revenue does Austin get?
A WHERE condition on the right-hand table can turn a LEFT JOIN into an inner join
The next class loses rows instead of repeating them.
Every customer with their March order count: a LEFT JOIN, with the date range in WHERE. What happens to Dee, who has no orders?
Put the condition in ON, where it picks which orders match
The fix moves the condition into ON, next to the key. ON can hold more than the key. Here it attaches only the SPRING orders, and every customer stays:
SELECT c.name, COUNT(o.order_id) AS spring_orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
AND o.promo = 'SPRING'
GROUP BY c.customer_id, c.name
ORDER BY c.customer_id;
| name | spring_orders |
|---|---|
| Ana | 0 |
| Ben | 1 |
| Cy | 0 |
| Dee | 0 |
Put the March range in ON the same way, and the March question comes out right:
| name | march_orders |
|---|---|
| Ana | 2 |
| Ben | 1 |
| Cy | 1 |
| Dee | 0 |
The rows each version groups, side by side:
| Range in WHERE: join every order, then cut | |
|---|---|
| Ana, 10, 2026-03-02 | kept |
| Ana, 11, 2026-03-02 | kept |
| Ben, 12, 2026-03-15 | kept |
| Cy, 13, 2026-03-31 | kept |
| Cy, 14, 2026-04-01 | cut |
| Dee, NULL | cut |
| 3 customers left |
| Range in ON: attach only the March orders | Count |
|---|---|
| Ana, 10 and 11 | 2 |
| Ben, 12 | 1 |
| Cy, 13 | 1 |
| Dee, NULL | 0 |
| 4 customers left |
In ON, the condition decides which orders match. In WHERE, it decides which rows survive. This isn't a SQLite quirk. PostgreSQL's manual says the same: a condition in ON is applied before the join and one in WHERE after it, which doesn't matter for an inner join and matters a lot for an outer one.
In the editor, fix MARCH_ORDERS: every customer, with Dee's 0.
COUNT(*) counts the NULL row, and IS NULL on a right-hand key keeps only the rows with no match
Dee's 0 comes from COUNT(o.order_id), which skips her NULL row. Swap in COUNT(*) and it counts that row:
SELECT c.name, COUNT(*) AS march_orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
AND o.placed_at >= '2026-03-01'
AND o.placed_at < '2026-04-01'
GROUP BY c.customer_id, c.name
ORDER BY c.customer_id;
| name | march_orders |
|---|---|
| Ana | 2 |
| Ben | 1 |
| Cy | 1 |
| Dee | 1 |
One right-hand condition belongs in WHERE on purpose. IS NULL on a right-hand key keeps only the rows that found no match. That's how you ask for customers with no March orders:
SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
AND o.placed_at >= '2026-03-01'
AND o.placed_at < '2026-04-01'
WHERE o.order_id IS NULL;
| name |
|---|
| Dee |
In the editor, fix APRIL_ORDERS: all three cities, with 0 where there were no April orders.
Asked for every city with its number of April orders, 0 for none, the assistant wrote APRIL_ORDERS. Only one city comes back. Fix it so all three do.
NULL means unknown, and WHERE keeps a row only when its condition is true
NULL is behind several of these bugs, so look at it alone first.
What does NULL = NULL give in SQL?
WHERE promo <> 'SPRING' skips the orders whose promo is NULL
Not equal is the first place NULL bites.
WHERE promo <> 'SPRING' on the five orders. Which ones come back?
One NULL in the list, and NOT IN is never true
The question: promo codes nobody has used. The queries below use four pieces of syntax:
VALUES ('FREESHIP'), ('SPRING'), ('WELCOME')is a table typed into the query, one code per row.promos(code)names that table and its one column.- In a
NOT EXISTSquery, the subquery runs once for each outer row. SELECT 1is all that subquery needs to return, because only whether a row comes back matters.
WITH promos(code) AS (VALUES ('FREESHIP'), ('SPRING'), ('WELCOME'))
SELECT code
FROM promos
WHERE code NOT IN (SELECT promo FROM orders);
It returns no rows at all. NOT IN is true only when the code differs from every value in the list. Here's that check, written out for WELCOME, the code nobody used:
'WELCOME' NOT IN (SELECT promo FROM orders). The list is NULL, 'FREESHIP', 'SPRING', NULL, NULL:
| Comparison | Result |
|---|---|
'WELCOME' <> NULL | NULL |
'WELCOME' <> 'FREESHIP' | true |
'WELCOME' <> 'SPRING' | true |
'WELCOME' <> NULL | NULL |
'WELCOME' <> NULL | NULL |
| all five together | NULL, so WHERE drops WELCOME |
One unknown is enough to make the whole check unknown. NOT EXISTS asks whether a matching row exists, and never compares anything with NULL. Here it finds the customers with no orders, running the subquery once for each customer c:
SELECT c.name
FROM customers c
WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id);
| name |
|---|
| Dee |
In the editor, fix UNUSED_PROMOS with NOT EXISTS.
COUNT(x), SUM and AVG skip NULLs, and an empty SUM needs care
Aggregates are the third place NULL bites.
A city has no orders. What does SUM(total_cents) give for it after a LEFT JOIN?
Two type traps: integer division and dates stored as text
In SQLite and PostgreSQL, an integer divided by an integer drops the fraction
The question: what share of orders used a promo?
SELECT COUNT(promo) / COUNT(*) AS promo_share
FROM orders;
| promo_share |
|---|
| 0 |
2 / 5 comes out as 0. In SQLite and PostgreSQL, an integer divided by an integer is an integer, cut toward zero. COUNT returns an integer in both, so every rate built from counts can hit this.
Put the 1.0 before the division, because multiplying after it is too late
Make one operand a real number before the division runs:
SELECT COUNT(promo) / COUNT(*) * 1.0 AS cast_after,
1.0 * COUNT(promo) / COUNT(*) AS cast_before,
CAST(COUNT(promo) AS REAL) / COUNT(*) AS cast_operand
FROM orders;
| cast_after | cast_before | cast_operand |
|---|---|---|
| 0.0 | 0.4 | 0.4 |
The first column is the trap. * and / have the same precedence and run left to right:
| cast_after | cast_before | |
|---|---|---|
| Written | COUNT(promo) / COUNT(*) * 1.0 | 1.0 * COUNT(promo) / COUNT(*) |
| First step | 2 / 5 * 1.0 | 1.0 * 2 / 5 |
| Then | 0 * 1.0 | 2.0 / 5 |
| Result | 0.0 | 0.4 |
On the left, the division runs first and loses the fraction. Multiplying by 1.0 afterwards only turns 0 into 0.0. In PostgreSQL, a ::numeric cast on either operand does the job of the 1.0.
Money in cents hits the same bug. The average order in dollars:
SELECT SUM(total_cents) / COUNT(*) / 100 AS avg_dollars_int,
SUM(total_cents) / COUNT(*) / 100.0 AS avg_dollars
FROM orders;
| avg_dollars_int | avg_dollars |
|---|---|
| 19 | 19.4 |
AVG itself is safe here. SQLite's avg() returns a floating point value even when every input is an integer, and PostgreSQL's avg of an integer column returns numeric.
In the editor, fix PROMO_SHARE: each customer's share of orders with a promo, not just 0 or 1.
Asked for each customer's share of orders that used a promo, the assistant wrote PROMO_SHARE. Every share comes out exactly 0 or 1. Fix it.
BETWEEN with a bare end date loses times on the last day, so end before the next start
Dates in SQLite are text, as the SQL window functions lesson showed: SQLite has no date or time type of its own. Here placed_at is a TEXT column, so comparing two values compares strings. Strings in one consistent ISO format sort in time order, so that mostly works, until the last day of a range.
The question: orders placed in March.
SELECT order_id, placed_at
FROM orders
WHERE placed_at BETWEEN '2026-03-01' AND '2026-03-31';
| order_id | placed_at |
|---|---|
| 10 | 2026-03-02 12:15 |
| 11 | 2026-03-02 19:40 |
| 12 | 2026-03-15 08:05 |
Order 13, at 20:30 on March 31, is missing. Here's where values sort as strings, and which ones each range keeps. BETWEEN here is BETWEEN '2026-03-01' AND '2026-03-31', and half-open is >= '2026-03-01' AND < '2026-04-01'.
| Value, in string order | BETWEEN | Half-open |
|---|---|---|
'2026-03-01' | in | in |
'2026-03-02 12:15' | in | in |
'2026-03-31' | in | in |
'2026-03-31 00:00' | out | in |
'2026-03-31 20:30' | out | in |
'2026-04-01' | out | out |
'2026-04-01 09:00' | out | out |
'2026-03-31 20:30' starts with '2026-03-31' and keeps going, so it sorts after the upper bound. So does every March 31 value with a time, even midnight:
SELECT '2026-03-31 20:30' <= '2026-03-31' AS evening,
'2026-03-31 00:00' <= '2026-03-31' AS midnight;
| evening | midnight |
|---|---|
| 0 | 0 |
PostgreSQL has real timestamp types and nearly the same bug. Its docs say a date compared with a timestamp counts as midnight. So the range stops at 00:00 on March 31 and drops the rest of the day.
In either engine, use a half-open range: on or after the start, and strictly before the next start. Here it is for one day, March 2, where BETWEEN '2026-03-02' AND '2026-03-02' finds nothing at all:
SELECT order_id, placed_at
FROM orders
WHERE placed_at >= '2026-03-02' AND placed_at < '2026-03-03';
| order_id | placed_at |
|---|---|
| 10 | 2026-03-02 12:15 |
| 11 | 2026-03-02 19:40 |
The same shape works for a week or a month, and you never need to know how many days the month has.
In the editor, fix MARCH_IDS: the March orders, the late evening of the 31st included.
Subtracting two timestamps counts the hours, not the calendar days
Day counts have their own edge. Take two timestamps a calendar week apart.
julianday('2026-03-09 08:00') - julianday('2026-03-02 19:40'): about how many days is that?
Each symptom points at a few bug classes
Run, compare, and name the class of each difference. One check usually tells the classes apart.
| Symptom | Points at | The check |
|---|---|---|
| A count or sum too high | counting rows, not things; a join that repeats rows | rows before and after each join; COUNT(*) against distinct keys |
| One side's sums too high, the other side's right | a fan-out | group the joined rows by the one side's key, keep counts above 1 |
| A row or group missing | an inner join; a WHERE on a LEFT JOIN's right side; a NULL comparison | keys on the left against rows in the output |
| No rows at all | NOT IN over a NULL; a condition that's never true | run the subquery alone; count NULLs in the column |
| 1 where 0 belongs | COUNT(*) over a LEFT JOIN's empty row | count a right-hand key instead |
| NULL where 0 belongs | SUM over no rows | wrap the sum in COALESCE |
| A rate of exactly 0 or 1 | integer division | look at the numerator and denominator alone, then cast an operand |
| Off at the ends of a range | BETWEEN on timestamps; an inclusive end; time zones | fixture rows late on the last day and just after it |
A worked example
The question, read back and answered by hand
Here's one generated query, debugged from start to finish with the routine. The question:
For each city, March revenue in dollars and dishes sold. List every city, with zeros where there were no March orders.
Read it back. One row per city, so three rows: Austin, Boston, Denver. Revenue counts each March order once. Dishes add up item quantities. March runs from 2026-03-01 00:00 up to, not including, 2026-04-01.
Answer it by hand. Austin's March orders are 10, 11 and 12: 2500 + 1800 + 1800 = 6100 cents, and 1 + 2 + 3 + 1 = 7 dishes. Boston's only March order is 13, late on the 31st: 1500 cents and 1 dish. Order 14 is April. Denver has nothing.
| city | revenue | dishes |
|---|---|---|
| Austin | 61.0 | 7 |
| Boston | 15.0 | 1 |
| Denver | 0.0 | 0 |
Read in run order, the generated query has three suspects before it runs
The generated query:
SELECT c.city,
SUM(o.total_cents) / 100.0 AS revenue,
SUM(i.qty) AS dishes
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
LEFT JOIN items i ON i.order_id = o.order_id
WHERE o.placed_at BETWEEN '2026-03-01' AND '2026-03-31'
GROUP BY c.city
ORDER BY c.city;
Explain it in run order. Say what one row is after each step:
| Runs | Clause | One row after it is |
|---|---|---|
| 1 | FROM customers c | a customer |
| 1 | LEFT JOIN orders o | an order, or a row of NULLs for a customer with none |
| 1 | LEFT JOIN items i | an item line, so order 10 and its 2500 appear twice |
| 2 | WHERE placed_at BETWEEN '2026-03-01' AND '2026-03-31' | a row in the range: the NULL rows can't pass, and the range ends at a bare date |
| 3 | GROUP BY c.city | a city |
| 4 | SUM(o.total_cents) / 100.0 | a city, with each total summed once per item line |
That's three suspects before you run anything: a fan-out, a WHERE that undoes the LEFT JOIN, and a boundary.
Run the generated query on the fixture. How many of the three cities come back?
The rows under the GROUP BY show the extra 2500 and every row WHERE drops
Look at the rows the GROUP BY works on, before the WHERE gets to them. The last column is the WHERE's condition on each row:
SELECT c.city, c.name, o.order_id, o.placed_at, o.total_cents, i.qty,
o.placed_at BETWEEN '2026-03-01' AND '2026-03-31' AS in_range
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
LEFT JOIN items i ON i.order_id = o.order_id
ORDER BY c.customer_id, o.order_id, i.dish;
| city | name | order_id | placed_at | total_cents | qty | in_range |
|---|---|---|---|---|---|---|
| Austin | Ana | 10 | 2026-03-02 12:15 | 2500 | 1 | 1 |
| Austin | Ana | 10 | 2026-03-02 12:15 | 2500 | 2 | 1 |
| Austin | Ana | 11 | 2026-03-02 19:40 | 1800 | 3 | 1 |
| Austin | Ben | 12 | 2026-03-15 08:05 | 1800 | 1 | 1 |
| Boston | Cy | 13 | 2026-03-31 20:30 | 1500 | 1 | 0 |
| Boston | Cy | 14 | 2026-04-01 09:00 | 2100 | 2 | 0 |
| Denver | Dee | NULL | NULL | NULL | NULL | NULL |
Seven rows from four customers and five orders. Order 10's 2500 appears twice: that's the extra $25.00.
The WHERE keeps the four rows with a 1. It drops Cy's 20:30 order on the boundary, and Cy's April order, as it should. Dee's row gets NULL, so it goes too, and takes Denver with it.
Fix one thing at a time, and check that each fix moves only what it should
Fix 1, the rows that vanished. Move the date condition into the ON of the orders join, and delete the WHERE:
LEFT JOIN orders o ON o.customer_id = c.customer_id
AND o.placed_at BETWEEN '2026-03-01' AND '2026-03-31'
Fix 2, the boundary. Make the range half-open:
LEFT JOIN orders o ON o.customer_id = c.customer_id
AND o.placed_at >= '2026-03-01'
AND o.placed_at < '2026-04-01'
Fix 3, the fan-out. Bring items to one row per order in a CTE called dishes, join it in place of items, and sum d.dishes.
Fix 4, the blanks. The question asked for zeros, so wrap both sums in COALESCE.
Here's the output after each fix, each one run as a whole query on the fixture:
Each cell is revenue, then dishes:
| After | Austin | Boston | Denver |
|---|---|---|---|
| the generated query | 86.0, 7 | no row | no row |
| 1: range moved into ON | 86.0, 7 | NULL, NULL | NULL, NULL |
| 2: half-open range | 86.0, 7 | 15.0, 1 | NULL, NULL |
| 3: dishes CTE | 61.0, 7 | 15.0, 1 | NULL, NULL |
| 4: COALESCE | 61.0, 7 | 15.0, 1 | 0.0, 0 |
Fix 1 brings every city back, but Boston is still blank: its one March order is outside the range. Fix 2 brings it in. Fix 3 takes the extra 2500 off Austin, and fix 4 turns Denver's blanks into zeros.
After fix 4 the output matches the hand answer, and each fix changed it only where you said it would.
Say it back. It was meant to report March revenue and dishes for every city. It reported revenue only for cities with a March order before the last day, and it counted each order's total once per item line. There were three causes. The item join repeated order totals. The date filter in WHERE threw away the rows the LEFT JOIN added. And BETWEEN with a bare end date cut off the last day. I moved the range into the ON clause as a half-open range, brought items to one row per order before the join, and turned empty sums into zeros.
In the editor, fix CITY_MARCH: the generated query, one fix at a time.
What interviewers ask next
Test generated SQL with six checks that need no answer key
How would you test generated SQL before trusting it? The fixture is the first answer. The second is a set of checks that need no answer key, so they work on production data the first time you run the query:
- Rows across joins. Count before and after each join. A join to a lookup on its key keeps the count. A rise is a fan-out, a drop is an inner join or a filter.
- The grain holds. In the output,
COUNT(*)equalsCOUNT(DISTINCT key)for the key the question names. - Nothing is missing. The output has a row for every group in the base table, every city in
customers. - Parts add up. Per-group totals sum to a total computed straight from the source table, with no joins.
- Values are plausible. Rates sit between 0 and 1, and not all at one end. Counts don't exceed the population.
- NULLs are counted. For every column you filter, join or compare on, you know how many NULLs it holds.
The parts-add-up check catches the worked example with no answer key:
SELECT SUM(total_cents) / 100.0 AS march_revenue
FROM orders
WHERE placed_at >= '2026-03-01' AND placed_at < '2026-04-01';
| march_revenue |
|---|
| 76.0 |
The generated query's cities added up to 86.0. That 86.0 holds two bugs pulling in opposite directions:
| Dollars | Where it comes from |
|---|---|
| 76.0 | March revenue, straight from orders |
| +25.0 | order 10's total, counted a second time by the fan-out |
| -15.0 | order 13, cut off by BETWEEN |
| 86.0 | what the generated query's cities add up to |
Here they don't cancel, so the total catches them. Two bugs can cancel exactly in a total, so a total is one check among several, never proof.
Comparing results is how OpenAI says it evaluates its own in-house data agent: "we compare both the SQL and the resulting data" (1 source). Reading the SQL tells you what to suspect. Only the data tells you what it returned.
Patch when one piece is wrong, and rewrite when the grain is wrong
Sometimes the right fix is a new query.
When should you rewrite generated SQL instead of patching it?
These bugs all run, and each turns on a fact the question didn't spell out
Why do these bugs get past review? If you're asked which of these an assistant gets wrong most, don't invent a ranking you can't back up. Say what they share. Each one runs, returns numbers of the right shape, and turns on a fact the question didn't spell out: the grain, whether empty groups stay, what NULL means, the column types, where a range ends. Those are the facts to state when you prompt for SQL, and the ones to check when you review it.
The practice problem, start to finish
That's the routine and every bug class in the practice. The simulation hands you all four queries under a timer, so rehearse the order once.
1.After joining orders to their item lines, a teammate patches the inflated numbers with
COUNT(DISTINCT o.order_id)andSUM(DISTINCT o.total_cents). What's still wrong?2.Every customer and their March order count, 0 for none. You move the date filter from WHERE into the LEFT JOIN's ON clause but keep
COUNT(*). What does a customer with no orders get?3.
SELECT code FROM promos WHERE code NOT IN (SELECT promo FROM orders)returns nothing, though one code was never used. Why?4.Two of five orders used a promo. In SQLite, what does
SELECT COUNT(promo) / COUNT(*) * 1.0 FROM ordersreturn?5.
placed_atholds text like'2026-03-31 20:30'. Which March orders doesWHERE placed_at BETWEEN '2026-03-01' AND '2026-03-31'miss?
Where this shows up
The reports behind this lesson, each with its source.
Same L5 DS report, technical round: explain the logic of AI-generated SQL or Python and find what is wrong with it.
Same Sep 2026 DS report: the live technical screen included 20 to 25 minutes of hands-on SQL.
OpenAI evaluates its data agent by comparing both the generated SQL and the resulting data, graded with OpenAI's Evals.
“we compare both the SQL and the resulting data, and feed these signals into OpenAI’s Evals grader.”
Practice
Sources (10)
- Built-in Aggregate Functions · SQLite documentation · Nov 13, 2025
“The count(X) function returns a count of the number of times that X is not NULL in a group. The count(*) function (with no arguments) returns the total number of rows in the group.”
- SELECT · SQLite documentation · Oct 9, 2026
“In an outer join, the extra NULL rows for non-matched rows on the other operand are added after ON clause processing but before WHERE clause processing.”
- NULL Handling in SQLite Versus Other Database Engines · SQLite documentation · Apr 18, 2022
“The idea was to make SQLite work like all the other engines.”
- SQL Language Expressions · SQLite documentation · Aug 14, 2026
“Integer divide yields an integer result, truncated toward zero.”
- Date And Time Functions · SQLite documentation · Jul 11, 2026
“SQLite does not have a dedicated date/time datatype.”
- 7.2. Table Expressions · PostgreSQL 18 documentation · Sep 25, 2025
“This is because a restriction placed in the ON clause is processed before the join, while a restriction placed in the WHERE clause is processed after the join. That does not matter with inner joins, but it matters a lot with outer joins.”
- 9.2. Comparison Functions and Operators · PostgreSQL 18 documentation · Sep 25, 2025
“Ordinary comparison operators yield null (signifying “unknown”), not true or false, when either input is null.”
- 9.3. Mathematical Functions and Operators · PostgreSQL 18 documentation · Sep 25, 2025
“Division (for integral types, division truncates the result towards zero)”
- 9.21. Aggregate Functions · PostgreSQL 18 documentation · Sep 25, 2025
“In particular, sum of no rows returns null, not zero as one might expect”
- 9.9. Date/Time Functions and Operators · PostgreSQL 18 documentation · Sep 25, 2025
“Similarly, a date value is assumed to represent midnight in the TimeZone zone when comparing it to a timestamp.”