I'm new to SQL. There is a request that I have been fighting for a day, and I don’t understand how to approach it, although, probably, it’s not that difficult.
The bottom line:
There are tables:
- companies (id, name),
- customers (id, name),
- projects (id, name, cost),
as well as companies_projects (company_id, project_id) and customers_projects (customer_id, project_id). Communication ManyToMany .
In addition, there are more tables, but they do not participate in this query.
It is necessary to find a client for each company that brings this company the lowest profit. That is, it turns out, the sum of cost projects of this client in the company is the smallest among the other clients who have projects in this company.
Print, respectively, companies.name, customers.name.
I would be grateful for your help, because I myself do not even understand in which direction to move.