SQL Query to Find All Parents Who Have at Least One Child

This technical problem deals with writing a SQL query to find all parents who have at least one child. The example input and output are provided in the body of the problem.

Problem

Given two tables, parent and child, write a SQL query to find all the parents who have at least one child.
Example input:
parent
id
1
2
3
4
child
id
parent_id
1
1
2
2
3
3
4
4
5
4
Example output:
parent
id
1
2
3
4

Solution

This solution is in SQL. Our A.I. can create solutions in multiple languages.
by sarastevens
SELECT DISTINCT parent.id
FROM parent
JOIN child ON parent.id = child.parent_id;

/*

The above solution is optimal because it uses a JOIN to combine the two tables and then uses DISTINCT to remove any duplicate rows.

*/

A.I. Evaluation of the Solution

The candidate's solution is correct and demonstrates a good understanding of SQL. The solution is also efficient, using a JOIN to combine the two tables and then using DISTINCT to remove any duplicate rows.

Evaluated at: 2022-11-24 04:16:09