Doubt in an SQL query

Asked

Viewed 146 times

0

Modelo DER

Through this DER template I would like to make an sql query that returns the following information: Write a command that shows how many customers are single.

  • have you tried anything? you can post??

1 answer

1

Let’s start by analyzing your table diagram.

Given your problem, just two tables of all the informed ones are important to find what you need.

Notice that there is a table Cliente and a table Conjuge. Both relate through the column Cod_Cli, how can you tell by the yellow "key" that connects one another.

That said, we can understand that only married (or dating) customers will have a spouse, that is, they will have their code referenced in the table Conjuge.

Therefore, we have set up an SQL command that will count how many clients do not have their code referenced in the table Conjuge, thus identifying single customers:

SELECT COUNT(cliente.Cod_Cli) 
FROM Cliente cliente 
WHERE cliente.Cod_Cli NOT INT (SELECT conj.Cod_Cli FROM Conjuge conj);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.