DISTINCT keyword

Estimated reading: 1 minute 18 views

1. What does the DISTINCT keyword do in SQL?

The DISTINCT keyword removes duplicate rows from the result set and returns only unique values for the specified columns. It is used to ensure that each row in the output is distinct.


2. Write a query to find unique values from a column in a table.

To find unique values in a column, use DISTINCT in the SELECT statement.
Example:

				
					SELECT DISTINCT department_id 
FROM employees;

				
			

This returns a list of unique department IDs from the employees table.


3. Can you use DISTINCT with multiple columns? Provide an example.

Yes, DISTINCT can be used with multiple columns. In this case, it ensures that the combination of values across the specified columns is unique in the result set.
Example:

				
					SELECT DISTINCT department_id, role 
FROM employees;

				
			

Leave a Comment

Share this Doc

DISTINCT keyword

Or copy link

CONTENTS