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.
Question
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.
Solution
Claro, aquí tienes el paso a paso para resolver la consulta:
-
Identificar las tablas relevantes: Tenemos dos tablas principales,
PersonsyLikes. -
Entender la relación entre las tablas: La tabla
Likestiene dos columnaspersonA_idypersonB_idque hacen referencia a la tablaPersons. La columnapersonA_idindica quién le gusta apersonB_id. -
Filtrar por color de ojos: Necesitamos encontrar personas con ojos azules en la tabla
Persons. -
Unir las tablas: Unimos la tabla
Likescon la tablaPersonspara obtener los nombres de las personas que les gustan a personas con ojos azules. -
Eliminar duplicados: Utilizamos
DISTINCTpara asegurarnos de que no haya duplicados en el resultado.
Aquí está la consulta:
SELECT DISTINCT p1.name
FROM Persons p1
JOIN Likes l ON p1.id = l.personA_id
JOIN Persons p2 ON l.personB_id = p2.id
WHERE p2.eyeColor = 'blue';
Explicación de la consulta:
SELECT DISTINCT p1.name: Selecciona los nombres únicos de las personas.FROM Persons p1: De la tablaPersonscomop1.JOIN Likes l ON p1.id = l.personA_id: Une la tablaPersonscon la tablaLikesdondep1es la persona que le gusta a alguien.JOIN Persons p2 ON l.personB_id = p2.id: Une nuevamente con la tablaPersonspara obtener los detalles de la persona que es gustada.WHERE p2.eyeColor = 'blue': Filtra para que solo se consideren personas con ojos azules.
Esta consulta te dará los nombres de todas las personas que les gusta alguien con ojos azules, eliminando cualquier duplicado.
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: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: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.
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
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.