MySQL CREATE FUNCTION Statement | MySQL Creating stored function | Create Functions My SQL
Step 1: Create a New Query
First of all, open MySQL Workbench.
After opening MySQL Workbench, go to the SQL Editor and open a new Query Tab.
This is where we are going to write all of our SQL commands.
The first thing we need to do is create a database.
After opening MySQL Workbench, go to the SQL Editor and open a new Query Tab.
This is where we are going to write all of our SQL commands.
The first thing we need to do is create a database.
Step 2: Create the Database
Since we are creating a marksheet for students, let's create a database named school.
Write the following command:
CREATE DATABASE school;
After writing the command, select it and click on the Run button.
If the query runs successfully, our database has been created.
So now we have a database called school, and we will use this database for our student marksheet.
Write the following command:
CREATE DATABASE school;
After writing the command, select it and click on the Run button.
If the query runs successfully, our database has been created.
So now we have a database called school, and we will use this database for our student marksheet.
Step 3: Select the Database
Creating the database is not enough.
We also need to tell MySQL that we want to work inside this particular database.
For that, we use the USE command.
Write:
USE school;
Now run the query.
After running this command, the school database becomes our active database.
From this point onward, the tables that we create will be created inside the school database.
We also need to tell MySQL that we want to work inside this particular database.
For that, we use the USE command.
Write:
USE school;
Now run the query.
After running this command, the school database becomes our active database.
From this point onward, the tables that we create will be created inside the school database.
Step 4: Create the Student Marks Table
Now let's create a table where we can store the students' information and marks.
We will call this table marks.
Before creating the table, let's decide what information we want to store.
We need the student's roll number, name, marks in Hindi, Maths, Science, and English.
Apart from these subjects, we also need two more columns.
One will be Total, where we will calculate the total marks.
The second will be Average, where we will calculate the student's percentage.
So, our table will have eight columns in total.
Now write the following command:
CREATE TABLE marks (
roll_no INT,
name VARCHAR(50),
hindi INT,
maths INT,
science INT,
english INT,
total INT,
average DECIMAL(5,2)
);
Let's understand this command.
The first column is roll_no.
We are using INT because the roll number will contain a number.
The second column is name.
For the name, we are using VARCHAR(50), which means we can store text with a maximum length of 50 characters.
Next, we have Hindi, Maths, Science, and English.
All of these columns are INT because they will contain numerical marks.
After that, we have the total column.
This column will be used for the total marks.
Finally, we have the average column.
We are using DECIMAL(5,2) here because the average can contain decimal values such as 72.25 or 85.50.
Now run the query.
Our marks table has been created successfully.
We will call this table marks.
Before creating the table, let's decide what information we want to store.
We need the student's roll number, name, marks in Hindi, Maths, Science, and English.
Apart from these subjects, we also need two more columns.
One will be Total, where we will calculate the total marks.
The second will be Average, where we will calculate the student's percentage.
So, our table will have eight columns in total.
Now write the following command:
CREATE TABLE marks (
roll_no INT,
name VARCHAR(50),
hindi INT,
maths INT,
science INT,
english INT,
total INT,
average DECIMAL(5,2)
);
Let's understand this command.
The first column is roll_no.
We are using INT because the roll number will contain a number.
The second column is name.
For the name, we are using VARCHAR(50), which means we can store text with a maximum length of 50 characters.
Next, we have Hindi, Maths, Science, and English.
All of these columns are INT because they will contain numerical marks.
After that, we have the total column.
This column will be used for the total marks.
Finally, we have the average column.
We are using DECIMAL(5,2) here because the average can contain decimal values such as 72.25 or 85.50.
Now run the query.
Our marks table has been created successfully.
Step 5: Check the Table Structure
Before entering any data, let's check whether our table has been created correctly.
For this, we use the DESCRIBE command.
Write:
DESCRIBE marks;
Run the query.
Now MySQL will show us the complete structure of the table.
Here we can see all the columns that we created, along with their data types.
This is useful because if we accidentally create a wrong column name or data type, we can identify the problem here.
For this, we use the DESCRIBE command.
Write:
DESCRIBE marks;
Run the query.
Now MySQL will show us the complete structure of the table.
Here we can see all the columns that we created, along with their data types.
This is useful because if we accidentally create a wrong column name or data type, we can identify the problem here.
Step 6: Insert Student Data
Now our table is ready, so the next step is to enter student records.
We could insert each student one by one, but MySQL also allows us to insert multiple records using a single INSERT query.
Let's enter five students.
Write:
INSERT INTO marks
(roll_no, name, hindi, maths, science, english, total, average)
VALUES
(1, 'Raman', 78, 89, 66, 56, 0, 0),
(2, 'Manish', 85, 76, 90, 80, 0, 0),
(3, 'Rahul', 99, 88, 75, 85, 0, 0),
(4, 'Aman', 70, 82, 65, 78, 0, 0),
(5, 'Rohit', 90, 95, 85, 88, 0, 0);
Let's understand what is happening here.
First, we mention the table name, which is marks.
Then we mention all the column names in the same order in which we are going to enter the data.
After VALUES, we enter the information for each student.
For example, the first record belongs to Raman.
His roll number is 1.
His Hindi marks are 78.
His Maths marks are 89.
His Science marks are 66.
And his English marks are 56.
At the end, we have entered zero for Total and zero for Average.
We are doing this because we will calculate these values later using formulas.
The same thing has been done for the other four students.
Notice one important thing here.
When inserting multiple records, we separate each student's record using a comma.
Only the final record ends with a semicolon.
Now run the query.
Our five student records should now be inserted into the table.
We could insert each student one by one, but MySQL also allows us to insert multiple records using a single INSERT query.
Let's enter five students.
Write:
INSERT INTO marks
(roll_no, name, hindi, maths, science, english, total, average)
VALUES
(1, 'Raman', 78, 89, 66, 56, 0, 0),
(2, 'Manish', 85, 76, 90, 80, 0, 0),
(3, 'Rahul', 99, 88, 75, 85, 0, 0),
(4, 'Aman', 70, 82, 65, 78, 0, 0),
(5, 'Rohit', 90, 95, 85, 88, 0, 0);
Let's understand what is happening here.
First, we mention the table name, which is marks.
Then we mention all the column names in the same order in which we are going to enter the data.
After VALUES, we enter the information for each student.
For example, the first record belongs to Raman.
His roll number is 1.
His Hindi marks are 78.
His Maths marks are 89.
His Science marks are 66.
And his English marks are 56.
At the end, we have entered zero for Total and zero for Average.
We are doing this because we will calculate these values later using formulas.
The same thing has been done for the other four students.
Notice one important thing here.
When inserting multiple records, we separate each student's record using a comma.
Only the final record ends with a semicolon.
Now run the query.
Our five student records should now be inserted into the table.
Step 7: Display the Student Data
Now let's check whether our records have been inserted successfully.
Write:
SELECT * FROM marks;
Run the query.
Now all five student records will appear on the screen.
We can see the roll number, name, marks for each subject, Total, and Average.
At the moment, Total and Average are showing zero.
That is completely fine because we have not applied our formulas yet.
Now let's calculate them.
Write:
SELECT * FROM marks;
Run the query.
Now all five student records will appear on the screen.
We can see the roll number, name, marks for each subject, Total, and Average.
At the moment, Total and Average are showing zero.
That is completely fine because we have not applied our formulas yet.
Now let's calculate them.
Step 8: Calculate the Total Marks
First, we will calculate the total marks.
We have four subjects:
Hindi, Maths, Science, and English.
So, to calculate the total, we simply need to add the marks of these four subjects.
Write:
SELECT roll_no, name, hindi, maths, science, english,
(hindi + maths + science + english) AS total
FROM marks;
Now run the query.
Let's understand this formula.
Inside the brackets, we are adding:
Hindi + Maths + Science + English.
The AS total part gives this calculated value the name total.
For example, let's take Raman's marks.
Hindi is 78.
Maths is 89.
Science is 66.
English is 56.
So the calculation will be:
78 + 89 + 66 + 56 = 289
Therefore, Raman's total marks are 289.
MySQL will perform the same calculation automatically for every student in our table.
We have four subjects:
Hindi, Maths, Science, and English.
So, to calculate the total, we simply need to add the marks of these four subjects.
Write:
SELECT roll_no, name, hindi, maths, science, english,
(hindi + maths + science + english) AS total
FROM marks;
Now run the query.
Let's understand this formula.
Inside the brackets, we are adding:
Hindi + Maths + Science + English.
The AS total part gives this calculated value the name total.
For example, let's take Raman's marks.
Hindi is 78.
Maths is 89.
Science is 66.
English is 56.
So the calculation will be:
78 + 89 + 66 + 56 = 289
Therefore, Raman's total marks are 289.
MySQL will perform the same calculation automatically for every student in our table.
Step 9: Calculate the Average Percentage
Now that we have calculated the total, let's calculate the average percentage.
Each subject has a maximum of 100 marks.
We have four subjects.
So the maximum possible marks are:
100 + 100 + 100 + 100 = 400.
To calculate the percentage, we use this formula:
Total Marks × 100 ÷ Maximum Marks
In our case, the maximum marks are 400.
So our formula becomes:
Total Marks × 100 ÷ 400
Now write the following query:
SELECT roll_no, name, hindi, maths, science, english,
(hindi + maths + science + english) AS total,
((hindi + maths + science + english) * 100 / 400) AS average
FROM marks;
Run the query.
Now we will get both Total and Average in the result.
Let's take Raman's example again.
His total marks are 289.
So we calculate:
289 × 100 ÷ 400
The result is 72.25.
Therefore, Raman's percentage is 72.25%.
MySQL will automatically perform this calculation for all the students.
Each subject has a maximum of 100 marks.
We have four subjects.
So the maximum possible marks are:
100 + 100 + 100 + 100 = 400.
To calculate the percentage, we use this formula:
Total Marks × 100 ÷ Maximum Marks
In our case, the maximum marks are 400.
So our formula becomes:
Total Marks × 100 ÷ 400
Now write the following query:
SELECT roll_no, name, hindi, maths, science, english,
(hindi + maths + science + english) AS total,
((hindi + maths + science + english) * 100 / 400) AS average
FROM marks;
Run the query.
Now we will get both Total and Average in the result.
Let's take Raman's example again.
His total marks are 289.
So we calculate:
289 × 100 ÷ 400
The result is 72.25.
Therefore, Raman's percentage is 72.25%.
MySQL will automatically perform this calculation for all the students.
Step 10: Understand Why We Use 400
Now let's understand one important point about the number 400.
We are using 400 because we have four subjects, and every subject has a maximum of 100 marks.
So:
4 subjects × 100 marks = 400 marks.
If you have five subjects, you will use 500 instead of 400.
If you have six subjects, you will use 600.
For example, with five subjects, the formula would be:
Total Marks × 100 ÷ 500
So whenever you add or remove subjects, remember to change the maximum marks in your formula.
We are using 400 because we have four subjects, and every subject has a maximum of 100 marks.
So:
4 subjects × 100 marks = 400 marks.
If you have five subjects, you will use 500 instead of 400.
If you have six subjects, you will use 600.
For example, with five subjects, the formula would be:
Total Marks × 100 ÷ 500
So whenever you add or remove subjects, remember to change the maximum marks in your formula.
Step 11: Display the Complete Marksheet
Now let's create one final query that displays everything together.
Write:
SELECT
roll_no,
name,
hindi,
maths,
science,
english,
(hindi + maths + science + english) AS total,
((hindi + maths + science + english) * 100 / 400) AS average
FROM marks;
Now run the query.
Here we can see the complete marksheet.
We have the Roll Number, Student Name, Hindi Marks, Maths Marks, Science Marks, English Marks, Total Marks, and Average Percentage.
The biggest advantage here is that we don't have to manually calculate the total or percentage for every student.
MySQL performs the calculation automatically using the formula.
Write:
SELECT
roll_no,
name,
hindi,
maths,
science,
english,
(hindi + maths + science + english) AS total,
((hindi + maths + science + english) * 100 / 400) AS average
FROM marks;
Now run the query.
Here we can see the complete marksheet.
We have the Roll Number, Student Name, Hindi Marks, Maths Marks, Science Marks, English Marks, Total Marks, and Average Percentage.
The biggest advantage here is that we don't have to manually calculate the total or percentage for every student.
MySQL performs the calculation automatically using the formula.
Step 12: Understand How MySQL Performs the Calculation
Let's understand the most important part once again.
This formula:
hindi + maths + science + english
adds the marks of all four subjects.
Then we use:
(hindi + maths + science + english) * 100 / 400
This converts the total marks into a percentage.
For example, if a student gets 320 marks out of 400:
320 × 100 ÷ 400 = 80
So the student's percentage will be 80%.
This is how we can use mathematical operations directly inside a SQL SELECT statement.
This formula:
hindi + maths + science + english
adds the marks of all four subjects.
Then we use:
(hindi + maths + science + english) * 100 / 400
This converts the total marks into a percentage.
For example, if a student gets 320 marks out of 400:
320 × 100 ÷ 400 = 80
So the student's percentage will be 80%.
This is how we can use mathematical operations directly inside a SQL SELECT statement.
Step 13: Important Point About the Total and Average Columns
There is one thing you should understand.
In our table, we created Total and Average columns and initially inserted zero into them.
However, in our SELECT query, we are calculating Total and Average dynamically.
That means the values shown in the result are calculated when the query runs.
We are not permanently changing the zero values stored in the table.
For learning how formulas work in MySQL, this method is simple and useful.
In our table, we created Total and Average columns and initially inserted zero into them.
However, in our SELECT query, we are calculating Total and Average dynamically.
That means the values shown in the result are calculated when the query runs.
We are not permanently changing the zero values stored in the table.
For learning how formulas work in MySQL, this method is simple and useful.
Step 14: Final Result
After running the final query, we will have a marksheet showing all the student information along with the calculated Total and Average.
For example, the result will look something like this:
Roll Number | Name | Hindi | Maths | Science | English | Total | Average
1 | Raman | 78 | 89 | 66 | 56 | 289 | 72.25
The same calculation will be performed for all the other students.
For example, the result will look something like this:
Roll Number | Name | Hindi | Maths | Science | English | Total | Average
1 | Raman | 78 | 89 | 66 | 56 | 289 | 72.25
The same calculation will be performed for all the other students.