Zain Ali
Zain Ali

Reputation: 15953

Select distinct from another select results

I want to select distinct results from another select statement results e.g;

select distinct from(select * from table)

following is result of inner select

testing department      9998901036      GOLD    
testing department      9998901036      GOLD

I want to get distinct from above select result.

Upvotes: 7

Views: 26365

Answers (2)

Martin Smith
Martin Smith

Reputation: 452977

select distinct * 
from
(select * from table) t

Works - You just need to give your sub select a table alias.

You can also use a CTE.

;WITH t AS
(
SELECT * 
FROM table
)
SELECT DISTINCT * 
FROM t

Upvotes: 2

Jabezz
Jabezz

Reputation: 1312

From your example, you could just do

select distinct * from table

But say you had some scenario where you wanted to distinct on some other results set, you could do

select distinct column1, column2 from (select * from table) T

Note that you have to alias your inner select

Upvotes: 13

Related Questions