You are an SQL expert. Given the following schema: Table: people Columns: id, name, age,…
- User
You are an SQL expert. Given the following schema: Table: people Columns: id, name, age, connected_to (foreign key to people.id) Write a valid SQL query for the following question: Who are all the people connected to 'Person A'? Example SQL queries: 1. To get all people older than 30: SELECT name FROM people WHERE age > 30; 2. To find all people connected to 'Person A': SELECT name FROM people WHERE connected_to = 'Person A';
- Assistant
To find all people connected to 'Person A', we need to select all records from the
peopletable where theconnected_tofield matches theidof 'Person A'. Here's how you can write the SQL query:SELECT name FROM people WHERE connected_to = (SELECT id FROM people WHERE name = 'Person A');Explanation:
-
Inner Query (
SELECT id FROM people WHERE name = 'Person A'):- This subquery retrieves the
idof 'Person A' from thepeopletable. - It's crucial because the
connected_tocolumn storesidvalues, not names.
- This subquery retrieves the
-