0
0
SQLquery~30 mins

DATE_FORMAT and EXTRACT in SQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Using DATE_FORMAT and EXTRACT to Analyze Sales Dates
📖 Scenario: You work for a retail company that tracks sales data. Each sale has a date stored in the database. You want to learn how to format these dates and extract parts like the year and month to better understand sales trends.
🎯 Goal: Build SQL queries that use DATE_FORMAT to display dates in a friendly way and EXTRACT to get the year and month from sales dates.
📋 What You'll Learn
Create a table called sales with columns sale_id (integer) and sale_date (date).
Insert specific sales dates into the sales table.
Write a query using DATE_FORMAT to show sales dates as 'Month Day, Year'.
Write a query using EXTRACT to get the year and month from each sale date.
💡 Why This Matters
🌍 Real World
Formatting and extracting parts of dates is common in sales reports, analytics, and scheduling systems.
💼 Career
Database professionals often write queries to transform and analyze date data for business insights.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns sale_id as integer and sale_date as date. Then insert these rows exactly: (1, '2023-01-15'), (2, '2023-03-22'), (3, '2024-06-05').
SQL
Need a hint?

Use CREATE TABLE to make the table and INSERT INTO to add rows.

2
Add a query to format the sale dates
Write a SQL query selecting sale_id and the sale_date formatted as 'Month Day, Year' using DATE_FORMAT. Name the formatted column formatted_date.
SQL
Need a hint?

Use DATE_FORMAT(date, format) with format '%M %e, %Y' to get 'Month Day, Year'.

3
Extract the year and month from sale dates
Write a SQL query selecting sale_id, the year extracted from sale_date as sale_year, and the month extracted as sale_month using EXTRACT.
SQL
Need a hint?

Use EXTRACT(YEAR FROM date) and EXTRACT(MONTH FROM date) to get year and month.

4
Combine formatted date and extracted parts in one query
Write a SQL query selecting sale_id, the formatted date as formatted_date using DATE_FORMAT, and the extracted year and month as sale_year and sale_month using EXTRACT from the sales table.
SQL
Need a hint?

Combine DATE_FORMAT and EXTRACT in the same SELECT statement.