Skip to content
Advertisement

Find all users with a unique last name

I have a table users.

I wrote a sql query that displays all users with a unique last name

tell my why query removes collapsed users name2 and name3? HAVING COUNT(DISTINCT u.first_name) = 1;

How it works? help to understand how it works

Advertisement

Answer

The query is aggregating by last name and counting the number of users.

Run this version of the query:

The third column is the number of first names. You’ll note that it is 1 for 'lastname1' and 2 for 'lastname2'.

The HAVING clause is keeping only the 1 value.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement