If you believed that only conventional Computer languages only have features of Functions then, you might have missed the domain here! In addition, MySQL also has predefined functions that comes handy in usage.
We will discuss the Conventional functions only here so that the Querying process becomes Smooth and you can handle most of the Cases you get.
The following functions will be discussed in the following sections of the material.
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL AVG
- SQL SUM
- SQL CONCAT
SQL COUNT Function:
Going by the order, let’s start with the ‘Count’ function. This is used to count the Number of Rows whether used along with a condition or not. It is combined with the SELECT query.
Okay, now let us find out the Number of entries in the country table by using the ‘COUNT’ function.
SELECT COUNT(*) FROM country;
Now, if we want to know the Number of Workers in Air Force table who have Serving Time more than or equal to 35.
SELECT COUNT(*) FROM air_force WHERE serving_time>=35;
Note: There is no Space in between COUNT(*), otherwise it may show you an Error.
SQL MAX Function:
As the name suggests, MAX function also gives the Maximum value from that particular Attribute.
SELECT MAX(population) FROM country;
Similarly, if we want all the attributes to be shown along with the satisfying condition for the tuple, we can use this explicit query mentioned below.
SELECT * FROM country WHERE population=(SELECT MAX(population) FROM country);
SQL MIN Function:
The ‘MIN’ function gives out the Minimum value out of the Attribute provided.
SELECT * FROM railways WHERE costperkm<100 AND avg_delay=(SELECT MIN(avg_delay) FROM railways);
SQL AVG Function:
The ‘AVG’ Function calculates the Average Value of the Attribute that you provide as an Argument.
SELECT AVG(marks) Marks FROM school;
Note: The Above Mentioned Syntax also shows how to name your own Column as “Marks” is selected here!
SQL SUM Function:
SELECT SUM(quantity) Total_Quantity FROM store;
SQL CONCAT Function:
The ‘CONCAT’ function performs the Concatenation of the Strings that are passed to the function.
SELECT CONCAT(roll,name,marks,grade) Condensed_Form FROM school;