Differences between UNIQUE and DISTINCT in select statements

Answers were Sorted based on User's Feedback



Differences between UNIQUE and DISTINCT in select statements..

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

Differences between UNIQUE and DISTINCT in select statements..

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

Differences between UNIQUE and DISTINCT in select statements..

Answer / chandra sekhar

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

Differences between UNIQUE and DISTINCT in select statements..

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

Differences between UNIQUE and DISTINCT in select statements..

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

Differences between UNIQUE and DISTINCT in select statements..

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

Differences between UNIQUE and DISTINCT in select statements..

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

Differences between UNIQUE and DISTINCT in select statements..

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

Differences between UNIQUE and DISTINCT in select statements..

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

Differences between UNIQUE and DISTINCT in select statements..

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

Post New Answer

More SQL PLSQL Interview Questions

how to find 5th row ?

10 Answers  


What is a database trigger?

0 Answers  


Difference between truncate, delete and drop commands?

0 Answers  


What is db journal file?

0 Answers  


What is record data type?

0 Answers  






what is the use of anchoring object? what r the difference between anchoring object & enclosing object? can any one tell me all the details?

0 Answers  


Differentiate pl/sql and sql?

0 Answers  


in table there r so many rows write a query which two rows r updated last two day befor?

3 Answers   Exilant,


what is global table

7 Answers   iFlex,


how to retrieve last tree records from table? select *from emp where rownum > (select count(*)-3 from emp); i am using this query to get last three records from table but its not giving any output, so please tell me what is the error in this query.

16 Answers  


What are pl/sql exceptions? Tell me any three.

1 Answers  


Why is a trigger used?

0 Answers  


Categories