Home >> MySql >> how to Find average value of top n in MySql

how to Find average value of top n in MySql

Find average value of n ignoring NULL values in a column.

Example-1

SELECT AVG(price) FROM products
 
Example-2
Sale Table
id         city                        year       amount
1         Lucknow                2020     1000.00
2         Allahabad              2021      1200.00
3        Banglore                 2020     1500.00
4        Banglore                 2020     1600.00
5        Lucknow                 2019     1800.00
6        Pune                       2018     1900.00

Solution

SELECT AVG(amount) as avg_amount FROM sale;

Output
avg_amount
4500

Example-3
Find an average city wise

Solution

SELECT city, AVG(amount) as avg_amount FROM sale GROUP BY city;

Output
City               avg_amount
Allahabad     1200.00
Banglore       1550.00
Lucknow       1400.00
Pune             1900.00

Post Your Comment

Next Questions
How to calculate an average value across rows
How to get the average of the values
How to get the average of the last X values
How to Find a minimum value for a certain column value
How to select data where a field has a min value
Select row with min value in GROUP BY
Select Rows with min value for each group
How to Find a maximum value for a certain column value
How to select data where a field has a max value
Select row with max value in GROUP BY
Select Rows with max value for each group
How to find the sum of values in a column
How do you find the sum of all the values in a column
How to find absolute value of a column
How do I get the absolute value of a column
How to find round value
How to get rounded data
How to find square root of a number
How to find the greatest value in a list
How to find the least value in a list
How To Find e raised to the power of the specified number
How to find a value extracted from a date
How to find a value extracted from a date or datetime
How to find a value extracted from an internal value

Copyright ©2022 coderraj.com. All Rights Reserved.