-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path16.SQL_INTERVIEW.sql
More file actions
31 lines (25 loc) · 843 Bytes
/
16.SQL_INTERVIEW.sql
File metadata and controls
31 lines (25 loc) · 843 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
#https://youtu.be/OX6kA3JDO_E
use world;
#How to delete duplicates from a table?
with emp_table as
(select *,
RANK() OVER(PARTITION by firstname,lastname order by employee_id desc) as rnk
from empm)
delete from emp_table where rnk>1;
select * from emp;
# to get second highest salary,How to find nth highest salary
select max(salary) from emp where
salary<
(select max(salary) from emp);
# from top
SELECT TOP 1 * FROM
(select TOP 1 * from emp order by salary desc) AS SAL_ORDER
ORDER BY SALARY;
# WITH DENSE RANK, 2rd highest salary
with salry_cte as
(SELECT * ,
DENSE_RANK() OVER(order by salary desc) as sal_order FROM emp)
select * from salry_cte where sal_order =2;
# Employee Manager Hierarchy - Self Join
SELECT e.emp_id,e.emp_name as employeeee,m.emp_name as magr_name
FROM EMPM e INNER JOIN empm m on e.manager_id=m.emp_id