
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding average marks of students for different subjects and display only the highest average marks in MySQL
For this, you can use subquery. Let us first create a table −
mysql> create table DemoTable ( StudentName varchar(40), StudentMarks int ); Query OK, 0 rows affected (0.50 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Adam',56); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Chris',78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Adam',89); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('Robert',98); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Chris',65); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Robert',34); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+--------------+ | StudentName | StudentMarks | +-------------+--------------+ | Adam | 56 | | Chris | 78 | | Adam | 89 | | Robert | 98 | | Chris | 65 | | Robert | 34 | +-------------+--------------+ 6 rows in set (0.00 sec)
Following is the query to find average marks of students and display only the highest average marks −
mysql> select max(avSal.StudentMarks) from (select avg(StudentMarks) as `StudentMarks` from DemoTable group by StudentName) as avSal;
This will produce the following output −
+-------------------------+ | max(avSal.StudentMarks) | +-------------------------+ | 72.5000 | +-------------------------+ 1 row in set (0.09 sec)
- Related Questions & Answers
- Finding average of n top marks of each student in JavaScript
- Golang Program to read the marks of subjects and display the Grade
- Get the average of marks in MongoDB with aggregate?
- Finding the average and display the maximum average of duplicate ids?
- What are the different quote marks of MySQL?
- C program to find marks of students for boys or girls
- Group the marks of a particular student from a table and display total marks in a separate column for each student?
- How to project grouping into object in MongoDB and display only the marks field?
- Get the Average of Average in a single MySQL row?
- Different possible marks for n questions and negative marking in C++ Program
- Finding average word length of sentences - JavaScript
- Find percentage from marks in MySQL
- Display the student marks in a single column on the basis of subject in MySQL?
- Display tick marks in a JSlider with Java
- Calculate average of column values and display the result with no decimals in MySQL
Advertisements