Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 310
Zain Ali Avatar asked Mar 11 '11 11:03

Zain Ali


People also ask

Can you use SELECT and SELECT distinct in SQL?

Yes, the DISTINCT clause can be applied to any valid SELECT query. It is important to note that DISTINCT will filter out all rows that are not unique in terms of all selected columns.

How do you use distinct in the middle of a selected statement?

The DISTINCT clause filters out FULL DUPLICATE ROWS. It goes right after the SELECT keyword, since it applies to the entire row, not single columns. You cannot use it in between columns.

Can we use distinct in SELECT query?

The SQL SELECT DISTINCT StatementThe 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.

Can you use SELECT distinct and GROUP BY?

Well, GROUP BY and DISTINCT have their own use. GROUP BY cannot replace DISTINCT in some situations and DISTINCT cannot take place of GROUP BY. It is as per your choice and situation how you are optimizing both of them and choosing where to use GROUP BY and DISTINCT.


1 Answers

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

like image 101
Jabezz Avatar answered Nov 24 '22 17:11

Jabezz