Tables, Keys & Constraints · Question 16 · Hard

The second INSERT passes NULL as the price. What does the final SELECT return?

CREATE TABLE Product (
  id INT PRIMARY KEY,
  name VARCHAR(20),
  price INT CHECK (price > 0)
);
INSERT INTO Product VALUES (1, 'Pen', 10);
INSERT INTO Product VALUES (2, 'Sample', NULL);
SELECT COUNT(*) FROM Product WHERE price IS NULL;
  1. 0
  2. 2
  3. 1
  4. Nothing: the second INSERT is rejected by the CHECK constraint
Answer and explanation

Answer: C. 1

Explanation: A CHECK constraint rejects a row only when its condition is FALSE. For a NULL price, price > 0 is UNKNOWN, which is not FALSE, so the row is accepted. To forbid it, the column also needs NOT NULL.

Previous: A table Orders with 50 lakh rows is searched very often by customer_id, so an… · Next: Emp.dept_id is declared as FOREIGN KEY REFERENCES Dept(id) with no ON DELETE…

Practise tables, keys & constraints

Try a fresh set of tables, keys & constraints questions with an explanation after every answer, free and without signing up: Tables, Keys & Constraints practice. Or sit a full placement mock test.

More tables, keys & constraints questions

More sql practice

DML & Transactions · SELECT, WHERE & ORDER BY · SQL Joins · Aggregates & GROUP BY · Subqueries & CTEs · Window Functions · NULL Handling · SQL Functions & CASE

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge