Question: What does NOT NULL mean in SQL?
Answer: NOT NULL is a column constraint that says a column must always have a real value; the database will reject NULL for that column. It does not mean empty string, zero, or false — those are valid values if the column type allows them. Interviewers ask this because it tests whether you understand the difference between “missing/unknown” and an actual value.
Interview-Ready Answer: I’d say NOT NULL is a constraint that prevents a column from storing NULL, so every row must have a value there. It’s important because NULL means “unknown or missing,” which is different from 0 or an empty string. In practice, the database checks this on INSERT and UPDATE, and it will reject the row if the column is null.
NOT NULL really meansNULL is a special SQL marker for “missing,” “unknown,” or “not applicable.” A NOT NULL constraint tells the database: this column is required. Think of it like a form field marked with a red asterisk — you cannot submit the row without filling it in.
INSERT or UPDATE.NULL, the engine rejects the change immediately.Under the hood, most engines store a small “null flag” alongside each column or row. The check itself is simple and fast, usually O(1) per row for the constraint evaluation. The real cost is not the check; it is the write to disk, index maintenance, and transaction logging that already happen for the row.
Use NOT NULL for data that must exist for the row to make sense: user email, order total, created timestamp, product SKU, or foreign keys that are mandatory. It improves data quality because bad data is stopped at the door instead of being discovered later in reports, joins, or application code.
NOT NULL vs common alternatives| Constraint | Meaning | Typical use |
|---|---|---|
NOT NULL | Value required | Mandatory fields |
DEFAULT | Fills in a value if omitted | Auto timestamps, status |
CHECK | Validates a rule | Range or format rules |
PRIMARY KEY | Unique row identifier | Row identity |
UNIQUE | No duplicates | Email, username |
PRIMARY KEY is stricter than NOT NULL because it also requires uniqueness. A column can be NOT NULL without being unique, like a country_code column where many rows can store US.
'' is a real value, so it passes NOT NULL even though it may still be a bad business value.NULL, the default does not rescue you; the constraint still fails.NOT NULL later may require cleanup. If existing rows already contain nulls, the change fails until you backfill the data.NOT NULL is one of the cheapest constraints to enforce. It does not create an index, and it does not speed up lookups by itself. Its value is correctness: it narrows the possible states of your data, which makes joins, filters, and application logic safer.
Memory hook: imagine a seat with a seatbelt clip. NOT NULL says the clip must be fastened before the car can move; an empty seat is not acceptable, but a properly filled seat can be anything valid.
Version note: exact syntax for adding or dropping NOT NULL can vary by database engine. The concept is universal, but the migration path may differ, especially when existing nulls must be cleaned up first.
In an e-commerce checkout service, an orders table usually needs a non-null customer_id, order_total, and created_at. Those fields are essential because every order must belong to a customer and must have a price and time.
Imagine a bug in the API layer where a retry path forgets to include customer_id. If the column is NOT NULL, the database rejects the bad row immediately. Without that constraint, the row gets saved with a null customer, and later the order cannot join to the customer table, the receipt job fails, and support sees “missing customer” tickets.
What goes wrong when people misunderstand it: a developer assumes the app layer validation is enough and removes the database constraint. A release goes out, a null sneaks in, and suddenly analytics show broken revenue numbers because the warehouse ETL cannot group orders by customer. The logs often look like “cannot join on null key” or “order.customer_id is null,” and the user impact is delayed emails, broken dashboards, and manual cleanup work.
-- NOT NULL demo in a SQLite-compatible script.
-- The goal is to show required columns, default values, and a failure path.
DROP TABLE IF EXISTS users;
CREATE TABLE users (
user_id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
display_name TEXT,
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- This row is valid: required columns are present.
INSERT INTO users (user_id, email, display_name)
VALUES (1, 'ada@example.com', 'Ada');
-- This row is also valid: display_name is optional, and created_at uses its default.
INSERT INTO users (user_id, email)
VALUES (2, 'grace@example.com');
-- Edge case: NULL is rejected only for the NOT NULL column.
-- Uncommenting the next statement should fail because email cannot be NULL.
-- INSERT INTO users (user_id, email, display_name)
-- VALUES (3, NULL, 'Broken Row');
-- Another subtle point: empty string is NOT the same as NULL.
-- This insert succeeds because '' is a real value, even if the app may still dislike it.
INSERT INTO users (user_id, email, display_name)
VALUES (3, '', 'Empty Email Text');
SELECT
user_id,
email,
display_name,
created_at
FROM users
ORDER BY user_id;
-- If you want to verify the schema idea, this query shows the rows you kept.
-- The failed NULL insert never appears because the database would reject it before writing.
Follow-up & Tricky Questions:
NOT NULL and UNIQUE? Yes. That means the value is required and cannot repeat, which is common for email addresses or usernames.DEFAULT and NOT NULL? DEFAULT supplies a value when one is omitted; NOT NULL blocks missing values entirely. They are often used together so the column is always filled.NOT NULL to an existing column? Yes, but only after fixing any rows that currently contain NULL. Many databases will scan the table and refuse the change until the data is clean.NOT NULL improve query speed? Not by itself. It improves correctness; any performance benefit is indirect because the optimizer knows the column cannot be null and can simplify some logic.NOT NULL? Not always. If a relationship is optional, the foreign key can be null; if every row must point to a parent, then make it NOT NULL.NULL? No. Empty string is a real value, while NULL means unknown or missing. This is one of the most common interview traps.NOT NULL? Usually no, because primary keys are already not null by definition in standard SQL and in mainstream engines.NOT NULL fail even when a default exists? No, not if the default fills it. But if you explicitly send NULL, the default does not apply and the insert fails.Common Mistakes:
'' is a value; NULL is the absence of a value.DEFAULT to save explicit NULL. Correction: defaults are used when the column is omitted, not when null is passed.NOT NULL.Memory Hook: “Required field at the database door.” If the value is missing, the bouncer sends the row away; if it is present, the row gets in.
Cheat Sheet:
NOT NULL means the column must contain a value.NULL means unknown or missing, not zero or empty string.INSERT and UPDATE.DEFAULT helps when a column is omitted, not when explicit NULL is sent.PRIMARY KEY implies uniqueness and not null.Practice Tasks:
DEFAULT to a NOT NULL column and observe the difference between omitting the column and sending NULL.NOT NULL constraint.