I've been trying to see the average salary of each profession of a hospital (this is an exercise BTW) but I can't manage to make an average for each service separately.
My last and what I believe is the closest to the answer is:
SELECT DISTINCT profession, COUNT(salaire) as salaire_moyen
FROM personnel
LIMIT 0,5;
But it just shows this:
+------------+---------------+
| profession | salaire_moyen |
+------------+---------------+
| infirmier | 17 |
+------------+---------------+
Final goal would be to obtain this:
+---------------+---------------+
| profession | salaire moyen |
+---------------+---------------+
| secretaire | 1300.00 |
| aide soignant | 1400.00 |
| infirmier | 1505.00 |
| psychologue | 1700.00 |
| medecin | 1966.67 |
+---------------+---------------+
(Sorry BTW, except for the SQL code, the terms are in French, I don't think they're so important to understand but just in case profession means job, salaire moyen is average salary, and the rest are the names of each service (secretary, doctor, etc..)).
In SQL, you can calculate an average of a column using the AVG function:
SELECT profession, AVG(salaire) AS salaire_moyen
FROM personnel
GROUP BY profession
LIMIT 0,5;
You can think of AVG(a)
as SUM(a) / COUNT(a)