Knowee
Questions
Features
Study Tools

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 persons that are more than 5 years older than everybody they know.

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 persons that are more than 5 years older than everybody they know.

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

To solve this task, you would need to write a SQL query. Here is a step-by-step guide on how to do it:

  1. First, you need to join the Persons table with the Knows table twice. The first join is on Persons.id = Knows.personA_id and the second join is on Persons.id = Knows.personB_id. This will give you a table where each row contains information about a person and someone they know.

  2. Next, you need to group the table by the personA's id, name, and age. For each group, you calculate the minimum age of the people they know.

  3. Then, you filter out the groups where the personA's age is not more than 5 years older than the minimum age of the people they know.

  4. Finally, you select the names of the remaining groups.

Here is the SQL query that does all of this:

SELECT A.name
FROM Persons A
WHERE NOT EXISTS (
    SELECT 1
    FROM Knows K
    JOIN Persons B ON K.personB_id = B.id
    WHERE K.personA_id = A.id AND A.age <= B.age + 5
)

This query works by checking for each person if there exists someone they know who is not more than 5 years younger than them. If such a person exists, they are excluded from the result.

This problem has been solved

Solution 2

To solve this task, you would need to write a SQL query. Here is a step-by-step guide on how to do it:

  1. First, you need to join the Persons table with the Knows table twice. The first join is to get the age of the person who knows (personA), and the second join is to get the age of the person who is known (personB).

  2. Then, you need to group by personA's id and name, and calculate the minimum age difference between personA and all persons they know (personB).

  3. Finally, you need to filter out the groups where the minimum age difference is less than or equal to 5.

Here is the SQL query that does all of this:

SELECT A.name
FROM Persons A
JOIN Knows K ON A.id = K.personA_id
JOIN Persons B ON K.personB_id = B.id
GROUP BY A.id, A.name
HAVING MIN(A.age - B.age) > 5

This query first joins the Persons table with the Knows table on personA_id, and then again on personB_id. It groups the result by personA_id and personA_name, and then filters out the groups where the minimum age difference between personA and personB is less than or equal to 5. The result is the names of all persons who are more than 5 years older than everybody they know.

This problem has been solved

Similar Questions

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: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: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.

Consider a table Employee with attributes Name, Age and Branch. Select the SQL query to retrieve the Age and Branch of the youngest Employee over the age of 18 for branches with atleast 10 employees. Select all that apply.

Give the names of three persons not related to you, whom you have known at least three (3) years.Provide the following info: Name, Address, phone or Email. Year acquainted*

1/3

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.