For this, use MAX() along with GROUP BY clause. Let us first create a table −
mysql> create table DemoTable -> ( -> StudentEmailId varchar(20), -> Marks1 int, -> Marks2 int -> ); Query OK, 0 rows affected (0.90 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('[email protected]',45,32); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('[email protected]',32,45); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values('[email protected]',32,45); Query OK, 1 row affected (1.64 sec) mysql> insert into DemoTable values('[email protected]',45,32); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable ;
This will produce the following output −
+-----------------+--------+--------+ | StudentEmailId | Marks1 | Marks2 | +-----------------+--------+--------+ | [email protected] | 45 | 32 | | [email protected] | 32 | 45 | | [email protected] | 32 | 45 | | [email protected] | 45 | 32 | +-----------------+--------+--------+ 4 rows in set (0.00 sec)
Here is the query to fetch maximum individual marks for a student with marks1 and marks2 records −
mysql> select StudentEmailId,max(Marks1),max(Marks2) from DemoTable -> group by StudentEmailId;
This will produce the following output −
+-----------------+-------------+-------------+ | StudentEmailId | max(Marks1) | max(Marks2) | +-----------------+-------------+-------------+ | [email protected] | 45 | 45 | | [email protected] | 32 | 45 | | [email protected] | 45 | 32 | +-----------------+-------------+-------------+ 3 rows in set (0.00 sec)