Aufgabe 4 mit anderem Lösungsweg (der etwas näher an der Vorlesung ist) bearbeitet.
Lösung von Aufgabe b ist 24, so habe es auch viele in Facebook geschrieben.
This was not intended to be an alternative, but for explanation on how to begin such a query. This one only returns all required friendships including those already existing.
Then better delete it completely.
You cannot use MAX() in a WHERE clause, instead use a subquery.
The ORDER BY is also unneeded, because we only select the maximum. So what to order, there is only one number (even if multiple entries with the same number).
A LEFT (OUTER) JOIN is totally sufficient here, you do not want to have one Berater NULL with about 10 customers (because some customers do not have a Berater).
Moreover, when using the altered version as expected (i.e. the one with Kunden2Berater table) there is no column Kunden.berater_id anymore. You thus have to join Kunden2Berater.
I have NOT checked the COUNT throroughly, but according to my understand you will have to count on one of the Kunden2Berater columns, because COUNT() will ignore NULL columns and this is what we want. If LEFT JOIN returned a row where there are no columns on the right side, the count shall give NULL. Yet, I have to admit that I do not understand COUNT() too well. The DISTINCT is then unneeded, because we GROUP BY berater_id and the kunden_id for each berater itself will already be unique. So please do not take the COUNT statement for granted now, it's untested. http://www.techonthenet.com/sql/count.php