SQL CHECK Get Certified
Learn how the CHECK constraint forces every inserted or updated value to satisfy a Boolean condition, protecting the quality of your data.
What is the CHECK Constraint?
A CHECK constraint is a rule attached to a column or to the whole table. Whenever a row is inserted or updated, SQLite evaluates the condition. If the condition is false the statement is rejected and the data remains unchanged.
Basic CHECK – Positive Price
What this code does: Ensures that every product price is greater than zero.
DROP TABLE IF EXISTS products;
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL,
price REAL CHECK (price > 0)
);
INSERT INTO products VALUES
(1, 'Desk Lamp', 850.00),
(2, 'USB Fan', 450.00),
(3, 'Keyboard', 1200.00);
SELECT *
FROM products
ORDER BY product_id;
CHECK for a Minimum Age
What this code does: Accepts only members who are 18 or older.
DROP TABLE IF EXISTS members;
CREATE TABLE members (
member_id INTEGER PRIMARY KEY,
member_name TEXT NOT NULL,
age INTEGER CHECK (age >= 18)
);
INSERT INTO members VALUES
(101, 'Aarav', 24),
(102, 'Meera', 31),
(103, 'Kabir', 42);
SELECT *
FROM members
ORDER BY member_id;
CHECK with BETWEEN
What this code does: Restricts exam scores to the inclusive range 0–100.
DROP TABLE IF EXISTS exam_results;
CREATE TABLE exam_results (
result_id INTEGER PRIMARY KEY,
student_name TEXT NOT NULL,
score INTEGER CHECK (score BETWEEN 0 AND 100)
);
INSERT INTO exam_results VALUES
(1, 'Isha', 88),
(2, 'Rohan', 76),
(3, 'Nisha', 94);
SELECT *
FROM exam_results
ORDER BY result_id;
CHECK with a List of Allowed Values
What this code does: Limits ticket status to the three legal values Open, Pending or Closed.
DROP TABLE IF EXISTS tickets;
CREATE TABLE tickets (
ticket_id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
status TEXT CHECK (status IN ('Open', 'Pending', 'Closed'))
);
INSERT INTO tickets VALUES
(1, 'Login problem', 'Open'),
(2, 'Payment question', 'Pending'),
(3, 'Password reset', 'Closed');
SELECT *
FROM tickets
ORDER BY ticket_id;
Multiple Conditions with AND
What this code does: Requires both age ≥ 18 and salary ≥ 15 000.
DROP TABLE IF EXISTS employees;
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
employee_name TEXT NOT NULL,
age INTEGER,
salary REAL,
CHECK (age >= 18 AND salary >= 15000)
);
INSERT INTO employees VALUES
(1, 'Dev', 25, 42000),
(2, 'Anaya', 29, 51000),
(3, 'Vikram', 34, 68000);
SELECT *
FROM employees
ORDER BY employee_id;
Comparing Two Columns
A table-level CHECK can refer to more than one column in the same row.
What this code does: Guarantees that an event’s end hour is later than its start hour.
DROP TABLE IF EXISTS events;
CREATE TABLE events (
event_id INTEGER PRIMARY KEY,
event_name TEXT NOT NULL,
start_hour INTEGER NOT NULL,
end_hour INTEGER NOT NULL,
CHECK (end_hour > start_hour)
);
INSERT INTO events VALUES
(1, 'Morning Workshop', 9, 12),
(2, 'Design Session', 13, 16),
(3, 'Evening Seminar', 17, 20);
SELECT *
FROM events
ORDER BY event_id;
Separate CHECKs on Quantity and Price
What this code does: Ensures both quantity and unit price are positive, then calculates a total.
DROP TABLE IF EXISTS order_items;
CREATE TABLE order_items (
item_id INTEGER PRIMARY KEY,
product_name TEXT NOT NULL,
quantity INTEGER NOT NULL,
unit_price REAL NOT NULL,
CHECK (quantity > 0),
CHECK (unit_price > 0)
);
INSERT INTO order_items VALUES
(1, 'Notebook', 3, 120.00),
(2, 'Desk Organizer', 2, 350.00),
(3, 'Pen Set', 5, 90.00);
SELECT
item_id,
product_name,
quantity,
unit_price,
quantity * unit_price AS total
FROM order_items
ORDER BY item_id;
CHECK Applies to Both INSERT and UPDATE
What this code does: Creates an inventory table, inserts rows, then successfully updates a stock value that still satisfies the CHECK.
DROP TABLE IF EXISTS inventory;
CREATE TABLE inventory (
item_id INTEGER PRIMARY KEY,
item_name TEXT NOT NULL,
stock INTEGER CHECK (stock >= 0)
);
INSERT INTO inventory VALUES
(1, 'Camera Tripod', 12),
(2, 'LED Panel', 8),
(3, 'Microphone Stand', 15);
UPDATE inventory
SET stock = 20
WHERE item_id = 2;
SELECT *
FROM inventory
ORDER BY item_id;
Named CHECK Constraint
Giving a constraint a name makes error messages clearer and the rule easier to identify later.
What this code does: Creates a named constraint that limits course duration to a sensible range.
DROP TABLE IF EXISTS courses;
CREATE TABLE courses (
course_id INTEGER PRIMARY KEY,
course_name TEXT NOT NULL,
duration_hours INTEGER,
CONSTRAINT valid_duration
CHECK (duration_hours > 0 AND duration_hours <= 500)
);
INSERT INTO courses VALUES
(1, 'Database Fundamentals', 40),
(2, 'Mechanical Design', 80),
(3, 'Industrial Automation', 120);
SELECT *
FROM courses
ORDER BY course_id;
CHECK with Date Strings
ISO-format dates (YYYY-MM-DD) can be compared lexicographically, so CHECK works with them.
What this code does: Ensures every booking’s end date is on or after its start date.
DROP TABLE IF EXISTS bookings;
CREATE TABLE bookings (
booking_id INTEGER PRIMARY KEY,
customer_name TEXT NOT NULL,
start_date TEXT NOT NULL,
end_date TEXT NOT NULL,
CHECK (end_date >= start_date)
);
INSERT INTO bookings VALUES
(1, 'Aman', '2026-08-10', '2026-08-12'),
(2, 'Priya', '2026-09-03', '2026-09-06'),
(3, 'Rahul', '2026-10-15', '2026-10-20');
SELECT *
FROM bookings
ORDER BY booking_id;
Common CHECK Patterns
Advantages of CHECK
- Prevents invalid values from ever being stored.
- Improves data quality and consistency.
- Can validate numeric ranges and enumerated sets.
- Can compare values across multiple columns.
- Is evaluated on both INSERT and UPDATE.
Best Practices
- Use CHECK for rules that must always be true for stored data.
- Keep conditions simple and easy to understand.
- Give important constraints meaningful names.
- Combine CHECK with NOT NULL when a value is mandatory.
- Test both INSERT and UPDATE statements against the constraint.
The CHECK constraint is a simple yet powerful way to keep bad data out of your tables. Applied thoughtfully it makes the database more reliable and easier to maintain. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.
🧪 Test Your SQL Code
Edit the SQL code on the left and click "Run Code" to see the result on the right.
Click "Run Code" to see the result here.