Total Pageviews

Showing posts with label Advance SQL Queries. Show all posts
Showing posts with label Advance SQL Queries. Show all posts

Thursday, April 5, 2012

Advance SQL Queries

1. SELECT * FROM STUDENTS
Result:



Now:
select sname from (select Row_Number() over (partition by sname order by sname) as ROWNO,
sname from student) p
order by ROWNO,sname desc
Result:



2.List All the Tables for All Databases Using System Tables


sp_msforeachdb 'select "?" AS db, * from [?].sys.tables'


or


sp_msforeachdb 'select * from ?.sys.tables' 


3.Find Nth Highest Salary of Employee – Query to Retrieve 
the Nth Maximum value



SELECT Min(Salary) FROM emp
WHERE Salary IN
(SELECT TOP n Salary FROM emp ORDER BY Salary DESC)

Note:where 'n' stands for nth highest salary.