'can any one explain how to get distinct records from a table.?
How do I get distinct records from a table?
Someone suggested this query to me:
select * from emp a
where rowid = (select max(rowid) from emp b
where a.empno=b.empno);
Is rowid is unique for each row inserted? What is the use using max(rowid)?
Why can't I use the below query?
select distinct sal, distinct empno from emp;
Solution 1:[1]
rowid is unique for each row and the proposed request will get the last record inserted for each empno
Don't repeat DISTINCT, the following SQL Request will return all distinct couple of sal, empno
select distinct sal, empno from emp;
Solution 2:[2]
You can use group by for getting distinct records.
select sal, empno from emp group by Sal,empno;
Solution 3:[3]
delete from emp a where rowid != (select max(rowid) from emp b where a.empno=b.empno);
Solution 4:[4]
Select eid, count() from employee group by eid having count()=1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Guilhem Hoffmann |
| Solution 2 | navku |
| Solution 3 | fthiella |
| Solution 4 | My Art Programming |
