SQL Practice Logo

SQLPractice Online

Date & Time Functions: Mistakes

Module: SQL Fundamentals

CREATE TABLE events (event_time TIMESTAMP);

CREATE TABLE events (event_time TIMESTAMPTZ);

TIMESTAMP without timezone causes bugs in multi-region apps. User in NYC creates event at 2PM EST, user in India sees 2PM IST (12 hours wrong!). Use TIMESTAMPTZ to store UTC internally, convert to session timezone on display.

Always use timezone-aware types for global apps: TIMESTAMPTZ (PostgreSQL), DATETIMEOFFSET (SQL Server)

High

No error, but causes timezone bugs: events appear in wrong order, "future" timestamps

SELECT * FROM orders WHERE YEAR(order_date) = 2024;

SELECT * FROM orders WHERE order_date >= '2024-01-01' AND order_date < '2025-01-01';

YEAR() function prevents index usage. Must calculate YEAR() for EVERY row. Use date range instead for 100-1000x better performance with index.

Never use functions on indexed columns in WHERE. Use date ranges instead

Critical

No error, but 1000x slower