Home › Interview Questions › Write a query to retrieve unique records based on …

Write a query to retrieve unique records based on first name using the DISTINCT function.

🟢 Easy Coding Junior level
1Times asked
Jun 2026Last seen
Jun 2026First seen

💡 Model Answer

You can use the DISTINCT keyword to eliminate duplicate first names:

sql
SELECT DISTINCT first_name
FROM employees;

If you also need other columns but want uniqueness on first_name, you can use GROUP BY:

sql
SELECT first_name, MIN(employee_id) AS sample_id
FROM employees
GROUP BY first_name;

The first query returns each unique first name once. The second query returns one row per first name, along with the smallest employee_id as a representative value. Both approaches run in O(n) time with respect to the number of rows, assuming appropriate indexes on first_name.

This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.

🎤 Get questions like this answered in real-time

Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.

Get Assisting AI — Starts at ₹500