🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

SQL – UNIQUE

Enforce that all values in a column (or combination of columns) are distinct using the UNIQUE constraint.

The UNIQUE constraint ensures no two rows in a table share the same value in the specified column. This is the perfect tool for enforcing natural identifiers — usernames, email addresses, SKUs, license plates — where every entry must be one-of-a-kind.

Unlike a PRIMARY KEY, a UNIQUE column can contain NULL values (and typically multiple NULLs are allowed, since NULL ≠ NULL in SQL). You can also create composite UNIQUE constraints spanning multiple columns — e.g., ensuring no student can enroll twice in the same course.

UNIQUE variations

-- Single column unique
CREATE TABLE users (email TEXT UNIQUE);

-- Composite unique (two columns together must be unique)
CREATE TABLE enrollments (
  student_id INTEGER,
  course_id  INTEGER,
  UNIQUE (student_id, course_id)
);

Try It Yourself — Interactive SQL Editor

Edit the query below and click Run Query ▶ to see live results powered by SQLite running directly in your browser.

SQLite – edit & run
Results
← Click Run Query ▶ to see results

Key Points

  • UNIQUE prevents duplicate values in the constrained column
  • Multiple UNIQUE constraints are allowed on the same table
  • NULL values are typically exempt from uniqueness checks
  • Composite UNIQUE spans multiple columns: the combination must be unique

Pro Tip from CodesCompiler: The best way to learn SQL is to break things intentionally — modify the query above, change the WHERE conditions, try different columns. Every error teaches you something the docs cannot.

In the next lesson, we continue exploring SQL’s powerful feature set to build your database mastery.