SQL interview question: Unique Index
One Account per Email
Support keeps merging duplicate student accounts. The fix: make the database itself refuse duplicate emails.
Create a UNIQUE index named exactly idx_students_email on the email column of students.
Note: Uniqueness is verified from the catalog. Rolled back afterwards.
This challenge changes data. Your statement runs on a private copy of the database, then we check the resulting table.
Tables
students 25 rows
- student_id integer PK
- name text
- email text
- grade_level integer
- gpa real
- enrollment_date text
| student_id | name | grade_level | gpa | enrollment_date | |
|---|---|---|---|---|---|
| 1 | Alice Johnson | [email protected] | 12 | 3.9 | 2024-09-01 |
| 2 | Bob Smith | [email protected] | 12 | 3.7 | 2024-09-01 |
| 3 | Carol Williams | [email protected] | 12 | 3.8 | 2024-09-01 |