
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Select aggregate function and all other columns in MySQL
Let us first create a table −
mysql> create table DemoTable1621 -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(20), -> StudentMarks int -> ); Query OK, 0 rows affected (1.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1621(StudentName,StudentMarks) values('Chris',45); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable1621(StudentName,StudentMarks) values('Bob',78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1621(StudentName,StudentMarks) values('David',89); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1621(StudentName,StudentMarks) values('Adam',87); Query OK, 1 row affected (0.20 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1621;
This will produce the following output −
+-----------+-------------+--------------+ | StudentId | StudentName | StudentMarks | +-----------+-------------+--------------+ | 1 | Chris | 45 | | 2 | Bob | 78 | | 3 | David | 89 | | 4 | Adam | 87 | +-----------+-------------+--------------+ 4 rows in set (0.00 sec)
Following is the query to select aggregate function −
mysql> select tblData.*,( select max(StudentMarks) from DemoTable1621) as MaximumMarks from DemoTable1621 tblData;
This will produce the following output −
+-----------+-------------+--------------+--------------+ | StudentId | StudentName | StudentMarks | MaximumMarks | +-----------+-------------+--------------+--------------+ | 1 | Chris | 45 | 89 | | 2 | Bob | 78 | 89 | | 3 | David | 89 | 89 | | 4 | Adam | 87 | 89 | +-----------+-------------+--------------+--------------+ 4 rows in set (0.00 sec)
- Related Articles
- Select all duplicate MySQL rows based on one or two columns?
- Call aggregate function in sort order with MySQL
- MySQL alias for SELECT * columns?
- Select multiple columns and display in a single column in MySQL?
- Select distinct values from two columns in MySQL?
- Select distinct combinations from two columns in MySQL?
- How MySQL aggregate functions can be combined with MySQL IF() function?
- How to find the maximum using aggregate and get the output with all the columns in R?
- MySQL Aggregate Function to find the number of occurrences?
- MYSQL select DISTINCT values from two columns?
- MySQL Select Statement DISTINCT for Multiple Columns?
- Find the average of column values in MySQL using aggregate function
- How to add column values in MySQL without using aggregate function?
- How do I SELECT none of the rows and columns in MySQL?
- Select multiple sums with MySQL query and display them in separate columns?

Advertisements