Database interview questions
- User
Database interview questions
- Assistant
- User
How do you retrieve unique values from a column in SQL?
- Assistant
To retrieve unique values from a column in SQL, you can use the
DISTINCTkeyword. TheDISTINCTkeyword eliminates duplicate records from the result set, returning only unique values.Here’s a basic syntax of how to use
DISTINCT:SELECT DISTINCT column_name FROM table_name;Example
Suppose you have a table called
Employeesand you want to retrieve a list of unique job titles from theJobTitlecolumn. You would write the following SQL query:SELECT DISTINCT JobTitle FROM Employees;Key Points:
-
Multiple Columns: If you want unique combinations of multiple columns, you can specify more than one column in the
SELECTstatement, like this:SELECT DISTINCT column1, column2 FROM table_name;
-