Differences between UNIQUE and DISTINCT in select statements
Answers were Sorted based on User's Feedback
Answer / rob reid
Howard Cheon is correct. They are THE SAME. There is no
difference.
The following is from the Oracle 9i SQL Reference (Note the
statement "these two keywords are synonymous" - that means
they mean the same thing):
DISTINCT | UNIQUE
Specify DISTINCT or UNIQUE if you want Oracle to return
only one copy of each set of duplicate rows selected (these
two keywords are synonymous). Duplicate rows are those with
matching values for each expression in the select list.
Restrictions on DISTINCT and UNIQUE Queries
When you specify DISTINCT or UNIQUE, the total number of
bytes in all select list expressions is limited to the size
of a data block minus some overhead. This size is specified
by the initialization parameter DB_BLOCK_SIZE.
You cannot specify DISTINCT if the select_list contains LOB
columns.
Is This Answer Correct ? | 59 Yes | 6 No |
Answer / manoj
UNIQUE(Constraint):- Can be only once for a column in a
table
DISTINCT(Clause):- Useed in select query to retrive distinct
(non repeated) data from table(s)
Is This Answer Correct ? | 114 Yes | 68 No |
the diff between unique and distinct is
unique is applied before insertion and retrival.it consists
of non duplicate values.if unique constraint is given it
does not take duplicate values.distinct is used in retrival
it gives the suppressed row(ex if two rows are same it will
show single row and non duplicate row) therefore distinct is
the combination of suppressed duplicate and non duplicate rows.
Is This Answer Correct ? | 35 Yes | 11 No |
Answer / gourvendra singh
Buddy i dont found any difference in using distinct and
unique in the select statement, as both are performing same
operation in the select statement, almost every diff you
have discussed i want to add one that is u cant use
distinct in the create whereas unique u can.
Is This Answer Correct ? | 19 Yes | 9 No |
Answer / manoj
see cautiously the major difference b/w unique and
distinct
UNIQUE :Allways take part on DATA INSERTION (In brief)
DINTINCT:Allways concern on data retrival (In brief)
Now we will take following case
when
case 1: UNIQUE KEY is defined (may b 4 one or more column)
It means this constraint will not allow us to
INSERT duplicate record/column_value(s)(i.e. exactly
same record/column_value(s) again for specified key column
(s)only)
NOW if u use DISTINCT clause in query
(select DISTINCT.from.)the will b same as simple query
(select....from...)
Bcoz data are already not repeated (bcoz of constraint)
case 2:if UNIQUE not defined
data may b repeated and DISTINCT will now useful
Have eye on Major difference
(and we never use UNIQUE CONS.. in select.....)
Is This Answer Correct ? | 40 Yes | 31 No |
Answer / cutepa1
yeah Manoj, this was analysed with your previous answer
itself but please try these statements and help me with
it's significance on the statements.
select unique(<clomun_name>) from <table_name>;
select distinct(<clomun_name>) from <table_name>;
Is This Answer Correct ? | 16 Yes | 8 No |
Answer / gvmahesh
unique is a constraint it's declare for preventing the data
duplication.we create it along with table creation.
DISTINCT is a clause it is used for retrieving the data
without duplication.but it can't prevent the data
duplication.It just hiding the duplicated data.
Is This Answer Correct ? | 11 Yes | 3 No |
Answer / karthicksharma
As said earlier UNIQUE is a constraint and can be used
during a table creation to prevent duplication of data .
-----------------------------------------------------
CREATING A TABLE USING UNIQUE CONSTRAINT:
=================================================
eg:
SQL> create table empl1 (name varchar2(15),no number(5)
unique);
Table created.
-----------------------------------------------------
So from this it is clearly implied that UNIQUE constraint
is used to prevent INSERTION AS WELL AS RETRIVAL of non-
duplicate data.
whereas DISTINCT clause can only be used for RETRIVAL of
non-duplicate data.
Whereas DISTINCT is a clause and cannot be used during a
table creation to limit the duplication of data.
-----------------------------------------------------
CREATING A TABLE USING DISTINCT CLAUSE:
===========================================
eg:
create table empl2(name varchar2(15),no number(5) distinct)
*
ERROR at line 1:
ORA-00907: missing right parenthesis
-----------------------------------------------------
Is This Answer Correct ? | 7 Yes | 0 No |
Answer / david alexander
There is some confusion here. Yes, UNIQUE is a constraint,
but the question is not about constraints, its about a
select statement. And, to answer it quite simply, its a
trick question. There is no difference. Although I always
see it as DISTINCT. Here is the quote from Oracle® Database
SQL Reference 10g Release 2 (10.2):
"Specify DISTINCT or UNIQUE if you want the database to
return only one copy of each set of duplicate rows
selected. These two keywords are synonymous. Duplicate rows
are those with matching values for each expression in the
select list."
Is This Answer Correct ? | 8 Yes | 4 No |
Answer / qman*
The two keywords are only synonymous in regards to a SELECT
statement, and also ONLY in an Oracle environment. DISTINCT
is an ANSI standard where as UNIQUE can only be used this
way in Oracle. To further the differences between the two,
UNIQUE may also be used in a table creation statement to
specify that inputs into the specified column cannot share
the same value, and if attempted will result in a
constraint violation error, I hope this clears things up.
Is This Answer Correct ? | 5 Yes | 1 No |
Explain the working of primary key?
what is the difernece between named and anonymous pl/sql blocks??
What is a function in oracle pl sql?
what is 'mysqlimport'? : Sql dba
Can we insert delete data in view?
Is subquery faster than join?
Enlist the data types that can be used in pl/sql?
overloading of stored procedure is possible in oracle?
How do I sort a table in sql?
Is it possible to include an insert statement on the same table to which the trigger is assigned?
How to rename a table?
Difference between a query and strored procedure?