Write a SQL statement to retrieve the average number of pages per category
Let suppose SQLTable is the table name, It is having two columns and multiple rows
Columns are pages and category,
category is same for some of the pages and we have to find the average of pages category wise
So, here we will use , GROUP BY clause to make group of each category
After that , we will use aggregate function AVG to find the average of pages for each category
Below is the final query:
SELECT AVG(pages),category from SQLTable GROUP BY category;
Example:
Below is the table:
pages | category |
90 | C001 |
60 | C001 |
80 | C002 |
20 | C002 |
20 | C002 |
As per above query , below will be the output:
AVG(pages) | category |
75 | C001 |
40 | C002 |
so, by GROUP BY , category goes grouped C001 and C002
and average is found groupwise,
C001 - have 60,90 - whose average is 75
C002 - have 60,20,20 - whose average is 40
Get Answers For Free
Most questions answered within 1 hours.