- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fetch maximum individual marks for a student with marks1 and marks2 records in MySQL?
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('John@gmail.com',45,32); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values('John@gmail.com',32,45); Query OK, 1 row affected (0.34 sec) mysql> insert into DemoTable values('Carol@gmail.com',32,45); Query OK, 1 row affected (1.64 sec) mysql> insert into DemoTable values('David@gmail.com',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 | +-----------------+--------+--------+ | John@gmail.com | 45 | 32 | | John@gmail.com | 32 | 45 | | Carol@gmail.com | 32 | 45 | | David@gmail.com | 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) | +-----------------+-------------+-------------+ | John@gmail.com | 45 | 45 | | Carol@gmail.com | 32 | 45 | | David@gmail.com | 45 | 32 | +-----------------+-------------+-------------+ 3 rows in set (0.00 sec)
Advertisements