-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplexQueries.sql
More file actions
34 lines (26 loc) · 841 Bytes
/
ComplexQueries.sql
File metadata and controls
34 lines (26 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
--Employee Table
create table Employee (
id int not null primary key),
name varchar(50),
slary decimal(10,2));
--Query to find nth Highest Salary of Employee?
--corelated sub query
select salary from Employee e
where n=(select count(distinct salary) from employee p
where e.salary <= p.salary)
--specific to sql server
select top 1 from (select top n from Employee order by salary desc) as MyTabls
order by salary asc;
--Query to get n highest salaries
select * from Employee e
where n >= (select count(distinct salary) from employee p
where e.salary <= p.salary)
--Query to find duplicate rows
select name, id
from Employee
group by id
having count(*) > 1
-- fetch top row
select top (1) * from employee;
-- fetch last row
select top (1) * from employee order by id desc;