MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
In this tutorial, you will learn about the MySQL CEIL
and CEILING
functions, which are equivalent and can be used interchangeably. These functions return the smallest integer value that is greater than or equal to a specified number.
Prerequisites:
Tutorial:
To start the mysql
command-line client, open a terminal or command prompt, and enter:
mysql -u [username] -p
Replace [username]
with your MySQL username and enter your password when prompted.
Select the database containing the table you want to query:
USE [database_name];
Replace [database_name]
with the name of your database.
You can use the CEIL
or CEILING
function with a constant value. For example:
SELECT CEIL(3.5); -- Returns 4 SELECT CEILING(3.5); -- Returns 4
Suppose you have a products
table with the following columns: id
, name
, price
, and quantity
. You can use the CEIL
or CEILING
function to calculate the total number of boxes required to pack the products, assuming each box can hold up to 10 items.
SELECT id, name, price, quantity, CEIL(quantity / 10) AS total_boxes FROM products;
You can also use the CEIL
or CEILING
function with expressions. For example, let's say you want to calculate the discount price after applying a 25% discount to each product's price:
SELECT id, name, price, CEIL(price * 0.75) AS discount_price FROM products;
EXIT;
Now you have learned how to use the MySQL CEIL
and CEILING
functions. These functions are useful when you need to round up a number to the nearest integer, such as in calculating the total number of boxes required to pack items or rounding up discount prices.
How to use CEIL function in MySQL:
CEIL
function to round up numbers to the nearest integer.SELECT CEIL(number) AS rounded_up_value;
Rounding up numbers in MySQL with CEIL:
CEIL
to round up decimal or floating-point numbers.SELECT CEIL(5.75) AS rounded_up_value; -- Result: 6
MySQL CEILING function examples:
SELECT CEIL(8.3) AS rounded_up_value; -- Result: 9
Using CEIL for integer rounding in MySQL:
CEIL
rounds positive decimal numbers up to the next integer.SELECT CEIL(3.2) AS rounded_up_value; -- Result: 4
Rounding decimals up with CEIL in MySQL:
CEIL
.SELECT CEIL(2.45) AS rounded_up_value; -- Result: 3
CEIL vs ROUND function in MySQL:
CEIL
with ROUND
for different rounding behaviors.SELECT CEIL(4.3) AS ceil_result, ROUND(4.3) AS round_result; -- Result: 5, 4
MySQL CEIL for rounding positive numbers:
CEIL
to round positive numbers up.SELECT CEIL(7.8) AS rounded_up_value; -- Result: 8
Negative numbers and CEIL in MySQL:
CEIL
.SELECT CEIL(-3.6) AS rounded_up_value; -- Result: -3
CEIL function in MySQL for floating-point values:
CEIL
for rounding floating-point values.SELECT CEIL(9.99) AS rounded_up_value; -- Result: 10
MySQL CEIL function in mathematical expressions:
CEIL
in mathematical expressions.SELECT CEIL(column1 * 1.5) AS rounded_result FROM table_name;
Applying CEIL in WHERE clause in MySQL:
CEIL
in the WHERE clause for conditional filtering.SELECT column1 FROM table_name WHERE CEIL(column2) > 5;
Using CEIL with aggregate functions in MySQL:
CEIL
with aggregate functions for rounded results.SELECT AVG(CEIL(column1)) AS average_rounded_value FROM table_name;
MySQL CEIL for rounding in SELECT queries:
CEIL
within SELECT queries for rounded results.SELECT column1, CEIL(column2) AS rounded_value FROM table_name;