How do i resolve 'order by items must appear in the select list if select distinct is specified' in sql?

Example:

SELECT DISTINCT name FROM users ORDER BY age;

Using ORDER BY on a column that's not included in a DISTINCT SELECT statement.

Solution:

Include the column used for ordering in the SELECT statement.


SELECT DISTINCT name, age FROM users ORDER BY age;

Beginner's Guide to SQL