Skip to content
Related Articles

Related Articles

Improve Article

PHP – MySQL min(),max() aggregate operations

  • Last Updated : 22 Mar, 2021

In this article, we are going to find minimum and maximum details from the table column using PHP from MySQL Xampp server. We are taking an example from employee database to find the minimum and maximum salary of the employee.

Requirements –Xampp server

Introduction :

  1. PHP – 
    It stands for Hyper Text Preprocessor which is used to create dynamic web pages. We are going to connect PHP to my SQL xampp server.
     
  2. xampp server – 
    It is a cross-platform which is used to store the databases locally.
     
  3. MySQL – 
    It is a query language which is used to manage the databases.
     

Min(), max() aggregate operations :

min() –
This is an aggregate function which is used to return the minimum value in the given sql expression. This can be used along with SELECT statement.
Syntax :



select min(column_name) as 
minimum_column_name  from table_name; 

Here, as minimum_column_name is an optional one.

max() –
This is an aggregate function which is used to return the maximum value in the given SQL expression. This can be used along with SELECT statement.
Syntax :

select max(column_name) as maximum_column_name  from table_name;

Here, as maximum_column_name is an optional one.

Approach :

  • Create database (database) and table(salary) in xampp server.
  • Write a code to insert details in a salary table using PHP.
  • PHP’s code to find the minimum and maximum salary using min() and max() functions.
  • Observe the output on a web page.

Steps to start the server and storing data :

  • Start the xampp server.

  • Create a database named database and table salary in xampp. Type “localhost/phpmyadmin” in your browser.

PHP’s code to insert records (data1.php) :



PHP




<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "database";
  
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
// Check this connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
//insert records into table
$sql  = "INSERT INTO salary VALUES ('sravan',35000,'IT');";
$sql .= "INSERT INTO salary VALUES ('sudheer',45000,'IT');";
$sql .= "INSERT INTO salary  VALUES ('radha',25000,'MCA');";
$sql .= "INSERT INTO salary  VALUES ('vani',35000,'BCA');";
  
if ($conn->multi_query($sql) === TRUE) {
  echo "data stored successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}
  
$conn->close();
?>

Output :

localhost/data1.php

Records inserted in xampp server as follows.

emp_namesalarydepartment
sravan35000IT
sudheer45000IT
radha25000MCA
vani35000BCA

 

PHP code to find minimum salary(form.php) :

PHP




<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "database";
  
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
  
//sql query to find minimum salary
$sql = "SELECT  MIN(salary) FROM salary";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
    echo "Minimum Salary :". $row['MIN(salary)'];
    echo "<br />";
      
}
  
//close the connection
  
$conn->close();
?>
</body>
</html>

Output :

localhost/form.php

PHP code to find maximum salary :
(form.php)

PHP




<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "database";
  
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
  
//sql query to find the maximum salary
$sql = "SELECT  MAX(salary) FROM salary";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
    echo "Maximum Salary :". $row['MAX(salary)'];
    echo "<br />";
      
}
  
//close the connection
  
$conn->close();
?>
</body>
</html>

Output :

localhost/form.php

PHP code to display both minimum and maximum salary :

PHP




<html>
<body>
<?php
//servername
$servername = "localhost";
//username
$username = "root";
//empty password
$password = "";
//database is the database name
$dbname = "database";
  
// Create connection by passing these connection parameters
$conn = new mysqli($servername, $username, $password, $dbname);
  
//sql query to find mimimum salary and maximum salary
$sql = "SELECT  MIN(salary),MAX(salary) FROM salary";
$result = $conn->query($sql);
//display data on web page
while($row = mysqli_fetch_array($result)){
    echo "Minimum Salary :". $row['MIN(salary)'];
    echo "<br />";
    echo "Maximum Salary :". $row['MAX(salary)'];
    echo "<br />";
      
}
  
//close the connection
  
$conn->close();
?>
</body>
</html>

Output :




My Personal Notes arrow_drop_up
Recommended Articles
Page :