Using ABS and MOD Functions in MySQL
📖 Scenario: You are managing a small store's sales data. Some sales amounts are recorded as negative numbers due to returns. You want to clean up the data by converting all sales amounts to positive numbers and find out which sales amounts are even or odd.
🎯 Goal: Build a MySQL query that uses the ABS function to get absolute values of sales amounts and the MOD function to check if the sales amounts are even or odd.
📋 What You'll Learn
Create a table called
sales with columns id (integer) and amount (integer).Insert the exact sales amounts: -15, 22, -7, 40, and -3 with ids 1 to 5 respectively.
Write a SELECT query that uses
ABS(amount) to show positive sales amounts.Add a column in the SELECT query that uses
MOD(amount, 2) to show if the amount is even (0) or odd (1).💡 Why This Matters
🌍 Real World
Stores often need to clean and analyze sales data, especially when returns cause negative values. Using ABS helps get clean positive sales figures, and MOD helps categorize sales amounts.
💼 Career
Database analysts and developers use ABS and MOD functions to prepare and analyze numeric data for reports and business decisions.
Progress0 / 4 steps