w3resource
SQL exercises

SQL: Select distinct values from a table

SQL Basic Select Statement : Exercise-8 with Solution.

Write a query which will retrieve the values of salesman id of all salesmen getting orders from the customers in orders table without any repeats.

Sample table: orders


Sample Solution :

SELECT DISTINCT salesman_id
FROM orders;

Output of the Query:

salesman_id
5002
5003
5006
5001
5005
5007

Explanation :

Syntax of select distinct values from a table

Pictorial presentation :

Result of select distinct values from a table

Query Visualization:

Duration:

Query visualization of Select distinct values from a table - Duration

Rows:

Query visualization of Select distinct values from a table - Rows

Cost:

Query visualization of Select distinct values from a table - Cost

 

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a query to display salesman_id, salesman name, order date, order no, and purchase amount from orders table.
Next: Write a query to display names and city of salesman, who belongs to the city of Paris.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming