MySQL Division and Comparison Functions
Choose the operation that matches the measurement instead of relying on implicit conversion.
How it works
DIV returns an integer quotient and MOD returns a remainder. GREATEST and LEAST choose an extreme among supplied values, not across table rows. The examples use a small pack size and explicit stock bounds. Row aggregates such as MAX and MIN answer a different question and are covered in the aggregate lesson. Missing values and mixed types can affect comparisons, so validate inputs rather than using these helpers as an undocumented repair step.
Choose the function from its contract
- DIV: Counts whole packs.
- MOD: Counts the remainder.
- GREATEST: Chooses the largest supplied value.
- LEAST: Chooses the smallest supplied value.
Run in a disposable MySQL lab
Use Oracle MySQL 8.4 with InnoDB, not Bhylu’s SQLite playground. The examples were reviewed on MySQL 8.4.8. XAMPP may provide MariaDB, which has different feature and collation behaviour. Check your actual server before running the script. These lessons do not require a change to Bhylu’s PHP, Apache or normal database service.
- On a separate lab instance, create the empty bhylu_mysql_lab schema. Stop if it already exists; do not overwrite an existing database.
- Select that schema and import the complete repair-parts seed once. It sets the documented session mode, utf8mb4 collation, UTC and English date-name locale.
- Run this lesson in a fresh copy of the seed, not after another lesson’s writes. Use the command-line client or a compatible editor. DELIMITER lines are client instructions for stored-program examples.
Download this complete lesson script. Its setup is the linked seed; review the dataset before changing it. Administrative and destructive examples are restricted to the specifically named disposable objects. Every script repeats the session settings because each new connection has its own session. No MySQL browser runner is currently available.
Example
SET NAMES utf8mb4 COLLATE utf8mb4_0900_ai_ci;
SET SESSION sql_mode = 'STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
SET SESSION time_zone = '+00:00';
SET SESSION lc_time_names = 'en_US';
SELECT 'DIV' AS operation, 17 DIV 5 AS result;
SELECT 'MOD' AS operation, MOD(17,5) AS result;
SELECT 'GREATEST' AS operation, GREATEST(0,4,7) AS result;
SELECT 'LEAST' AS operation, LEAST(3,8,5) AS result;
Expected result
DIV | 3 MOD | 2 GREATEST | 7 LEAST | 3
Seventeen pieces make three complete packs with two left over. Negative quotient and remainder behaviour deserves its own test if the domain permits negative inputs; a stock count normally should not. A zero divisor must be rejected or handled deliberately. Strict SQL mode does not mean every expression warning has the same outcome in SELECT and a data-changing statement.
Use the official MySQL 8.4 reference for statement contracts and version details.