I have two tables in a MySQL database. The first one has a list of department names.
departments abbreviation | name -------------|-------------
I quick solution, but not the best, could be:
SELECT abbreviation, (SELECT COUNT(*) FROM courses C WHERE D.abbreviation = SUBSTRING(C.abbreviation, 0, 3)) AS c FROM departments D;