You scanned the SQL twice. The logic was fine. The bug was smaller: a missing new column in the schema. Without it, the feature could never work as intended.
Adding a new column is simple, but in production it must be exact. In SQL, the ALTER TABLE command is the standard. You declare the table, the column name, type, and constraints. Example:
ALTER TABLE users
ADD COLUMN last_login TIMESTAMP DEFAULT NOW();
This creates a new column without touching existing data. Choosing the right data type is essential for storage efficiency and query performance. Explicit defaults prevent null values from breaking client logic.
When adding a new column to large datasets, watch for locks and migration downtime. Many relational databases, like PostgreSQL and MySQL, have online DDL features or tools to apply schema changes without blocking reads and writes. Test these changes in staging before production.