DEV Community

Ramya .C
Ramya .C

Posted on

Day 71 of My Learning Journey !

🚀 — Working With Upcoming Wednesday Logic

#RamyaAnalyticsJourney

Today’s challenge was a fun mix of logic and SQL date functions.
The task: 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.


🛠️ Step 1: Create the Table

CREATE TABLE appointments (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    appointment_date DATE
);
Enter fullscreen mode Exit fullscreen mode

🗂️ Step 2: Insert Sample Appointment Data

INSERT INTO appointments (name, appointment_date) VALUES
('John', '2025-01-13'),
('Sarah', '2025-01-15'),
('Michael', '2025-01-10'),
('Anna', '2025-01-14');
Enter fullscreen mode Exit fullscreen mode

🔍 Step 3: Query Appointments Before the Upcoming Wednesday

SELECT name, appointment_date
FROM appointments
WHERE appointment_date < (
    CURRENT_DATE + ((3 - EXTRACT(DOW FROM CURRENT_DATE) + 7) % 7)
);
Enter fullscreen mode Exit fullscreen mode

đź§  How the Logic 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 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.


🚀 Wrapping Up

Day by day, challenge by challenge, I’m gaining more confidence in SQL.
Looking forward to what tomorrow brings!

**#SQL #DataEngineering #Database #LearningJourney #WomenInTech #CareerGrowth #DataDriven #RamyaAnalyticsJourney

Top comments (0)