- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to order or choose rows in MySQL GROUP BY clause?
Let us first create a table −
mysql> create table DemoTable1572 -> ( -> StudentId int, -> StudentMarks int, -> StudentName varchar(20) -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1572 values(1,79,'Sam'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1572 values(2,89,'Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1572 values(3,98,'David'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1572 values(4,79,'Bob'); Query OK, 1 row affected (0.10 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1572;
This will produce the following output −
+-----------+--------------+-------------+ | StudentId | StudentMarks | StudentName | +-----------+--------------+-------------+ | 1 | 79 | Sam | | 2 | 89 | Chris | | 3 | 98 | David | | 4 | 79 | Bob | +-----------+--------------+-------------+ 4 rows in set (0.00 sec)
Here is the query to order or choose rows in MySQL GROUP BY clause −
mysql> select * from DemoTable1572 -> where StudentId in (select min(StudentId) from DemoTable1572 group by StudentMarks);
This will produce the following output −
+-----------+--------------+-------------+ | StudentId | StudentMarks | StudentName | +-----------+--------------+-------------+ | 1 | 79 | Sam | | 2 | 89 | Chris | | 3 | 98 | David | +-----------+--------------+-------------+ 3 rows in set (0.10 sec)
- Related Articles
- How to select MySQL rows in the order of IN clause?
- How can group functions be used in ORDER BY clause?
- How Can MySQL GROUP BY clause behave like DISTINCT clause?
- How can I use RAND() function in an ORDER BY clause to shuffle MySQL set of rows?
- How to use union and order by clause in MySQL?
- How to order MySQL rows by multiple columns?
- Listing all rows by group with MySQL GROUP BY?
- GROUP BY the number of rows returned by GROUP BY in MySQL?
- Get rows with GROUP BY in MySQL?
- How can we create a MySQL view with GROUP BY clause?
- How to ORDER BY FIELD with GROUP BY in a single MySQL query?
- How to get MySQL query result in same order as given by IN clause?
- How can we create the MySQL view with ORDER BY clause?
- What is the use of ORDER BY clause in MySQL?
- Can we use MySQL GROUP BY clause with multiple columns like MySQL DISTINCT clause is used?

Advertisements