SQL SELECT DISTINCT Statement

in this tutorial we learn to all about subquery Examples like as a SQL SELECT DISTINCT, COUNT and other aggregates, SQL Count Distinct Values, SQL COUNT( ) with All, SELECT AVG DISTINCT statement, SELECT MAX DISTINCT statement, SQL SELECT DISTINCT on multiple columns or many more.

Also Read: Nested Queries Subqueries in SQL Example

How do I return unique values in SQL?

SELECT DISTINCT returns only distinct (i.e. different) values.
The DISTINCT keyword eliminates duplicate records from the results.

Read Also:  Very Simple PHP Script to Upload Image or file from an URL

DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.
It operates on a single column. DISTINCT for multiple columns is not supported.

The SQL SELECT DISTINCT syntax

The general syntax is

SELECT DISTINCT column-name
  FROM table-name

Can be used with COUNT and other aggregates

SELECT COUNT (DISTINCT column-name)
  FROM table-name

SQL SELECT Examples

Question: List all unique distributor countries in alphabetical order.

SELECT DISTINCT Country
  FROM Distributor
ORDER BY COUNTRY

Question: List the number of unique distributor countries

SELECT COUNT (DISTINCT Country)
  FROM Distributor

SQL Select Distinct Syntax

SELECT DISTINCT column FROM table_name;

SQL Count Distinct Values

SELECT Count(*) As DistinctGender FROM (SELECT DISTINCT CutomerGender FROM Buyer);

SQL Distinct Values with Count

SELECT DISTINCT BuyerGender, Count(*) as Occurs FROM Buyer GROUP BY BuyerGender;

SQL COUNT( ) with All

SELECT COUNT( ALL grade )
FROM buyer;

SELECT AVG DISTINCT statement

SELECT AVG(DISTINCT WorkId)      
FROM WorkerDetail      
WHERE WorkId= WorkId   

SELECT MAX DISTINCT statement

SELECT MAX(DISTINCT WorkId)          
FROM WorkerDetail          
WHERE WorkId= WorkId 

SQL SELECT DISTINCT on multiple columns

SELECT DISTINCT WorkName,WorkAddress,WorkCity FROM WorkerDetail Where WorkId=5       

SELECT example without DISTINCT

SELECT * FROM WorkerDetail       

I hope you get an idea about sql count distinct.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  how to write subquery in sql

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 6.2 Create Custom Helper Tutorial with Example
  2. Read Also:  sql subquery examples
  3. Read Also:  sql subquery example
  4. Read Also:  sql multiple subqueries in (select statement)
  5. Read Also:  subquery in sql
  6. Read Also:  PHP Get country city location details
  7. Read Also:  How to Get Current Date & Time in JavaScript
  8. Read Also:  Most Useful Laravel String Functions(helpers)
  9. Read Also:  Bind DropdownList with Ng-Repeat using Angular Example
  10. Read Also:  Getting Current Date Time in Vue.js Date Format