0
0
SQLquery~20 mins

NOT NULL constraint in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.