unique rows sql code example
Example 1: select count of distinct values sql
SELECT COUNT (DISTINCT column-name) FROM table-name
Example 2: MySQL DISTINCT
When querying data from a table, you may get duplicate rows. In order to remove these duplicate rows, you use the DISTINCT clause in the SELECT statement.
Here is the syntax of the DISTINCT clause:
SELECT DISTINCT
select_list
FROM
table_name;
Example 3: how to select unique element in sql
DISTINCT
- select distinct * from employees; ==>
retrieves any row if it has at
least a single unique column.
- select distinct first_name from employees; ==>
retrieves unique names
from table. (removes duplicates)
- select distinct count(*) from employees;
retrieve number of unique rows
if any row has at least a single unique data.
Example 4: unique element in sql
DISTINCT
- select distinct * from employees; ==> retrieves any row if it has at
least a single unique column.
- select distinct first_name from employees; ==> retrieves unique names
from table. (removes duplicates)
- select distinct count(*) from employees; retrieve number of unique rows
if any row has at least a single unique data.
Example 5: sql server select rows by distinct column
SELECT a.*
FROM emails a
INNER JOIN
(SELECT email,
MIN(id) as id
FROM emails
GROUP BY email
) AS b
ON a.email = b.email
AND a.id = b.id;