Pages

Sunday, September 12, 2010

Top 5 SQL Queries

Beginning SQL Queries: From Novice to Professional   SQL Queries Joes 2 Pros: SQL Query Techniques For Microsoft SQL Server 2008, Volume 2


Remove duplicates
DELETE from emp WHERE rowid NOTIN ( SELECT max(rowid) from emp GROUPBY empno);


Display only duplicated
SELECT deptno from emp GROUPBY deptno HAVING count(*)>1


Top 5 salaries
SELECT empno, ename, sal from (SELECT * from emp ORDERBY sal DESC) WHERE rowno<6


Only 5th row
SELECT * from emp a WHERE 5=(SELECT count (rowid) from emp b WHERE a.rowid>=b.rowid);


5th max sal
SELECT distinct from emp a WHERE 5= (SELECT count (DISTINCT sal) from emp b WHERE b.sal>=a.sal;

No comments:

Post a Comment