w3resource logo


MySQL Tutorial

MySQL MAX() function with having

Secondary Nav

MAX() function with having

MySQL MAX() function retrieves the maximum value from an expression which has undergone a grouping operation by GROUP BY clause and filtered using HAVING clause followed by some condition.

Example :

Sample table : publisher

Code :

SELECT country,MAX(no_of_branch)
FROM publisher
GROUP BY country
HAVING MAX(no_of_branch)>=8;

Explanation

The above MySQL statement will extract those countries ('country') which have eight or more branches.

Output :

mysql> SELECT country,MAX(no_of_branch)
    -> FROM publisher
    -> GROUP BY country
    -> HAVING MAX(no_of_branch)>=8;
+---------+-------------------+
| country | MAX(no_of_branch) |
+---------+-------------------+
| India   |                10 | 
| UK      |                 8 | 
| USA     |                25 | 
+---------+-------------------+
3 rows in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-MAX-with-having- php mysql examples | w3resource</title>
<meta name="description" content="example-MAX-with-having- php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>List of country, city and maximum number of branches of publishers those who have at least eight branches:</h2>
<table class='table table-bordered'>
<tr>
<th>Country</th><th>maximum no of branches</th>
</tr>
<?php
$hostname="your_hostname";
$username="your_username";
$password="your_password";
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT country,MAX(no_of_branch)
FROM publisher
GROUP BY country
HAVING MAX(no_of_branch)>=8') as $row) {
echo "<tr>";
echo "<td>" . $row['country'] . "</td>"; 
echo "<td>" . $row['MAX(no_of_branch)'] . "</td>";
echo "</tr>"; 
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>

View the example in browser

JSP script

<[email protected] contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example-max-with-having</title>
</head>
<body>
<%
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data = "SELECT country,MAX(no_of_branch) FROM publisher GROUP BY country HAVING MAX(no_of_branch)>=8";
rs = statement.executeQuery(Data);
%>
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>country</td>
<td>maximum no of branches</td>
</tr>
<%
while (rs.next()) {
%>
<TR>
<TD><%=rs.getString("country")%></TD>
<TD><%=rs.getString("MAX(no_of_branch)")%></TD>
</TR>
<%   }    %>
</table>
<%
rs.close();
statement.close();
connection.close();
} catch (Exception ex) {
out.println("Can’t connect to database.");
}
%>
</body>
</html>


Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program