×

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 Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 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 DATE_FORMAT() function

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

3 minutes read.

MySQL COUNT function

The count function returns the number of rows in the table. This function shows either the entire rows count or the required row count of the table. It determines the...

5 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL SOUNDEX() function

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

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 vs SQL

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL NOW() function

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

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 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 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 Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

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...

3 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.