Knowee
Questions
Features
Study Tools

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.

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

Solution

Claro, aquí tienes el paso a paso para resolver la consulta:

  1. Identificar las tablas relevantes: Tenemos dos tablas principales, Persons y Likes.

  2. Entender la relación entre las tablas: La tabla Likes tiene dos columnas personA_id y personB_id que hacen referencia a la tabla Persons. La columna personA_id indica quién le gusta a personB_id.

  3. Filtrar por color de ojos: Necesitamos encontrar personas con ojos azules en la tabla Persons.

  4. Unir las tablas: Unimos la tabla Likes con la tabla Persons para obtener los nombres de las personas que les gustan a personas con ojos azules.

  5. Eliminar duplicados: Utilizamos DISTINCT para 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 tabla Persons como p1.
  • JOIN Likes l ON p1.id = l.personA_id: Une la tabla Persons con la tabla Likes donde p1 es la persona que le gusta a alguien.
  • JOIN Persons p2 ON l.personB_id = p2.id: Une nuevamente con la tabla Persons para 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.

This problem has been solved

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

1/1

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.