unique value in sql code example

Example 1: select count of distinct values sql

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

Example 2: how to find 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 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: 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;

Tags:

Misc Example