Let's say that we have clients and providers. A client can have multiple providers (like the internet, phone, TV etc) and I would like to find clients' names who have multiple providers.
create table clients
(
client_id char(8) not null,
client_name varchar(80) not null,
contract char(1) not null,
primary key (client_id)
)
create table client_provider
(
provider_id char(11) not null,
client_id char(8) not null,
primary key (provider_id, client_id),
foreign key (provder_id) references providers ON DELETE CASCADE,
foreign key (client_id) references clients ON DELETE CASCADE
);
Therefore, even without knowing anything about providers, we can know clients with multiple providers by the following relational algebra (just started learning, please correct me if I am wrong):
π client_name (
[ σ client_provider2.provider_id ≠ client_provider.provider_id ∧ client_provider2.client_id = client_provider.client_id (ρ client_provider2 (client_provider) ⨯ client_provider))
⨝ clients]
what I have tried so far (returning "not a GROUP BY expression" in line 1):
SQL> select c.client_name
2 from clients c
3 inner join client_provider cp on c.client_id = cp.client_id
4 group by cp.client_id
5 having count(*) > 1;