<?php
$my_conn = new PDO('sqlite:my_student.sqlite3');
// Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$sql="SELECT min(mark) as m_mark FROM student ";
$result=$my_conn->query($sql);
$row = $result->fetch(PDO::FETCH_OBJ);
echo " Lowest Mark : ".$row->m_mark;
?>
Output is here
Lowest Mark : 18
We can get the details of the student who got minimum mark. Here we will use sub query to get the matching record.
$sql="SELECT * FROM `student` WHERE mark=(select min(mark) from student)";
$result=$my_conn->query($sql);
$row = $result->fetch(PDO::FETCH_OBJ);
echo " Lowest Mark : ".$row->mark;
echo " <br>ID : ".$row->id;
echo " <br>Name : ".$row->name;
echo " <br>Class : ".$row->class;
echo " <br>Sex : ".$row->sex;
Output is here
Lowest Mark : 18
ID : 19
Name : Tinny
Class : Nine
Sex : male
We can get the lowest mark of all classes by using group by query.
$my_conn = new PDO('sqlite:my_student.sqlite3');
// Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
$sql="SELECT class,min(mark) as m_mark FROM student group by class";
$step = $my_conn->prepare($sql);
$step->execute();
$step = $step->fetchAll();
echo "<table>";
foreach ($step as $row) {
echo "<tr ><td>$row[class]</td><td>$row[m_mark]</td></tr>";
}
echo "</table>";
$my_conn = null;
Output
Eight 79
Five 75
Four 55
Nine 18
Seven 55
Six 54
Three 55