Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query to determine that values in a column are unique

How to write a query to just determine that the values in a column are unique?

like image 326
JackyBoi Avatar asked Oct 05 '14 05:10

JackyBoi


People also ask

How do I find unique values in a query?

The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.

What SQL clause is used to find the unique values of a column?

Description. The SQL DISTINCT clause is used to remove duplicates from the result set of a SELECT statement.

How do I find unique values for a particular field in SQL?

To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.


1 Answers

Try this:

SELECT CASE WHEN count(distinct col1)= count(col1) THEN 'column values are unique' ELSE 'column values are NOT unique' END FROM tbl_name; 

Note: This only works if 'col1' does not have the data type 'ntext' or 'text'. If you have one of these data types, use 'distinct CAST(col1 AS nvarchar(4000))' (or similar) instead of 'distinct col1'.

like image 184
Ganesh Avatar answered Sep 22 '22 07:09

Ganesh