MySQL SELECT DISTINCT, COUNT, ROWS EXAMPLE

mysql distinct multiple columns, distinct count, distinct column, distinct two columns, group by, distinct vs group by, select distinct on one column with multiple columns returned, sql select unique values from multiple columns Examples

Introduction to MySQL DISTINCT clause

SELECT DISTINCT
    select_list
FROM
    table_name;

MySQL DISTINCT examples

SELECT 
    membersirname
FROM
    members
ORDER BY 
    membersirname;

This location ment uses the DISTINCT clause to select unique last names from the members table: SQL Unique Count

SELECT 
    DISTINCT membersirname
FROM
    members
ORDER BY 
    membersirname;

MySQL DISTINCT and NULL values

SELECT DISTINCT location
FROM members;

MySQL DISTINCT with multiple columns

SELECT DISTINCT
    location, area
FROM
    members
WHERE
    location IS NOT NULL
ORDER BY 
    location, 
    area;

Without the DISTINCT clause, you will get the duplicate combination of location and area as follows:

SELECT 
    location, area
FROM
    members
WHERE
    location IS NOT NULL
ORDER BY 
    location , 
    area;

DISTINCT clause vs. GROUP BY clause

SELECT 
    location
FROM
    members
GROUP BY location; 

You can achieve a similar result by using the DISTINCT clause:

SELECT DISTINCT
    location
FROM
    members;
SELECT DISTINCT
    location
FROM
    members
ORDER BY 
    location;

DISTINCT and aggregate functions

SELECT 
    COUNT(DISTINCT location)
FROM
    members
WHERE
    place = 'INDIA';

DISTINCT with LIMIT clause

SELECT DISTINCT
    location
FROM
    members
WHERE
    location IS NOT NULL
LIMIT 5;                

I hope you get an idea about SQL SELECT DISTINCT, COUNT, ROWS.
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.

Leave a Comment