- 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
MySQL query to find the average of only first three values from a column with five values
For this, you can use a subquery. Let us first create a table −
mysql> create table DemoTable ( Score int ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(80); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(55); Query OK, 1 row affected (0.33 sec) mysql> insert into DemoTable values(78); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(88); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Score | +-------+ | 80 | | 45 | | 55 | | 78 | | 88 | +-------+ 5 rows in set (0.00 sec)
Following is the query to find the average of only the first three column values. Here, we are using LIMIT 3 to find the average of only three records −
mysql> select avg(Score) from ( select Score from DemoTable limit 3) tbl;
This will produce the following output −
+------------+ | avg(Score) | +------------+ | 60.0000 | +------------+ 1 row in set (0.00 sec)
- Related Articles
- How to remove only the first word from columns values with a MySQL query?
- MySQL query to retrieve only the column values with special characters?
- Return only the first 15 characters from a column with string values in MySQL
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to get first two highest column values from a table?
- MySQL query to return the count of only NO values from corresponding column value
- MySQL query to calculate the average of values in a row?
- Set a specific value for the first three column values in MySQL?
- MySQL query to replace only the NULL values from the table?
- Find the average of column values in MySQL using aggregate function
- Find and display duplicate values only once from a column in MySQL
- Create a MySQL function and find the average of values in a column
- MySQL query to remove special characters from column values?
- MySQL query to match any of the two strings from column values
- MySQL query to replace backslash from a varchar column with preceding backslash string values

Advertisements