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.
Question
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.
Solution
To solve this task, you would need to use SQL (Structured Query Language). Here is a step-by-step guide on how to write the query:
-
First, you need to join the
Personstable with theKnowstable twice, once forpersonA_idand once forpersonB_id. This will allow you to access the age of the people thatpersonAknows. -
Then, you need to filter the results to only include rows where the person that
personAknows is older than 60. -
After that, you need to group the results by
personA_idand count the number of people that each person knows. -
Finally, you need to filter the results again to only include rows where the count is exactly 2.
Here is the SQL query that accomplishes this:
SELECT pA.name
FROM Persons pA
JOIN Knows k ON pA.id = k.personA_id
JOIN Persons pB ON k.personB_id = pB.id
WHERE pB.age > 60
GROUP BY pA.id
HAVING COUNT(*) = 2
This query will return the names of all people that know exactly 2 people who are older than 60.
Similar Questions
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.
This task concerns the following tables:PersonsidnameaddressageeyeColorgenderWrite a query that returns the name of all persons that are at least as old as everybody else.TipRecall the logic recap: FOR ALL X can be expressed by NOT EXISTS NOT X
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.
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.
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.