MySQL: Rows holding group wise maximum
Rows holding group-wise maximum
In this page, we have shown how to retrieve the rows holding group-wise maximum for a column.
In the example shown and explained in this page, we retrieved the most expensive books of each language.
Example:
Sample table: book_mast
+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+ | book_id | book_name | isbn_no | cate_id | aut_id | pub_id | dt_of_pub | pub_lang | no_page | book_price | +---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+ | BK001 | Introduction to Electrodynamics | 0000979001 | CA001 | AUT001 | P003 | 2001-05-08 | English | 201 | 85.00 | | BK002 | Understanding of Steel Construction | 0000979002 | CA002 | AUT002 | P001 | 2003-07-15 | English | 300 | 105.50 | | BK003 | Guide to Networking | 0000979003 | CA003 | AUT003 | P002 | 2002-09-10 | Hindi | 510 | 200.00 | | BK004 | Transfer of Heat and Mass | 0000979004 | CA002 | AUT004 | P004 | 2004-02-16 | English | 600 | 250.00 | | BK005 | Conceptual Physics | 0000979005 | CA001 | AUT005 | P006 | 2003-07-16 | NULL | 345 | 145.00 | | BK006 | Fundamentals of Heat | 0000979006 | CA001 | AUT006 | P005 | 2003-08-10 | German | 247 | 112.00 | | BK007 | Advanced 3d Graphics | 0000979007 | CA003 | AUT007 | P002 | 2004-02-16 | Hindi | 165 | 56.00 | | BK008 | Human Anatomy | 0000979008 | CA005 | AUT008 | P006 | 2001-05-17 | German | 88 | 50.50 | | BK009 | Mental Health Nursing | 0000979009 | CA005 | AUT009 | P007 | 2004-02-10 | English | 350 | 145.00 | | BK010 | Fundamentals of Thermodynamics | 0000979010 | CA002 | AUT010 | P007 | 2002-10-14 | English | 400 | 225.00 | ... ... ... +---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
Code:
SELECT book_name, pub_lang, book_price
FROM book_mast b1
WHERE book_price =
SELECT MAX( b2.book_price )
FROM book_mast b2
WHERE b1.pub_lang = b2.pub_lang );
Explanation:
The above MySQL statement has performed the following -
1. book_name, pub_lang, book_price are retrieved from book_mast, if
a) language of the most costly book of book_mast aliased as b2 and b1 are alike.
Sample Output:
mysql> SELECT book_name, pub_lang, book_price
-> FROM book_mast b1
-> WHERE book_price = (
-> SELECT MAX( b2.book_price )
-> FROM book_mast b2
-> WHERE b1.pub_lang = b2.pub_lang );
+----------------------------------+----------+------------+
| book_name | pub_lang | book_price |
+----------------------------------+----------+------------+
| Guide to Networking | Hindi | 200.00 |
| Transfer of Heat and Mass | English | 250.00 |
| Fundamentals of Heat | German | 112.00 |
| The Experimental Analysis of Cat | French | 95.00 |
+----------------------------------+----------+------------+
4 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.0">
<title>example-rows-holding-group-wise-maximum-for-a-column php mysql examples | w3resource</title>
<meta name="description" content="PHP MySQL PDO Example">
<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 most expensive books of each language : </h2>
<table class='table table-bordered'>
<tr>
<th>Book</th><th>Language</th><th>Price</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 book_name, pub_lang, book_price
FROM book_mast b1
WHERE book_price = (
SELECT MAX( b2.book_price )
FROM book_mast b2
WHERE b1.pub_lang = b2.pub_lang )') as $row) {
echo "<tr>";
echo "<td>" . $row['book_name'] . "</td>";
echo "<td>" . $row['pub_lang'] . "</td>";
echo "<td>" . $row['book_price'] . "</td>";
echo "</tr>";
}
?>
</tbody></table>
</div>
</div>
</div>
</body>
</html>
Online Practice Editor:
PREV : Find duplicate data in MySQL
NEXT : Date Calculation
