Posted inMysql / Laravel / Mysqli / php / Programming

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.

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype