Scrivio Interview library
LeadershipAdvanced7 min read · updated 12 November 2026

NULL-related bugs keep reaching production in reporting and data services across teams. What standards for schema design, SQL review and testing would you introduce?

I would set standards at three points. In schema design, columns are NOT NULL by default, and every nullable column has a documented meaning for NULL. In SQL review and linting, NOT IN with a subquery is banned in favour of NOT EXISTS, and aggregates over nullable columns are checked. In testing and Java mapping, fixtures must include NULLs and empty sets for every optional column, and nullable columns map to wrapper types or Optional, never to primitive defaults. Then we tag defects by root cause, so we can show whether NULL bugs actually go down.

Why this question is asked

When the same class of bug keeps reaching production from several teams, it is no longer a coding mistake; it is a gap in how the organisation builds software. NULL bugs are a good example: each one is small and easy to fix, the fixes are local, and so the next team makes the same mistake. Interviewers ask this leadership question to see whether we can turn recurring defects into lightweight standards that people actually follow, and whether we measure the effect instead of announcing rules. In this answer, we will look at where NULL bugs come from, then set standards for schemas, SQL review, testing and Java mapping, and finish with how to roll them out and prove they work.

1Understanding Where the Bugs Come From

Before writing rules, I would read the last six months of incident and bug reports in reporting and data services and classify the NULL-related ones. In a typical organisation they cluster into a few patterns. NOT IN with a subquery that returns no rows once a NULL appears. Aggregates that ignore NULLs, so an average or a count silently excludes rows. Filters on a left-joined table in WHERE, turning outer joins into inner joins. Unique constraints that allow duplicates because one key column is NULL. And on the Java side, ResultSet.getInt or primitive entity fields turning NULL into 0, so 'unknown' becomes a real-looking number.

Say we find 14 such defects in six months across five teams, with two that reached customers through wrong invoices. That baseline matters: it tells us which rules will pay off, and it gives us something to measure against later. It also shows that the root cause is usually not ignorance of NULL, but data that was cleaner in tests than in production.

A sample root-cause count (six months)
PatternDefectsStandard that targets it
NOT IN with a subquery4Lint rule: use NOT EXISTS
Aggregate ignored NULL rows3Review check, COALESCE with a stated meaning
Outer join filtered in WHERE2Review check, test with missing matches
Primitive mapping turned NULL into 03Wrapper types / Optional rule
Duplicate rows past a unique key2NOT NULL default, NULLS NOT DISTINCT

2Standard 1: Schema Design

The cheapest NULL bug is the one the schema makes impossible. So the first rule is that new columns are NOT NULL unless there is a reason, and the reason is written in a column comment: what NULL means for this column, for example 'not yet shipped' or 'customer declined to give it'. If nobody can say what NULL means, the column should not allow it. Defaults help too: a boolean flag with DEFAULT false NOT NULL removes a whole three-state problem.

For existing tables, we do not rewrite everything. We pick the columns involved in past defects and in key reports, clean their data, and add NOT NULL where the business agrees. On PostgreSQL that can be done safely on large tables by first adding a CHECK (col IS NOT NULL) NOT VALID constraint, validating it, and then setting NOT NULL, which can use the validated check instead of a full locked scan; check your version for that behaviour. For composite unique keys with optional parts, we use UNIQUE NULLS NOT DISTINCT on PostgreSQL 15 and later or a partial unique index, chosen deliberately.

3Standard 2: SQL Review and Linting

Rules that a machine can check should be checked by a machine. We add a SQL linter to CI for every repository that contains SQL, including SQL in Java strings and migration files, and enable a small set of rules: no NOT IN with a subquery, no implicit comma joins, no = NULL. Linters such as SQLFluff can be configured for some of these, and a custom rule or a simple pattern check covers the rest. A linter that blocks merges on three rules is followed; one with fifty warnings is ignored.

For what a linter cannot see, we add a short checklist to SQL reviews in reporting and data code: for each nullable column used in a filter, join or aggregate, what happens when it is NULL; for each left join, are filters on the right-hand table in ON rather than WHERE; for each SUM or AVG, should NULL count as zero and is that written down. The checklist is five questions, not a policy document, so reviewers actually use it.

4Standard 3: Test Data and Java Mapping

Most of these bugs pass tests because the test data has no NULLs. So the testing rule is that every fixture for a table with optional columns includes at least one row with each optional column NULL, and every query test includes the empty-set case, such as a customer with no orders or a day with no sales. Running these tests against a real database with Testcontainers matters here, because an in-memory database may treat NULL in constraints or sorting differently from production.

For Java, the rule is that nullable columns map to wrapper types, Integer rather than int, or to Optional at the API boundary, never to primitives. In JDBC code we use getObject(column, Integer.class) or check wasNull. A static analysis check can flag primitive fields on entities whose columns are nullable, and an architecture test can enforce it in shared libraries. Here is the kind of example we put in the guideline, so the rule has a reason attached.

Java
// Before: an unknown discount becomes a real-looking 0
int discount = rs.getInt("discount"); // 0 for NULL
// After: unknown stays unknown
Integer discount = rs.getObject("discount", Integer.class); // null for NULL
// Entity: nullable column, wrapper type
@Column(name = "discount") private Integer discount;
public Optional<Integer> discount() { return Optional.ofNullable(discount); }

5Rolling It Out and Proving It Works

Standards across teams only stick if the teams help write them. I would draft the three standards with one or two engineers from the teams that had the most defects, show each rule next to the real incident it would have prevented, and agree on them in the engineering forum rather than announcing them. Then we turn on the linter in warning mode for a month, fix the existing violations, and switch it to blocking. Existing code is not rewritten wholesale; it is fixed when touched.

Finally, we measure. Every bug ticket for these services gets a root-cause tag, and NULL handling is one of the values. We review the count each quarter against the baseline of 14 in six months. If the number falls to two or three, the standards are working and we can say so with evidence. If one pattern persists, the standard for it is wrong or not enforced, and we change it. Measuring also keeps the standards honest: a rule that prevents nothing after a year can be dropped.

Step throughTurning recurring defects into standards
  1. Classify past defects by root causebaseline: 14 in six months
  2. Draft rules with the affected teamseach rule tied to a real incident
  3. Automate what can be checkedlinter in warning mode, then blocking
  4. Review and test the restchecklist, NULL fixtures, wrapper types
  5. Tag new defects and review quarterlykeep, change or drop each rule

5 steps in order. Play them, or step through them one at a time.

6Follow-up questions

How do you handle teams that push back on NOT NULL by default?

Usually the pushback is about migrations and legacy data, not the principle. So the standard applies to new columns, and existing ones are tightened only when we touch them or when they caused a defect. We also show the cost of the alternative: each nullable column needs documented meaning, NULL test cases and careful aggregates. Seen that way, NOT NULL is the cheaper option, and teams generally agree.

Should Optional be used for entity fields?

Not as the field type. JPA expects plain fields, and Optional is not serialisable or meant for fields. The common pattern is a wrapper-type field, such as Integer, and an accessor that returns Optional at the domain or API boundary. That keeps persistence simple and makes callers deal with absence explicitly.

What if the bugs come from data the teams do not control, such as an upstream feed?

Then the standard moves to the boundary: the ingestion code validates the feed, decides explicitly what each missing field means, and either rejects, defaults or stores NULL with a known meaning. Data contracts with the upstream team, listing which fields may be missing, help a lot. The downstream services then rely on the contract rather than guessing.

7Conclusion

In this answer, we started from the real defects, classified them by root cause, and set standards where each pattern begins: NOT NULL by default with documented meaning in schemas, a linter that bans NOT IN with subqueries plus a short review checklist, NULLs and empty sets in every test fixture, and wrapper types or Optional for nullable columns in Java. We rolled them out with the affected teams and tracked defects by root cause to show whether they worked.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start with data, not rules. I would go through six months of bugs in the reporting and data services and tag the NULL-related ones by pattern: NOT IN with subqueries, aggregates silently skipping NULLs, outer joins filtered in WHERE, duplicates past unique keys, and Java mapping NULL to zero through getInt or primitive fields.

Then, if they want more

That gives a baseline, say 14 defects across five teams, and tells me which standards matter. Then three standards. Schema: columns are NOT NULL by default, and any nullable column has a comment saying what NULL means; existing columns get tightened when they caused a bug or when we touch them.

SQL: a linter in CI that bans NOT IN with a subquery, comma joins and equals NULL, plus a five-question review checklist for nullable columns in filters, joins and aggregates.

Testing and Java: every fixture includes NULLs for optional columns and an empty-set case, tests run on the real database with Testcontainers, and nullable columns map to wrapper types or Optional at the boundary, never primitives.

I would write these with engineers from the teams that had the most incidents, tie each rule to a real incident, start the linter in warning mode and then make it blocking.

Finally, every new defect gets a root-cause tag, and we compare the count each quarter with the baseline, so we can show the standards work, or change the ones that do not.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 15
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "NULL-related bugs keep reaching production in reporting and data services across teams....": https://scrivio-interview-library.netlify.app/databases/sql-joins-and-subqueries/f6857a2442/