Open In App

MySQL QUARTER() Function

Last Updated : 10 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

MySQL QUARTER() function is used to return the quarter of the year for a given date value.

QUARTER function in MySQL returns values between 1 to 4. For January to March, it returns 1, for April to June, it returns 2, For July to September, it returns 3, For October to December, it returns 4.

Syntax

MySQL QUARTER() function syntax is:

QUARTER(date)

Parameter:

  • date: The date or DateTime from which we want to extract the quarter.

MySQL QUARTER() Function Examples

Let's look at some examples of the QUARTER function in MySQL.

Example 1

In this example, we are finding the Current QUARTER Using QUARTER() Function.

SELECT QUARTER(NOW()) AS CURRENT_QUARTER;

Output :

CURRENT_QUARTER
4

Example 2

In this example, we are finding the Quarter from given DateTime Using QUARTER() Function.

SELECT QUARTER('2020-04-26 08:09:22') AS QUARTER_NUMBER;

Output :

QUARTER_NUMBER
2

Example 3

In this example, we are finding the Quarter from given DateTime Using QUARTER() Function when the date is NULL.

SELECT QUARTER(NULL) AS QUARTER_NUMBER;

Output :

QUARTER_NUMBER
NULL

Using QUARTER function on a table column 

The QUARTER function can also be used to find the total product sold for every QUARTER. To demonstrate create a table named.

Product :

CREATE TABLE Product(
  Product_id INT AUTO_INCREMENT,  
  Product_name VARCHAR(100) NOT NULL,
  Buying_price DECIMAL(13, 2) NOT NULL,
  Selling_price DECIMAL(13, 2) NOT NULL,
  Selling_Date Date NOT NULL,
  PRIMARY KEY(Product_id)
);

Now insert some data to the Product table :

INSERT INTO Product (Product_name, Buying_price, Selling_price, Selling_Date)
VALUES
  ('Audi Q8', 10000000.00, 15000000.00, '2018-01-26'),
  ('Volvo XC40', 2000000.00, 3000000.00, '2018-04-20'),
  ('Audi A6', 4000000.00, 5000000.00, '2018-07-25'),
  ('BMW X5', 5000500.00, 7006500.00, '2018-10-18'),
  ('Jaguar XF', 5000000, 7507000.00, '2018-01-27'),
  ('Mercedes-Benz C-Class', 4000000.00, 6000000.00, '2018-04-01'),
  ('Jaguar F-PACE', 5000000.00, 7000000.00, '2018-12-26'),
  ('Porsche Macan', 6500000.00, 8000000.00, '2018-04-16');

So, Our table looks like :

Product_idProduct_nameBuying_priceSelling_priceSelling_Date;
1Audi Q810000000.00 15000000.00 2018-01-26
2Volvo XC402000000.00 3000000.00 2018-04-20
3 Audi A6 4000000.00 5000000.00 2018-07-25
4BMW X55000500.00 7006500.00 2018-10-18 
5Jaguar XF 5000000.00 7507000.002019-01-27
6Mercedes-Benz C-Class4000000.00 6000000.00 2019-04-01
7Jaguar F-PACE 5000000.00 7000000.002019-12-26
8Porsche Macan 6500000.00 8000000.00 2020-04-16

Now, we are going to find the number of products sold per quarter by using the MONTH() function.

SELECT 
    QUARTER(Selling_Date) AS quarter, 
    COUNT(Product_id) AS PRODUCT_SOLD 
FROM 
    Product   
GROUP BY QUARTER(Selling_Date)       
ORDER BY QUARTER(Selling_Date);

Output :

QUARTERPRODUCT_SOLD
12
23
31
42

Next Article
Article Tags :

Similar Reads