Menu Close

How to Get Quarter from Date in MySQL

quarter from date in MySQL

In this article, we will see how to get the quarter from Date in MySQL with the help of the MySQL quarter function. In real-life applications, sometimes we have to make decisions on the basis of a quarter of the date, Then in that time, we can use a quarter() function offered by MySQL which falls in the DateTime functions category in MySQL.

Let’s take a definition and example of the MySQL quarter() function.

What is quarter() function in MySQL?

The QUARTER() function is a MySQL DateTime function that takes a date as a parameter and returns an integer number from 1 to 4 which indicates the quarter of the passed date.

As you can see in the below table.

MonthsQuarter
January – March1
April – June2
July – September3
Ooctober – December4

Syntax

This is the syntax of the quarter() function.

quarter(date)

Let’s see the use cases of the quarter function in MySQL.

Get quarter from Date in MySQL

For a demonstration of this article, I have already created a table with the name of the employee along with some records. if you don’t know how to create a table and insert records into a table please follow the given tutorial links.

In fact, you use the below CREATE TABLE statement in order to create a new table.

CREATE TABLE employee (
   id int NOT NULL AUTO_INCREMENT,
   emp_id varchar(20) DEFAULT NULL,
   first_name varchar(50) DEFAULT NULL,
   last_name varchar(50) DEFAULT NULL,
   email varchar(100) DEFAULT NULL,
   gender varchar(10) DEFAULT NULL,
   age int DEFAULT NULL,
   salary decimal(20,2) DEFAULT NULL,
   designation varchar(100) DEFAULT NULL,
   department varchar(100) DEFAULT NULL,
   joining_date date DEFAULT NULL,
   company varchar(100) DEFAULT NULL,
   data_created datetime DEFAULT CURRENT_TIMESTAMP,
   PRIMARY KEY (id)
 )

Let me show you the records of the employee table by using the SELECT statement. I have selected only a few columns of the table like first_name, last_name, salary, and joining_date.

SELECT 
    first_name,
    last_name,
    salary,
    joining_date
FROM
    employee;

After executing the above SQL Statement, All the records of the employee table will be shown. These are the records in my case, it might be different in your case.

Employee Table Records


We will apply a quarter() function on the joining_date column of the employee.

The quarter() function will take joining_date as a parameter and return a new integer value that indicates the quarter of the year.
Let’s try to get a quarter of the year in MySQL in different scenarios.

Note:- You can match the quarter number by using the above given quarter table.

Scenario 1:

In this scenario, we will try to get the quarter from the passed joining_date column.

SELECT 
    first_name,
    last_name,
    salary,
    joining_date,
    QUARTER(joining_date) AS 'quarter'
FROM
    employee;

Output

+------------+-----------+-----------+--------------+---------+
| first_name | last_name | salary    | joining_date | quarter |
+------------+-----------+-----------+--------------+---------+
| Vishvajit  | Rao       |  25000.00 | 2021-12-23   |       4 |
| Vinay      | Kumar     |  35000.00 | 2020-08-22   |       3 |
| Minakshi   | Kumari    |  30000.00 | 2021-07-20   |       3 |
| John       | Doe       |  80000.00 | 2020-11-06   |       4 |
| Akash      | Saini     |  25500.00 | 2022-01-30   |       1 |
| Harshita   | Singh     |  20000.00 | 2021-12-25   |       4 |
| Ayush      | Shukla    |  23500.00 | 2021-08-20   |       3 |
| Abhishek   | Mishra    | 120000.00 | 2018-05-29   |       2 |
| Vaishanvi  | Mehta     |  75250.00 | 2020-02-26   |       1 |
| Mahima     | Chaudhari |  40300.00 | 2021-03-23   |       1 |
| Arushi     | Shulka    |  40300.00 | 2021-05-29   |       2 |
| Mahesh     | Kumar     |  85340.00 | 2020-03-22   |       1 |
| Ajay       | Gupta     |  85340.00 | 2021-08-28   |       3 |
| Manish     | Das       |  90000.00 | 2021-07-20   |       3 |
| David      | Russel    | 105000.00 | 2021-05-21   |       2 |
| Nishant    | Chahal    |  65000.00 | 2021-09-24   |       3 |
| Darshan    | Natrajan  |  40000.00 | 2021-05-20   |       2 |
+------------+-----------+-----------+--------------+---------+

As you can see in the above output, we have successfully fetched the quarter from the joining_date column even you can match the quarter from the joining_date column’s month to the above Quarter Table.

You can see, I have executed the same code in my MySQL workbench application also.

Get Quarter from Date in MySQL

Scenario 2:

In this scenario, we are about to see the total number of employees who have joined the organization in a specific quarter of a year. Here we will use the GROUP BY clause, count() aggregate function, a quarter() function, and extract() function.

Let’s see how.

SQL query will be like this.

SELECT 
    COUNT(*) AS 'Total Employees Joined',
    EXTRACT(YEAR FROM joining_date) AS 'Year',
    QUARTER(joining_date) AS 'Quarter'
FROM
    employee
GROUP BY EXTRACT(YEAR FROM joining_date) , QUARTER(joining_date);

After executing the above SQL query the output will be, It might be different in your case.

+------------------------+------+---------+
| Total Employee Joined | Year | Quarter |
+------------------------+------+---------+
|                      2 | 2021 |       4 |
|                      1 | 2020 |       3 |
|                      5 | 2021 |       3 |
|                      1 | 2020 |       4 |
|                      1 | 2022 |       1 |
|                      1 | 2018 |       2 |
|                      2 | 2020 |       1 |
|                      1 | 2021 |       1 |
|                      3 | 2021 |       2 |
+------------------------+------+---------+

SQL Query has been executed in the MySQL Workbench application.

Get Quarter from Date in MySQL

Scenario 3:

In this scenario, we are about to get all those employees who have joined the company in the 3rd quarter of the year and their first_name must start with ‘M‘.
To achieve this, I have formed below SQL query.

SELECT 
    first_name,
    last_name,
    salary,
    joining_date,
    QUARTER(joining_date) AS 'quarter'
FROM
    employee
WHERE
    QUARTER(joining_date) = 3
        AND first_name LIKE 'M%';

After executing the above SQL query, The output will be:

+------------+-----------+----------+--------------+---------+
| first_name | last_name | salary   | joining_date | quarter |
+------------+-----------+----------+--------------+---------+
| Minakshi   | Kumari    | 30000.00 | 2021-07-20   |       3 |
| Manish     | Das       | 90000.00 | 2021-07-20   |       3 |
+------------+-----------+----------+--------------+---------+

Ass You can see below, The above code has been executed in MySQL workbench also.

Get Quarter from Date in MySQL

This is how you can use the MySQL quarter() function to get the quarter from Date. Like the above scenarios, you can form multiple scenarios which are most important from the interview point of view.

👉 MySQL quarter() function documentation – Click Here

Related MySQL Articles:

Conclusion

So, In this article, MySQL quarter function has been successfully covered along with examples. You can use this function to get quarter from date in MySQL.
In real-life applications, sometimes we have to make decisions on the basis of a quarter of the year like counting total employees of the quarter of the year, applying group by clause on quarter function, displaying records of specific quarters of the past year, and so on, In that time, this function is going to be very useful.

If you found this article helpful, please share and keep visiting for further MySQL articles.

Happy Coding…

How to Give AWS Lambda Access to IAM User
How to Convert Excel to JSON in Python

Related Posts