Introduction
Function AVG
23. AVG and DISTINCT
Functions SUM, MAX, MIN
Revision

Instruction

Yes, that's right. There is also another thing which many people don't know about: you can actually use AVG with DISTINCT to only count the average from distinctive values:

SELECT AVG(DISTINCT price)
FROM project;

Does that really have any practical application? Well, it might. Take a look at the exercise.

Exercise

Click Database on the right and take a look at the projects commissioned by the client with id = 10. Quite a lot. But, as you can see, some of these projects are very repetitive. This is because the client needs a specific document translated each time the date changes for formal reasons.

Now, we would like to find the average price this client paid, but only count the price of these repetitive tasks once. Try to find such an average for this client.

Stuck? Here's a hint!

Use AVG(DISTINCT price).

Console

Code editor

Result

TableConsole