To solve this problem, we need to identify names that appear exactly once in a table containing id and name columns. Here's the step-by-step approach and solution:
Approach
- Group by Name: First, we group the records by the
namecolumn to aggregate occurrences of each name. - Count Occurrences: For each group, calculate how many times the name appears using the
COUNT()function. - Filter Unique Names: Retain only groups where the count of occurrences is exactly 1.
Solution Code
Assuming the table name is people (adjust if your table has a different name):
SELECT name
FROM people
GROUP BY name
HAVING COUNT(*) = 1;
Explanation
GROUP BY name: Groups all rows with the same name together.- *`COUNT()`**: Counts the number of rows in each group (i.e., how many times the name appears).
- *`HAVING COUNT() =1
**: Filters groups to keep only those where the name appears exactly once. This is different fromWHEREbecauseHAVINGapplies to aggregated results (after grouping), whereasWHERE` applies to individual rows (before grouping).
This query efficiently retrieves all names that occur exactly once in the table. Adjust the table name to match your actual table name if needed.


作者声明:本文包含人工智能生成内容。