MySQL Part - 9
Date Functions
• Date and Time functions allow us to perform many
types of tasks on Date type data.
• The default date format in MySQL is YYYY-MM-DD.
CURDATE()
• Returns the current date in 'YYYY-MM-DD' format
SELECT CURDATE();
2025-04-21
NOW ()
• Returns the current date and time in 'YYYY-MM-DD
HH:MM:SS'
SELECT NOW();
2025-04-21 09:38:00
SYSDATE()
• Returns the current date and time in 'YYYY-MM-DD
HH:MM:SS' format.
SELECT SYSDATE();
2025-04-21 09:38:52
DATE ()
• Extracts the date part of a date/datetime expression
SELECT DATE(NOW( ));
2025-04-21
SELECT DATE('2020-05-07 01:02:03');
2020-05-07
SELECT DATE('2009-10-16 01:02:03');
2009-10-16
DAY ()
• Returns the numeric day from the date passed, in
the range 1 to 31
SELECT DAY(' 2025-04-21');
21
Select id, date_join, day(date_join) from employee;
MONTH ()
• Returns the numeric month from the date passed, in
the range 0 to 12.
SELECT MONTH(NOW());
4
Select id, date_join, month(date_join) from employee;
YEAR ()
• Returns the year for date passed in the range 0 to
9999. Returns values like 1998, 2010, 1996, etc.
SELECT YEAR(NOW());
2020
SELECT id,date_join,year(date_join) from Employee;
DAYNAME ()
• It returns the name of the weekday for the date passed,
like Monday or Friday.
SELECT DAYNAME(' 2025-04-21');
Monday
Select id, date_join, dayname(date_join) from employee;
MONTHNAME ()
• It returns the name of the month for the date passed,
like January or September.
SELECT MONTHNAME(' 2025-04-21');
April
Select id, date_join, monthname(date_join) from employee;
Aggregate/ Group/ Multiple Row
Functions
SUM ()
• Returns the SUM of the values for the given column.
SELECT SUM(salary) FROM employee;
SELECT SUM(salary) FROM employee WHERE city = 'Delhi';
AVG ()
• Returns the AVERAGE of the values of the given column.
SELECT AVG(salary) FROM employee;
SELECT AVG(salary) FROM employee WHERE first_name like '%a';
MAX ()
• Returns the MAXIMUM (highest) of the values for the
given column.
SELECT MAX(salary) FROM employee;
SELECT MAX(salary) FROM employee WHERE city = 'Delhi';
SELECT MAX(first_name) FROM employee;
SELECT MAX(date_join) FROM employee;
MIN ()
• Returns the MINIMUM (lowest) of the values under the
specified column.
SELECT MIN(salary) FROM employee;
SELECT MIN(salary) FROM employee WHERE city = 'Delhi';
SELECT MIN(first_name) FROM employee;
SELECT MIN(date_join) FROM employee;
COUNT ()
• Returns the COUNT of the number of values under the
specified column.
SELECT COUNT(salary) FROM employee;
SELECT COUNT(distinct city) FROM employee;
SELECT COUNT(first_name) FROM employee;
SELECT COUNT(date_join) FROM employee;
Using COUNT (*)
• Using * with count, counts the number of records in
the table.
SELECT COUNT(*) FROM employee;
SELECT COUNT(*) FROM employee where last_name='sharma';