How To Use SQL MINIMUM and MAXIMUM Functions In PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

SQL MINIMUM and MAXIMUM Functions will used to search the lowest value and highest value of a column.

min_max

SQL MINIMUM and MAXIMUM Functions.

MIN(): It returns the lowest value of the column.
MAX(): It returns the largest value of the column.

 

SQL MIN() Syntax

SELECT MIN(column_name)
FROM table_name
WHERE condition;
 

SQL MIN() Syntax

SELECT MAX(column_name)
FROM table_name
WHERE condition;
 

Let's Learn.
1.Create database and table 


Student table 
In this student table we need to identify minimum value and maximum value by using SQL statement MIN() and MAX()

StudentID StudentName Address Country DOB Section

Branch

Percentage

 1
    Liam 23 MG road
Banglore  
INDIA 12/06 A CSE  66
  2    Andrew 2415 Anthony  market str Germany 01/02 A ISE  80
 4     Ryan 18 Krishna sq.
Banglore
INDIA 26/12 B EE 67
 5    Charles 878 Eli Berguvsvägen 8 INDIA 23/11 C ECE 52


Example for SQL MIN() function.
SELECT MIN(Percentage) AS lowestPercentage FROM Student

above SQL function selecting Minimum percentage as lowest percentage from the student table

Example for SQL MAX() function.
SELECT MAX(Percentage) AS highestPercentage FROM Student
above SQL function selecting Maximimu percentage as Highest percentage from the student table

2.Complete code of SQL MIN() and MAX() Functions in PHP script.
<!DOCTYPE html>
<html lang="en">
<head>
    <title>SQL MINIMUM and MAXIMUM Functions.</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>SQL MIN() and MAX() Function</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");

        if ($connect->connect_error) {
            die("Connection failed: " . $connect->connect_error);
        }
        $sql_1 = "SELECT MIN(Percentage) AS lowestPercentage FROM Student";
        $sql_2= "SELECT MAX(Percentage) AS highestPercentage FROM Student";

        if($result_1 = mysqli_query($connect, $sql_1)){
            if(mysqli_num_rows($result_1) > 0){
                echo "<table class='table'>
                    <tr>
                    <th>lowestPercentage</th>
                    </tr>";
                while($row = $result_1->fetch_assoc()){
                    echo "<tr>
                  <td>" . $row['lowestPercentage'] . "</td></tr>";
                }
                echo "</table>";
            } else{
                echo "No records found.";
            }
        }

        echo"<br>";
        if($result_2 = mysqli_query($connect, $sql_2)){
            if(mysqli_num_rows($result_2) > 0){
                echo "<table class='table'>
                    <tr>
                    <th>highestPercentage</th>
                    </tr>
                    ";
                while($row = $result_2->fetch_assoc()){
                    echo "<tr>
                  <td>" . $row['highestPercentage'] . "</td>
                  
                  </tr>";
                }
                echo "</table>";
            } else{
                echo "No records found.";
            }
        }

        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post