

- 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
Get maximum age from records with similar student names in MySQL
For this, you can use GROUP BY along with aggregate function MAX(). Let us first create a table −
mysql> create table DemoTable1964 ( StudentName varchar(20), StudentAge int ); Query OK, 0 rows affected (0.00 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1964 values('Chris',23); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1964 values('David',34); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1964 values('Chris',27); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1964 values('Sam',31); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1964 values('David',32); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1964 values('David',37); Query OK, 1 row affected (0.00 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1964;
This will produce the following output −
+-------------+------------+ | StudentName | StudentAge | +-------------+------------+ | Chris | 23 | | David | 34 | | Chris | 27 | | Sam | 31 | | David | 32 | | David | 37 | +-------------+------------+ 6 rows in set (0.00 sec)
Here is the query to get maximum age:
mysql> select StudentName,max(StudentAge) from DemoTable1964 group by StudentName;
This will produce the following output −
+-------------+-----------------+ | StudentName | max(StudentAge) | +-------------+-----------------+ | Chris | 27 | | David | 37 | | Sam | 31 | +-------------+-----------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- Sum values in MySQL from similar day records
- Return similar names from different columns with distinct space allocations in MySQL?
- Fetch maximum individual marks for a student with marks1 and marks2 records in MySQL?
- Fetch similar ID records from two tables in MySQL
- How to get age from DOB in MySQL?
- Return maximum value from records in MySQL
- Display month names and year from a column with date records with MySQL
- Get the maximum mark records from a collection with documents in MongoDB
- Get the maximum mark records from a collection with documents in MongoDB query
- Querying age from DOB in MySQL?
- Delete rows with duplicate and similar content & get row with maximum number with MySQL select statement?
- How to get an age from a D.O.B field in MySQL?
- Get three records having higher value from MySQL
- Selecting all the users with maximum age values using a MySQL subquery?
- Change the column name from a MySQL table with Student record?
Advertisements