0
0
SQLquery~20 mins

CHECK constraint in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CHECK Constraint Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
CHECK constraint behavior on insert

Consider this table definition:

CREATE TABLE Employees (
ID INT PRIMARY KEY,
Age INT CHECK (Age >= 18)
);

What happens if you run this insert?

INSERT INTO Employees (ID, Age) VALUES (1, 16);
AThe insert fails with a CHECK constraint violation error.
BThe insert succeeds and the row is added.
CThe insert succeeds but Age is set to NULL.
DThe insert succeeds but Age is automatically set to 18.
Attempts:
2 left
💡 Hint

CHECK constraints prevent invalid data from being inserted.

🧠 Conceptual
intermediate
1:30remaining
Purpose of CHECK constraints

What is the main purpose of a CHECK constraint in a database table?

ATo speed up queries by indexing columns.
BTo create a relationship between two tables.
CTo enforce a rule that limits the values allowed in a column.
DTo automatically generate unique IDs for rows.
Attempts:
2 left
💡 Hint

Think about data validation rules inside a table.

📝 Syntax
advanced
2:00remaining
Identify the correct CHECK constraint syntax

Which of the following SQL statements correctly adds a CHECK constraint to ensure salary is positive?

AALTER TABLE Employees ADD CONSTRAINT chk_salary CHECK salary > 0;
BALTER TABLE Employees ADD CONSTRAINT chk_salary salary > 0 CHECK;
CALTER TABLE Employees ADD CHECK salary > 0;
DALTER TABLE Employees ADD CONSTRAINT chk_salary CHECK (salary > 0);
Attempts:
2 left
💡 Hint

CHECK conditions must be enclosed in parentheses.

🔧 Debug
advanced
2:30remaining
Why does this CHECK constraint fail to enforce the rule?

Given this table creation:

CREATE TABLE Products (
ID INT PRIMARY KEY,
Price DECIMAL(10,2),
CHECK Price > 0
);

Why does the database allow inserting a product with Price = -5?

AThe CHECK constraint syntax is incorrect; it needs parentheses around the condition.
BThe database does not support CHECK constraints on DECIMAL columns.
CThe Price column must be declared NOT NULL for the CHECK to work.
DThe primary key constraint overrides the CHECK constraint.
Attempts:
2 left
💡 Hint

Look carefully at the CHECK syntax.

optimization
expert
3:00remaining
Optimizing CHECK constraints for performance

You have a large table with a CHECK constraint on a complex expression involving multiple columns. What is the best way to optimize performance when inserting many rows?

ARemove the CHECK constraint and rely on application code validation only.
BTemporarily disable the CHECK constraint during bulk inserts and re-enable it afterward.
CAdd an index on the columns used in the CHECK constraint.
DRewrite the CHECK constraint as a trigger to improve speed.
Attempts:
2 left
💡 Hint

Think about how constraints affect bulk operations.