Day 71 of My Learning Journey !
Source: Dev.to
Working With Upcoming Wednesday Logic
Today’s challenge was a fun mix of logic and SQL date functions.
Create a table with names and appointment dates, then write a query that always returns all appointments scheduled before the upcoming Wednesday, based on the current date.
Even though it sounds simple, the tricky part was calculating the next Wednesday dynamically.
Table definition
CREATE TABLE appointments (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
appointment_date DATE
);
Sample data
INSERT INTO appointments (name, appointment_date) VALUES
('John', '2025-01-13'),
('Sarah', '2025-01-15'),
('Michael', '2025-01-10'),
('Anna', '2025-01-14');
Query to fetch appointments before the upcoming Wednesday
SELECT name, appointment_date
FROM appointments
WHERE appointment_date < (
CURRENT_DATE + ((3 - EXTRACT(DOW FROM CURRENT_DATE) + 7) % 7)
);
How it works
EXTRACT(DOW FROM CURRENT_DATE)returns the current day of the week (0 = Sunday, 1 = Monday, … 3 = Wednesday, … 6 = Saturday).(3 - dow + 7) % 7calculates how many days until the next Wednesday (day 3).- Adding this offset to
CURRENT_DATEgives the date of the upcoming Wednesday.
The query returns all appointments before that date.
This kind of hands‑on exercise is perfect for strengthening SQL foundations and learning how to handle dynamic date logic—something you’ll constantly use in real‑world data work. Day by day, challenge by challenge, I’m gaining more confidence in SQL.
#SQL #DataEngineering #Database #LearningJourney #WomenInTech #CareerGrowth #DataDriven #RamyaAnalyticsJourney