Bird
Raised Fist0
SQLquery~20 mins

NOT NULL constraint in SQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
NOT NULL Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Effect of NOT NULL constraint on INSERT
Given the table Users(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT), what happens when you run this query?

INSERT INTO Users (id, age) VALUES (1, 25);
SQL
CREATE TABLE Users(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT);
INSERT INTO Users (id, age) VALUES (1, 25);
AThe query fails with a NOT NULL constraint violation error.
BThe row is inserted with name set to NULL.
CThe row is inserted with name set to an empty string.
DThe query succeeds but issues a warning about missing name.
Attempts:
2 left
💡 Hint
Think about what NOT NULL means for a column when no value is provided.
🧠 Conceptual
intermediate
1:30remaining
Purpose of NOT NULL constraint
Why do database designers use the NOT NULL constraint on columns?
ATo ensure that the column always contains a value and never NULL.
BTo allow the column to store multiple values in one field.
CTo automatically generate a unique value for the column.
DTo speed up queries by indexing the column.
Attempts:
2 left
💡 Hint
Think about data integrity and missing information.
📝 Syntax
advanced
2:00remaining
Adding NOT NULL constraint to existing column
Which SQL statement correctly adds a NOT NULL constraint to the existing column email in the Customers table?
AALTER TABLE Customers SET email NOT NULL;
BALTER TABLE Customers ADD NOT NULL (email);
CALTER TABLE Customers MODIFY email VARCHAR(100) NOT NULL;
DALTER TABLE Customers CHANGE email email VARCHAR(100) NOT NULL;
Attempts:
2 left
💡 Hint
Different SQL dialects use different syntax; consider standard SQL or MySQL style.
query_result
advanced
2:00remaining
Query result with NOT NULL and DEFAULT
Consider this table:
CREATE TABLE Products(id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, price DECIMAL(5,2) NOT NULL DEFAULT 9.99);
What is the result of this query?

INSERT INTO Products (id, name) VALUES (1, 'Pen');
SELECT * FROM Products WHERE id = 1;
ARow inserted with id=1, name='Pen', price=NULL.
BQuery fails because price is NOT NULL but not provided.
CRow inserted with id=1, name='Pen', price=0.00.
DRow inserted with id=1, name='Pen', price=9.99.
Attempts:
2 left
💡 Hint
Think about how DEFAULT values work with NOT NULL columns.
🔧 Debug
expert
2:30remaining
Diagnosing NOT NULL constraint violation in multi-step insert
You have this table:
CREATE TABLE Orders(order_id INT PRIMARY KEY, customer_id INT NOT NULL, order_date DATE NOT NULL);
You run these queries:

INSERT INTO Orders (order_id, customer_id) VALUES (101, 5);

What error will you get and why?
AError: Duplicate primary key because order_id 101 already exists.
BError: NOT NULL constraint failed on 'order_date' because it was not provided.
CNo error, row inserted with order_date set to current date automatically.
DError: Foreign key constraint failed on customer_id.
Attempts:
2 left
💡 Hint
Check which NOT NULL columns are missing values in the INSERT.

Practice

(1/5)
1.

What does the NOT NULL constraint do in a database table?

easy
A. It automatically generates unique values for a column.
B. It allows a column to have duplicate values.
C. It ensures a column cannot have empty or missing values.
D. It deletes rows with null values.

Solution

  1. Step 1: Understand the purpose of NOT NULL

    The NOT NULL constraint prevents a column from having NULL (empty) values, ensuring data is always present.
  2. Step 2: Compare options with the definition

    Only It ensures a column cannot have empty or missing values. correctly describes this behavior; others describe different constraints or actions.
  3. Final Answer:

    It ensures a column cannot have empty or missing values. -> Option C
  4. Quick Check:

    NOT NULL means no empty values allowed [OK]
Hint: NOT NULL means no empty values allowed in a column [OK]
Common Mistakes:
  • Confusing NOT NULL with UNIQUE constraint
  • Thinking NOT NULL deletes rows
  • Assuming NOT NULL auto-generates values
2.

Which of the following is the correct syntax to add a NOT NULL constraint to a column named email when creating a table?

CREATE TABLE users (
id INT PRIMARY KEY,
email ???
);
easy
A. NULL VARCHAR(255)
B. NOT NULL VARCHAR(255)
C. VARCHAR(255) NULL
D. VARCHAR(255) NOT NULL

Solution

  1. Step 1: Recall correct order of column definition

    The data type comes first, then constraints like NOT NULL.
  2. Step 2: Check each option's order

    VARCHAR(255) NOT NULL correctly places VARCHAR(255) before NOT NULL; others have wrong order or use NULL instead.
  3. Final Answer:

    VARCHAR(255) NOT NULL -> Option D
  4. Quick Check:

    Data type first, then NOT NULL [OK]
Hint: Data type comes before NOT NULL in column definition [OK]
Common Mistakes:
  • Writing NOT NULL before data type
  • Using NULL instead of NOT NULL
  • Omitting data type
3.

Given the table employees with columns id INT PRIMARY KEY and name VARCHAR(100) NOT NULL, what happens when you run this query?

INSERT INTO employees (id, name) VALUES (1, NULL);
medium
A. The query fails with an error due to NOT NULL constraint.
B. The name column defaults to an empty string.
C. The row is inserted with a NULL name.
D. The row is inserted but name is ignored.

Solution

  1. Step 1: Understand NOT NULL behavior on insert

    NOT NULL means the column cannot accept NULL values during insert or update.
  2. Step 2: Analyze the insert statement

    The query tries to insert NULL into the name column, violating the NOT NULL constraint, causing an error.
  3. Final Answer:

    The query fails with an error due to NOT NULL constraint. -> Option A
  4. Quick Check:

    NOT NULL rejects NULL inserts [OK]
Hint: Inserting NULL into NOT NULL column causes error [OK]
Common Mistakes:
  • Assuming NULL is converted to empty string
  • Thinking the row inserts ignoring NULL
  • Confusing NOT NULL with default values
4.

Consider this table creation statement:

CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50) NOT NULL,
price DECIMAL(5,2)
);

Which of the following statements will cause an error?

medium
A. INSERT INTO products (product_id, product_name, price) VALUES (1, 'Pen', 1.50);
B. INSERT INTO products (product_id, price) VALUES (2, 2.00);
C. INSERT INTO products (product_id, product_name) VALUES (3, 'Notebook');
D. INSERT INTO products (product_id, product_name, price) VALUES (4, 'Eraser', NULL);

Solution

  1. Step 1: Identify columns with NOT NULL constraint

    Only product_name has NOT NULL, so it must have a value on insert.
  2. Step 2: Check each insert statement

    INSERT INTO products (product_id, price) VALUES (2, 2.00); omits product_name, so it tries to insert NULL, causing an error. Others provide product_name.
  3. Final Answer:

    INSERT missing NOT NULL column product_name causes error. -> Option B
  4. Quick Check:

    NOT NULL columns must be included in insert [OK]
Hint: Always provide values for NOT NULL columns on insert [OK]
Common Mistakes:
  • Ignoring NOT NULL columns in insert
  • Assuming NULL allowed if column omitted
  • Confusing NULL and empty string
5.

You have a table orders with columns order_id INT PRIMARY KEY, customer_id INT NOT NULL, and order_date DATE NOT NULL. You want to add a new column status that must always have a value and defaults to 'pending'. Which is the correct way to add this column?

hard
A. ALTER TABLE orders ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'pending';
B. ALTER TABLE orders ADD COLUMN status VARCHAR(20) DEFAULT 'pending';
C. ALTER TABLE orders ADD COLUMN status VARCHAR(20) NOT NULL;
D. ALTER TABLE orders ADD COLUMN status VARCHAR(20);

Solution

  1. Step 1: Understand NOT NULL with default value

    Adding a NOT NULL column requires a default value to avoid errors on existing rows.
  2. Step 2: Analyze each ALTER statement

    ALTER TABLE orders ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'pending'; adds status with NOT NULL and default 'pending', so existing rows get this value. Others miss NOT NULL or default.
  3. Final Answer:

    ALTER with NOT NULL and DEFAULT 'pending' is correct. -> Option A
  4. Quick Check:

    NOT NULL columns need default when added to existing table [OK]
Hint: Add NOT NULL column with DEFAULT to avoid errors [OK]
Common Mistakes:
  • Adding NOT NULL column without default
  • Assuming default alone enforces NOT NULL
  • Omitting NOT NULL when required