Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
SQL Aggregate functions
Next article icon

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

Last Updated : 30 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 minimum 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 :


Next Article
SQL Aggregate functions
author
sravankumar_171fa07058
Improve
Article Tags :
  • SQL
  • Web Technologies
  • PHP
  • HTML
  • mysql
  • PHP-MySQL

Similar Reads

  • PHP - MYSQL : sum() operation
    Problem Statement: In this article, we are going to perform sum() aggregate operation on our database using PHP with xampp server. So we are considering the food_order database and perform database sum() operation. Requirements: xampp Introduction: PHP stands for hypertext preprocessor which is a se
    3 min read
  • PHP-My SQL avg() aggregate function
    In this article, we are going to find the average of the column in the SQL database using PHP in Xampp server. We are taking an example of a food database to find the average cost of all the items of food. Let's discuss it one by one. Requirements - Xampp server Introduction :Here, we will see the o
    4 min read
  • SQL Aggregate functions
    SQL Aggregate Functions are used to perform calculations on a set of rows and return a single value. These functions are particularly useful when we need to summarize, analyze, or group large datasets in SQL databases. Whether you're working with sales data, employee records, or product inventories,
    4 min read
  • How to Use HAVING With Aggregate Functions in SQL?
    The HAVING clause in SQL is a powerful tool used to filter grouped data based on conditions applied to aggregate functions. While the WHERE clause is used to filter rows before grouping, the HAVING clause filters aggregated results after grouping. This makes it essential for queries involving calcul
    4 min read
  • How to Use SELECT With Aggregate Functions in SQL?
    SQL aggregate functions are essential tools for summarizing and processing data. These functions help us perform calculations on a set of values to produce a single result, such as SUM, COUNT, AVG, MAX, and MIN. These functions work with the SELECT statement to process data and derive meaningful ins
    4 min read
  • PHP | mysqli_fetch_array() Function
    The mysqli_fetch_array() function is used to fetch rows from the database and store them as an array. The array can be fetched as an associative array, as a numeric array or both. Associative arrays are the arrays where the indexes are the names of the individual columns of the table. On the other h
    2 min read
  • PHP | MySQL ( Creating Table )
    What is a table? In relational databases, and flat file databases, a table is a set of data elements using a model of vertical columns and horizontal rows, the cell being the unit where a row and column intersect. A table has a specified number of columns, but can have any number of rows. Creating a
    3 min read
  • DMin and DMax Functions MS Access
    1.DMin() Function : DMin() Function in MS Access is used to determine the minimum values in a specified set of records (a domain). The DMin functions return the minimum values that satisfy the criteria. If expression identifies numeric data, the DMin functions return numeric values. If expression id
    3 min read
  • How to Group and Aggregate Data Using SQL?
    In SQL, grouping and aggregating data are essential techniques for analyzing datasets. When dealing with large volumes of data, we often need to summarize or categorize it into meaningful groups. The combination of the GROUP BY clause and aggregate functions like COUNT(), SUM(), AVG(), MIN(), and MA
    4 min read
  • PHP | MySQL ORDER BY Clause
    The ORDER BY Clause can be used along with the SELECT statement to sort the data of specific fields in an ordered way. It is used to sort the result-set in ascending or descending order. Syntax : The basic syntax of the Order By clause is - Implementation of the Order By Clause : Let us consider the
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences