Lesson 04 Demo 05
Perform Mathematical Functions in MySQL
Objective: To demonstrate how to use mathematical functions for data analysis 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 SQUARE ROOT function
4. Use the EXPONENTIAL function
5. Use the LOG function
6. Use the BASE-10 LOG function
7. Use the RANDOM NUMBER 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 math_functions_database by executing the following SQL
statement:
SQL Code:
CREATE DATABASE IF NOT EXISTS math_functions_database;
USE math_functions_database;
2.2 Create the math_data table within the math_functions_database with
columns for id INT PRIMARY KEY, number_column DECIMAL(10, 2)
SQL Code:
CREATE TABLE IF NOT EXISTS math_data (
id INT PRIMARY KEY,
number_column DECIMAL(10, 2)
);
2.3 Insert sample data into the math_data table:
SQL Code:
INSERT INTO math_data (id, number_column) VALUES
(1, 25),
(2, 3),
(3, 2.718),
(4, 100),
(5, 0.75);
Step 3: Use the SQUARE ROOT function
3.1 Calculate the square root of the numbers in the number_column of the
math_data table using the SQRT() function
SQL Code:
SELECT id, number_column, SQRT(number_column) AS square_root
FROM math_data;
Step 4: Use the EXPONENTIAL function
4.1 Calculate the exponential value of the numbers in the number_column of
the math_data table using the EXP() function
SQL Code:
SELECT id, number_column, EXP(number_column) AS exponential_value
FROM math_data;
Step 5: Use the LOG function
5.1 Calculate the natural logarithm of the numbers in the number_column
of the math_data table using the LOG() function
SQL Code:
SELECT id, number_column, LOG(number_column) AS natural_logarithm
FROM math_data;
Step 6: Use the BASE-10 LOG function
6.1 Calculate the base-10 logarithm of the numbers in the number_column
of the math_data table using the LOG10() function
SQL Code:
SELECT id, number_column, LOG10(number_column) AS
base_10_logarithm FROM math_data;
Step 7: Use the RANDOM NUMBER function
7.1 Generate a random number for each row using the RAND() function
SQL Code:
SELECT id, RAND() AS random_number FROM math_data;
Mathematical functions are executed on a specific dataset using these
procedures.