0

I have a problem with a query for Oracle with this scenario:

Table People 
ID | Name 
1 | juan 
2 | pedro 
3 | luis 

Table Properties 
ID | nombre_inmueble | FK to Table People 
1 | house | 1 
2 | garden | 1 
3 | terrace | 1 
4 | moto | 2
5 | jet  | 2 

Table Accessories 
ID | accessories | FK Table Properties
1 | windows | 1 
2 | doors | 1 
3 | scale | 2 
4 | plants | 3 
5 | motor | 4

What I want is only the people who have Properties and that have ALL Accessories, in this case the output would be

1 | juan 

What would be the query?

1 Answer 1

1

Your query will look like this:

SELECT *
FROM People P
WHERE EXISTS(
        SELECT 1
        FROM Properties T
        WHERE T.PEOPLE= P.ID
)
AND NOT EXISTS(
        SELECT 1
        FROM Properties T
        WHERE T.PEOPLE= P.ID
        AND NOT EXISTS(
                SELECT 1
                FROM Accessories A
                WHERE A.Properties = T.ID
        )
);
Sign up to request clarification or add additional context in comments.

2 Comments

It show you some error or it produce you an wrong result?
Wrong result. Its show 2 rows: 1 | juan and 2 | pedro. And I look the query that return only 1 | juan (the only row that have Properties and every Properties has Accesories)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.