MySQL - CEILING() Function



MySQL provides a set of functions to perform various numerical functions. The CEILING() function of MySQL accepts an integer value as a parameter and returns the smallest integer not less than the given value.

The ceiling operation differs from approximation, as the value after the decimal point are neglected (unlike in approximation).

This function is a synonym for the CEIL() function in MySQL.

Syntax

Following is the syntax of MySQL CEILING() function −

CEILING(X)

Parameters

This function takes a numeric value (float or double as a parameter.

Return Value

This function returns the smallest integer greater than or equal to the given value.

Example

In the below query, we are using CEILING() function to calculate the ceiling value (smallest integer greater than or equal to) of 22.3 −

SELECT CEILING(22.3) As Result;

Output

This will produce the following result −

Result
23

Example

You can also pass a function as a value to this function as shown below. Here, the value of PI() function is 3.14 −

SELECT CEILING(PI()) As Result;

Output

The output for the query above is produced as given below −

Result
4

Example

You can also pass values to this function as strings −

SELECT CEILING("2254.554") As Result;

Output

This will produce the following result −

Result
2255

Example

In the below query, we are creating a table named CUSTOMERS

CREATE TABLE CUSTOMERS (
   ID INT AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),
   PRIMARY KEY (ID)
);

The following query inserts 7 records into the above created table −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES 
(1, 'Ramesh', 32, 'Ahmedabad', 2000.56 ),
(2, 'Khilan', 25, 'Delhi', 1500.33 ),
(3, 'Kaushik', 23, 'Kota', 2000.66 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.95 ),
(5, 'Hardik', 27, 'Bhopal', 8500.99 ),
(6, 'Komal', 22, 'Hyderabad', 4500.11 ),
(7, 'Muffy', 24, 'Indore', 10000.50 );

Execute the below query to fetch all the inserted records in the CUSTOMERS table −

Select * From CUSTOMERS;

Following is the CUSTOMERS table −

ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.56
2 Khilan 25 Delhi 1500.33
3 Kaushik 23 Kota 2000.66
4 Chaitali 25 Mumbai 6500.95
5 Hardik 27 Bhopal 8500.99
6 Komal 22 Hyderabad 4500.11
7 Muffy 24 Indore 10000.50

Now, let us use the MySQL CEILING() function on the SALARY column to return the smallest integer value that is greater than or equal to each value in the SALARY column −

SELECT ID, NAME, CEILING(SALARY) FROM CUSTOMERS;

Output

The output for the query above is produced as given below −

ID NAME SALARY
1 Ramesh 2001
2 Khilan 1501
3 Kaushik 2001
4 Chaitali 6501
5 Hardik 8501
6 Komal 4501
7 Muffy 10001
Advertisements