0
0
SQLquery~30 mins

DATE arithmetic (DATEDIFF, DATE_ADD) in SQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Calculate Employee Contract Durations and Renewal Dates
📖 Scenario: You work in the HR department of a company. You have a table of employees with their contract start and end dates. You want to calculate how many days each employee's contract lasts and find the date when their contract should be renewed by adding 30 days to the end date.
🎯 Goal: Create SQL queries that calculate the duration of each employee's contract in days using DATEDIFF and find the renewal date by adding 30 days to the contract end date using DATE_ADD.
📋 What You'll Learn
Create a table called employees with columns id, name, contract_start, and contract_end.
Insert exactly three employees with specified contract start and end dates.
Write a query to calculate the contract duration in days using DATEDIFF.
Write a query to calculate the renewal date by adding 30 days to contract_end using DATE_ADD.
💡 Why This Matters
🌍 Real World
HR departments often need to track contract durations and renewal dates to manage employee agreements efficiently.
💼 Career
Knowing how to use date arithmetic functions like DATEDIFF and DATE_ADD is essential for database analysts and developers working with time-based data.
Progress0 / 4 steps
1
Create the employees table and insert data
Create a table called employees with columns id (integer), name (varchar), contract_start (date), and contract_end (date). Then insert these three employees exactly: (1, 'Alice', '2023-01-01', '2023-12-31'), (2, 'Bob', '2023-03-15', '2023-09-14'), and (3, 'Charlie', '2023-06-01', '2024-05-31').
SQL
Need a hint?

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

2
Add a helper column alias for contract duration calculation
Write a SELECT query from employees that includes all columns and add a new column alias called duration_days which will hold the number of days between contract_start and contract_end. Use DATEDIFF(contract_end, contract_start) for this calculation.
SQL
Need a hint?

Use Datediff(contract_end, contract_start) and alias it as duration_days.

3
Calculate contract renewal date by adding 30 days
Extend the previous SELECT query to add a new column alias called renewal_date which adds 30 days to the contract_end date. Use DATE_ADD(contract_end, INTERVAL 30 DAY) for this calculation.
SQL
Need a hint?

Use DATE_ADD(contract_end, INTERVAL 30 DAY) and alias it as renewal_date.

4
Complete the query with ordering by renewal date
Modify the SELECT query to order the results by renewal_date in ascending order.
SQL
Need a hint?

Add ORDER BY renewal_date ASC at the end of the query.