MYSQL Tutorial

MySQL GREATEST() function

GREATEST() function

MySQL GREATEST() function returns the greatest of the given arguments.



MySQL Version: 5.6

Example: MySQL GREATEST() function

The following MySQL statement will retrieve the greatest argument for the list of arguments.



Sample Output:

mysql> SELECT GREATEST(15,10,25);
| GREATEST(15,10,25) |
|                 25 | 
1 row in set (0.01 sec)

Example : GREATEST() function with WHERE clause

The following MySQL statement will fetch those books (along with their date of publish and number of pages) from book_mast table which has more pages than the return value of GREATEST(200,300,395), i.e. 395.


SELECT book_name,dt_of_pub,no_page
FROM book_mast
WHERE no_page>GREATEST(200,300,395);

Sample table: book_mast

Sample Output:

mysql> SELECT book_name,dt_of_pub,no_page
    -> FROM book_mast
    -> WHERE no_page>GREATEST(200,300,395);
| book_name                      | dt_of_pub  | no_page |
| Guide to Networking            | 2002-09-10 |     510 | 
| Transfer  of Heat and Mass     | 2004-02-16 |     600 | 
| Fundamentals of Thermodynamics | 2002-10-14 |     400 | 
3 rows in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example1-greatest-function- php MySQL examples | w3resource</title>
<meta name="description" content="example1-greatest-function- php MySQL examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>A list of books having 395 pages along with date of publication and number of pages:</h2>
<table class='table table-bordered'>
<th>Book</th><th>date of publication</th><th>Number of pages</th>
$db = "your_dbname";
$dbh = new PDO("MySQL:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT book_name,dt_of_pub,no_page
FROM book_mast
WHERE no_page>GREATEST(200,300,395)') as $row) {
echo "<tr>";
echo "<td>" . $row['book_name'] . "</td>";
echo "<td>" . $row['dt_of_pub'] . "</td>";
echo "<td>" . $row['no_page'] . "</td>";
echo "</tr>";

View the example in browser

JSP script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
try {
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 book_name,dt_of_pub,no_page FROM book_mast WHERE no_page>GREATEST(200,300,395)";
rs = statement.executeQuery(Data);
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>date of publication</td>
<td>Number of pages</td>
while (rs.next()) {
<%   }    %>
} catch (Exception ex) {
out.println("Can’t connect to database.");

Slideshow of MySQL Comparison Function and Operators

MySQL Comparison Function and Operators, slide presentation

New Content: Composer: Dependency manager for PHP, R Programming