PHP - MySQL min(),max() aggregate operations - GeeksforGeeks

30 Sep, 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 :
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. xampp server – It is a cross-platform which is used to store the databases locally. MySQL – It is a query language which is used to manage the databases. 
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. 
xampp server – It is a cross-platform which is used to store the databases locally. 
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 connectionif ($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.
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 pagewhile($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 pagewhile($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 minimum salary and maximum salary$sql = "SELECT  MIN(salary),MAX(salary) FROM salary";$result = $conn->query($sql);//display data on web pagewhile($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 :
ruhelaa48
mysql
PHP-MySQL
PHP
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
How to create admin login page using PHP?
PHP str_replace() Function
How to pass form variables from one page to other page in PHP ?
Create a drop-down list that options fetched from a MySQL database in PHP
Different ways for passing data to view in Laravel
PHP | shell_exec() vs exec() Function
How to generate PDF file using PHP ?
PHP | Ternary Operator
PHP | file_exists( ) Function