Day 71 of My Learning Journey !

Published: (December 12, 2025 at 12:44 AM EST)
2 min read
Source: Dev.to

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) % 7 calculates how many days until the next Wednesday (day 3).
  • Adding this offset to CURRENT_DATE gives 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

Back to Blog

Related posts

Read more »

PostgreSQL Log Viewing

!Forem Logohttps://media2.dev.to/dynamic/image/width=65,height=,fit=scale-down,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%...

PostgreSQL Merge Into

sql UPDATE PSMT_INVOICE_M SET SHIPPING_COUNTRY_ID = SRC.COUNTRY_ID, SHIPPING_CITY_ID = SRC.CITY_ID, SHIPPING_TOWN_ID = SRC.TOWN_ID FROM SELECT PM.INVOICE_M_ID,...

step2

!Query Filterhttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2...