NOT — SQL Technical Interview Questions & Answers

NOT

NOT

  • NOT flips a test. WHERE NOT (marks < 40) is marks >= 40, but NULL still fails the test (unknown). WHERE city NOT IN ('Pune') vs WHERE NOT city = 'Pune' — NULL city is not ‘Pune’ and not ‘not Pune’ in three-valued logic.
  • NOT LIKE, NOT EXISTS, != / <> are cousins. Prefer readable: WHERE marks >= 40 instead of NOT (marks < 40) when you can.
  • Trap: NOT IN with NULL. Trap: assuming NOT makes NULL into true.
  • On the example next to this theory: NOT: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Technical Interview Tip: WHERE marks >= 40 rather than NOT (marks < 40).

Code Examples for NOT

NOT — sample query

-- NOT
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

NOT: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

How to Explain NOT to an Interviewer

What does NOT do?

Flips true/false NULL stays unknown