×

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples.

Introduction of MySQL Floor() function:

FLOOR() function in MySQL is used to return the largest integer value which will be either equal to or less than from a given input number.

Syntax of the MySQL Floor() function

The syntax of the MySQL FLOOR() function is given as follows:

FLOOR(expr);

The type of the input number depends on data type of the return value. The type of the returned value is exact numeric or floating-point type respectively, when the type of the input number is exact numeric or floating-point type.

Parameter or arguments used in MySQL Floor() function:

Only one parameter is accepted by the FLOOR (expr) Function, as mentioned above and described below:

expr: This is the expression whose floor value we want to calculate.

Returns: It will return the closest integer which is <=X. So, if Y is integer than it will return Y. Otherwise, largest integer which is lesser than Y.

MySQL versions used for Floor() function:

The FLOOR() function can be used in the given below MySQL versions.

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL Floor() function:

Let’s take some examples of using the FLOOR() function to understand it better.

Example: 1

Now we will Apply FLOOR() function to a positive number in MySQL.

SELECT FLOOR(1.59);

The Output is:

1
1 row in set (0.00 sec)

The result is 1 because it is the largest integer which is less than or equal to 1.59.

Example: 2

Now we will Apply FLOOR() function to a negative number in MySQL.

SELECT FLOOR(1.59);

The Output is:

-2
1 row in set (0.00 sec)

The largest integer which is less than or equal to -1.59 is 2, therefore, the FLOOR() function returned -2.

Example: 3

Now we will Use FLOOR() function in the query of MySQL.

Lets See the following commoditys table from the sample database:

Commodity
Commoditycode
Commodityname
Commodityline
Commodityscale
Commodityvendor
Commoditydescription
Quantityinstock
Buyprice
Msrp

The following statement finds the average stock for each commodity line:

SELECT
commodityLine,
FLOOR(AVG(quantityInStock)) averageStock
FROM
commoditys
GROUP BY
commodityLine
ORDER BY
averageStock;

Here is the output:

CommoditylineAveragemsrp
Trains2154
Ships2365
Vintage cars2973
Planes3482
Motorcycles3894
Trucks and buses4107
Classic cars5115

Because the AVG() function returns a decimal value, we need to apply the FLOOR() function to the average result.

Some more examples:

Example 4:

Now we will Apply FLOOR() function to a +ve integer in MySQL:

SELECT FLOOR(4) AS Floor_Value;

Output:

Floor_Value

4
1 row in set (0.00 sec)

Example 5 :

Now we will Apply FLOOR() function to a -ve integer.

SELECT FLOOR(-6) AS Floor_Value;

Output:

Floor_Value
-6
1 row in set (0.00 sec)

Example 6:

Now we will Apply FLOOR() function to a +ve floating number.

SELECT FLOOR(1.5) AS Floor_Value;

Output:

Floor_Value
1
1 row in set (0.00 sec)

Example 7:

Now we will Apply FLOOR() function to a -ve floating number.

SELECT FLOOR(-1.5) AS Floor_Value;

Output:

Floor_Value
-2
1 row in set (0.00 sec)

Example 8:

Now we will use FLOOR value of a numeric column in a table.

Table – Number

X
90.55
0
-9
-45.76
0.25
SELECT X, FLOOR(X) AS X_Floor FROM Number;

The Output is:

XX_Floor
90.5590
00
-9-9
-45.76-46
0.250

Summary:

In this context, we have learned how we can use the MySQL FLOOR() function to find the largest integer less than or equal to the number given.


Related Topics

MySQL QUARTER() function

In this context, we will learn how we can use the MySQL QUARTER() function with proper syntax and good examples. Introduction of MySQL QUARTER() function The QUARTER() function in MySQL returns the...

3 minutes read.

MySQL CONCAT() function

In this context, we will learn how we can use the MySQL CONCAT() function with proper syntax and good examples. Introduction of MySQL CONCAT() function By using the MySQL CONCAT function, we...

3 minutes read.

MySQL Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL CEILING() function

In this context, we will learn how we can use the MySQL CEILING () function with proper syntax and good examples. Introduction of MySQL CEILING () function The CEILING() function accepts the...

2 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL LENGTH() Function

In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples. Introduction of MySQL LENGTH() function This function in MySQL is used to...

2 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL ELT() function

In this context, we will learn how we can use the MySQL ELT() function with proper syntax and good examples. Introduction of MySQL ELT() function In the ELT function, the number field...

2 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 minutes read.