This task concerns the following tables:PersonsidnameaddressageeyeColorgenderLikesidpersonA_id → PersonspersonB_id → PersonsKnowsidpersonA_id → PersonspersonB_id → PersonsThe semantics of the Likes table is that personA likes personB.The semantics of the Knows table is that personA knows personB.Write a query that returns the name of all persons that like everyone they know.
Question
This task concerns the following tables:PersonsidnameaddressageeyeColorgenderLikesidpersonA_id → PersonspersonB_id → PersonsKnowsidpersonA_id → PersonspersonB_id → PersonsThe semantics of the Likes table is that personA likes personB.The semantics of the Knows table is that personA knows personB.Write a query that returns the name of all persons that like everyone they know.
Solution 1
To solve this task, we need to write a SQL query. The query should return the names of all persons who like everyone they know. This means that for every record in the 'Knows' table for a person, there should be a corresponding record in the 'Likes' table. Here is a step-by-step guide on how to write this query:
-
First, we need to join the 'Person', 'Likes', and 'Knows' tables on the appropriate columns. In this case, we join on 'personA_id' because the semantics of both the 'Likes' and 'Knows' tables is that 'personA' likes or knows 'personB'.
-
Next, we group the results by the name of the person (Person.name) because we want to return the names of persons.
-
Then, we count the number of unique persons that each person knows and likes.
-
Finally, we filter the results to only include persons where the count of unique persons they know is equal to the count of unique persons they like.
Here is the SQL query:
SELECT P.name
FROM Persons P
WHERE NOT EXISTS (
SELECT K.personB_id
FROM Knows K
WHERE K.personA_id = P.id
AND NOT EXISTS (
SELECT L.personB_id
FROM Likes L
WHERE L.personA_id = P.id
AND L.personB_id = K.personB_id
)
)
This query works by first selecting all persons. Then, for each person, it checks if there is someone they know who they do not like. If such a person exists, the outer query does not return the person. If no such person exists (i.e., they like everyone they know), the outer query returns the person.
Solution 2
To solve this task, we need to write a SQL query. The query should return the names of all persons who like everyone they know. This means that for every record in the 'Knows' table for a person, there should be a corresponding record in the 'Likes' table. Here is a step-by-step guide on how to write this query:
-
First, we need to join the 'Person', 'Likes' and 'Knows' tables. We join 'Person' with 'Likes' on 'id' and 'personA_id' and 'Person' with 'Knows' on 'id' and 'personA_id'. This will give us a table where each row contains information about a person, who they like and who they know.
-
Next, we group the results by person's name. This is because we want to check the condition for each person.
-
Then, we count the number of unique persons they know and the number of unique persons they like.
-
Finally, we filter the results to only include persons where the count of unique persons they know is equal to the count of unique persons they like.
Here is the SQL query:
SELECT P.name
FROM Persons P
JOIN Likes L ON P.id = L.personA_id
JOIN Knows K ON P.id = K.personA_id
GROUP BY P.name
HAVING COUNT(DISTINCT K.personB_id) = COUNT(DISTINCT L.personB_id)
This query will return the names of all persons who like everyone they know.
Similar Questions
Persons who like someone with blue eyesThis task concerns the following tables:PersonsidnameaddressageeyeColorgenderLikesidpersonA_id → PersonspersonB_id → PersonsThe semantics of the Likes table is that personA likes personB.Write a query that returns the name of all persons that like someone with blue eyes. Eliminate duplicates from your query result.
This task concerns the following tables:PersonsidnameaddressageeyeColorgenderLikesidpersonA_id → PersonspersonB_id → PersonsThe semantics of the Likes table is that personA likes personB.Use a LEFT JOIN to write a query that returns the name of all people that do not like anyone.ImportantYou can test whether an attribute is null by attribute IS NULL
This task concerns the following tables:PersonsidnameaddressageeyeColorgenderKnowsidpersonA_id → PersonspersonB_id → PersonsThe semantics of the Knows table is that personA knows personB.Write a query that returns the name of all people that know precisely 2 people that are older than 60.
This task concerns the following tables:PersonsidnameaddressageeyeColorgenderKnowsidpersonA_id → PersonspersonB_id → PersonsThe semantics of the Knows table is that the person referenced by personA_id knows the person referenced by personB_id.TakesClassesidperson_id → Personsclass_id → ClassesThe semantics of the TakesClasses table is that person referenced by person_id takes the class referenced by class_id.Use NOT IN to write a query that returns the name of all persons that do not know anyone who takes classes.
This task concerns the following tables:PersonsidnameaddressageeyeColorgenderSportTogetheridsportpersonA_id → PersonspersonB_id → PersonsThe semantics of the Sport table is that personA does sport with personB.CautionNote that the table does not contain redundancies. The table may contain a row (id, sport, personA, personB) without containing the symmetric row (id, sport, personB, personA). Nethertheless, the relation is to be understood as symmetric: if personA does sport with personB, then of course personB also does sport with personA! You need to take this into account in your query!Write a query that returns a table with columns: name and rugby. The table should contain the names of all people and the columns rugby there should contain "Yes" or "No" depending on whether this person plays rugby or not.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.