Extract Year, Month, and Day from Dates in MySQL
📖 Scenario: You are managing a small library database. You want to organize and analyze the dates when books were borrowed. To do this, you need to extract the year, month, and day parts from the borrow dates stored in your database.
🎯 Goal: Build a MySQL query that extracts the year, month, and day from a date column called borrow_date in a table named borrow_records. This will help you see the exact year, month, and day when each book was borrowed.
📋 What You'll Learn
Create a table called
borrow_records with columns id (integer) and borrow_date (date).Insert three specific rows with exact dates: '2023-04-15', '2022-12-01', and '2024-01-20'.
Write a SELECT query that extracts the year, month, and day from
borrow_date using the EXTRACT function.Alias the extracted parts as
year_borrowed, month_borrowed, and day_borrowed.💡 Why This Matters
🌍 Real World
Extracting parts of dates is common in reporting and analytics to group or filter data by year, month, or day.
💼 Career
Database administrators and analysts often use date extraction to prepare data for business insights and decision-making.
Progress0 / 4 steps