Skip to content

i-ambale/SQL-DateTime-Functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SQL DateTime Functions

Β© ExploreAI Academy

πŸ“˜ Overview In this exercise, we explore the use of SQL DateTime functions to work with date and time data. You'll learn how to extract, filter, and manipulate date-related fields from a database using SQL.

These techniques are particularly useful when analyzing time-based data such as sales, revenue trends, or customer activity.


🎯 Learning Objectives

By the end of this exercise, you will be able to:

βœ… Measure the period between dates and times.

βœ… Extract specific parts of a DateTime column (e.g., year, month).

βœ… Filter records using logical and comparison operators on DateTime fields.


🧰 Tools & Requirements

  • SQL Dialect: SQLite (Compatible with other SQL engines, but minor differences may exist)

  • Sample Database: chinook.db (A small demo database with tables such as invoices, customers, and employees)

  • Environment: Local environment (Jupyter Notebook, SQLite browser, or compatible SQL IDE)


πŸ—οΈ Sample Use Cases

  • Calculate monthly or yearly revenue

  • Find the age of employees when hired

  • Analyze customer purchases over time

  • Filter records based on date ranges


πŸ§ͺ Example Queries

  1. Extracting Year from Date:
SELECT 
    SUBSTR(InvoiceDate, 1, 4) AS Year, 
    ROUND(SUM(Total), 2) AS Revenue
FROM invoices
GROUP BY Year
ORDER BY Year;
  1. Filtering by Date:
SELECT * 
FROM invoices
WHERE InvoiceDate >= '2010-01-01';
  1. Calculating Duration Between Two Dates: SQLite does not support DATEDIFF(), but we can use julianday():
SELECT 
    CustomerId,
    InvoiceDate,
    julianday('now') - julianday(InvoiceDate) AS Days_Since_Invoice
FROM invoices;

🧼 Tips for Clean DateTime Queries

  • Use SUBSTR() or strftime() to extract year/month/day.

  • For SQLite: use julianday() to compute date differences.

  • Always ensure your dates are in ISO format (YYYY-MM-DD) for best results.


πŸ“Ž References

  • SQLite Date & Time Functions

  • ExploreAI Academy SQL Curriculum


πŸ“‚ File Structure

πŸ“ sql_datetime_functions/
β”‚
β”œβ”€β”€ chinook.db                # SQLite sample database
β”œβ”€β”€ datetime_exercise.sql     # SQL queries for the exercise
β”œβ”€β”€ README.md                 # This file

About

ALX Exercises

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published