I have 3 different tables :
Client
+----+-----------+----------+
| id | firstName | lastName |
+----+-----------+----------+
| 1 | John | Doe |
| 2 | Jane | Doe |
+----+-----------+----------+
Loan
+----+--------+-----------+----------------+
| id | amount | client_id | institution_id |
+----+--------+-----------+----------------+
| 1 | 200 | 2 | 3 |
| 2 | 400 | 1 | 1 |
+----+--------+-----------+----------------+
Institution
+----+---------------+
| id | name |
+----+---------------+
| 1 | Institution A |
| 2 | Institution B |
| 3 | Institution C |
+----+---------------+
I am looking to create a list of the number of loans a client has with each institution (for every row in the institution table). Including when a client has 0 loans with an institution.
Something that looks like :
+-----------+-----------+----------+--------------------------+-----------+
| client_id | firstName | lastName | financialInstitutionName | loanCount |
+-----------+-----------+----------+--------------------------+-----------+
| 1 | John | Doe | Institution A | 1 |
| 1 | John | Doe | Institution B | 0 |
| 1 | John | Doe | Institution C | 0 |
| 2 | Jane | Doe | Institution A | 0 |
| 2 | Jane | Doe | Institution B | 0 |
| 2 | Jane | Doe | Institution C | 1 |
+-----------+-----------+----------+--------------------------+-----------+
I have tried all manners of joins, subqueries and where clauses but without success. The concept that I do not grasp is how to get a row per institution, per client (total count institution x client). I would love if that query was possible without subqueries or union joins.
Thank you for your time!