Add NOT NULL and DEFAULT to a column

Easy
6 views 23 Jan 2026
You have a table Employees(emp_id, name, dept_id, salary, hire_date). Make dept_id mandatory and set default salary as 30000 for new rows....

Create a UNIQUE constraint for email

Easy
6 views 23 Jan 2026
Create table Users(user_id, email, phone). Ensure email is unique and not null....

Add a CHECK constraint for rating

Easy
9 views 23 Jan 2026
In table Reviews(review_id, rating, comment), rating must be between 1 and 5....

Composite primary key for enrollment

Easy
6 views 23 Jan 2026
Create Enrollment(student_id, course_id, enrolled_on). One student can enroll once per course. Make (student_id, course_id) the primary key....

Foreign key with basic reference

Easy
9 views 23 Jan 2026
Create Departments(dept_id, dept_name) and Employees(emp_id, name, dept_id). Add FK so employee dept_id must exist in Departments....

Prevent deleting parent rows when children exist

Medium
8 views 23 Jan 2026
In Orders(order_id) and Payments(payment_id, order_id), enforce that an order cannot be deleted if it has payments....

Auto-delete child rows on parent delete

Medium
7 views 23 Jan 2026
In Customers(customer_id) and Orders(order_id, customer_id), make it so when a customer is deleted, all their orders are deleted too....

Add a conditional CHECK using two columns

Medium
4 views 23 Jan 2026
In Coupons(code, discount_percent, max_discount), discount_percent must be 0 to 80, and max_discount must be at least 100 if discount_percent > 50....

Enforce case-insensitive uniqueness (pattern approach)

Medium
6 views 23 Jan 2026
In Users(email), you want emails to be unique even if case differs (A@x.com vs a@x.com). Write one practical approach using a stored generated column lower_email and a UNIQUE constraint on it....

Deferred constraint idea using transaction (concept + SQL)

Medium
5 views 23 Jan 2026
You are inserting parent and child rows in one transaction. Write a safe sequence so the FK does not fail when you insert child first....

Find rows violating a constraint rule

Hard
5 views 23 Jan 2026
In Products(product_id, price), price should be > 0 but table has old bad data. Write a query to find all invalid rows before adding a CHECK constraint....

Simulate a conditional unique rule

Hard
6 views 23 Jan 2026
In Seats(bus_id, seat_no, status), seat_no should be unique per bus only for active seats (status='ACTIVE'). Write a SQL approach that enforces it using a separate table ActiveSeats and FK/PK....

Protect against orphan data during bulk load

Hard
7 views 23 Jan 2026
You are loading Orders first, then Customers later. How will you prevent orphan customer_id in Orders? Write a staging-table approach....

Create a strong candidate key with multiple columns

Hard
5 views 23 Jan 2026
In table StudentMarks(student_id, exam_date, subject, marks), a student can have only one record per subject per exam_date. Add the right constraint....

Exactly one payment reference (XOR check)

Hard
8 views 23 Jan 2026
In Payments(payment_id, order_id, card_txn_id, upi_txn_id), a payment can be done either by card or by UPI. Enforce rule: exactly one of card_txn_id and upi_txn_id must be filled (not both, not none)....