It's UWAweek 47

help1402

This forum is provided to promote discussion amongst students enrolled in CITS1402 Relational Database Management Systems.

Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.

How do I ask a good question?
Displaying the 5 articles in this topic
Showing 5 of 684 articles.
Currently 125 other people reading this forum.


 UWA week 36 (2nd semester, mid-semester break) ↓
SVG not supported

Login to reply

👍?
helpful
6:45pm Fri 6th Sep, ANONYMOUS

Hi, Lab4 Q3 states "Hint use a subquery to calculate the average quantity, you will need to use HAVING also (this is because in SQL the WHERE is applied before GROUP BY, meaning it cannot reference aggregate functions such as an average)" To get the expected answer I used WHERE instead of GROUP BY + HAVING clause. Am I missing something? Cheers


SVG not supported

Login to reply

👍?
helpful
7:27pm Fri 6th Sep, Mengxi L.

"Where" filters rows before any grouping or aggregation is done,you cannot use aggregate functions (like AVG, COUNT, etc.) directly in the "WHERE" clause because the aggregation happens after the filtering is done. "Having" is used specifically to filter groups created by GROUP BY based on aggregate functions. In Lab4 Q3 Having allows filtering based on the result of an aggregate function AVG(), because HAVING clause applies after the GROUP BY. So I don' think you can use Where instead of GROUP BY + HAVING in this case. Sometimes your query with WHERE might have returned the correct results, but the proper SQL structure should involve GROUP BY and HAVING in this case.


SVG not supported

Login to reply

👍?
helpful
8:36pm Fri 6th Sep, ANONYMOUS

could you give a hint why we need to use GROUP BY ?


SVG not supported

Login to reply

👍?
helpful
9:21pm Fri 6th Sep, Mengxi L.

GROUP BY in SQL is to group rows that share a common value in one or more columns so that you can apply aggregate functions (like AVG(),COUNT(), etc.) on each group separately. Without GROUP BY, aggregate functions will operate on the entire result set,but you only want to calculate the average quantity for each product.


 UWA week 37 (2nd semester, week 7) ↓
SVG not supported

Login to reply

👍?
helpful
2:34pm Wed 11th Sep, Adam W.

A practical example could be if you had a table of student table with names, degree, and grade, you want to get the average grade for each different degree the students are taking. You would add GROUP BY degree, so it would get the AVERAGE(grade) for each distinct degree, if you left out the group by, it would just average all of the students regardless of the degree. Hopefully that helps. ANONYMOUS wrote:
> could you give a hint why we need to use GROUP BY ?

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  8:08AM Aug 25 2024
Privacy policy