Lesson 04 Demo 01
Perform Numeric Functions in MySQL
Objective: To demonstrate how to use the numeric function to manipulate
numeric values in MySQL
Tools required: MySQL Workbench
Prerequisites: None
Steps to be followed:
1. Log in to the MySQL Database
2. Create a Database and Table
3. Use the ABSOLUTE function
4. Use the CEILING function
5. Use the FLOOR function
6. Use the ROUND function
7. Use the MODE function
8. Use the POWER function
Step 1: Log in to the MySQL Database
1.1 Connect to your MySQL server using a MySQL client or command-line
interface
Step 2: Create a Database and Table
2.1 Create the salary_database by executing the following SQL statement
and using the Database
SQL Code:
CREATE DATABASE IF NOT EXISTS salary_database
USE salary_database;
2.2 Create the employee_info table within the salary_database with
columns for employee ID (emp_id), employee name (emp_name), salary
(salary), and designation (designation):
SQL Code:
CREATE TABLE IF NOT EXISTS employee_info (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
salary DECIMAL(10, 2),
designation VARCHAR(100));
2.3 Insert sample data into the employee_info table:
SQL Code:
INSERT INTO employee_info (emp_id, emp_name, salary, designation)
VALUES
(1, 'John Doe', 50000.50, 'Manager'),
(2, 'Jane Doe', 60000.75, 'Engineer'),
(3, 'Bob Smith', 75000.25, 'Analyst');
Step 3: Use the ABSOLUTE function
3.1 Write a SELECT query using the ABS() function in the salary column
SQL Code:
SELECT emp_name, ABS(salary) AS absolute_salary FROM employee_info;
Step 4: Use the CEILING function
4.1 Write a SELECT query using the CEIL() or CEILING() function in the salary
column
SQL Code:
SELECT emp_name, CEIL(salary) AS rounded_up_salary FROM
employee_info;
Step 5: Use the FLOOR function
5.1 Write a SELECT query using the FLOOR() function in the salary column
SQL Code:
SELECT emp_name, FLOOR(salary) AS rounded_down_salary FROM
employee_info;
Step 6: Use the ROUND function
6.1 Write a SELECT query using the ROUND() function in the salary column
SQL Code:
SELECT emp_name, ROUND(salary, 1) AS rounded_salary FROM
employee_info;
Step 7: Use the MOD() function
7.1 Write a SELECT query using the MOD() function in the emp_id column
SQL Code:
SELECT emp_name, MOD(emp_id, 2) AS modulus_result FROM
employee_info;
Step 8: Use the POWER function
8.1 Write a SELECT query using the POWER() function in the salary column
SQL Code:
SELECT emp_name, POWER(salary, 2) AS squared_salary FROM
employee_info;
Numeric functions are executed on a specific dataset using these procedures.