0% found this document useful (0 votes)
5 views3 pages

FAQ

Uploaded by

nsaranya89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

FAQ

Uploaded by

nsaranya89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

176.

Second Highest Salary


Medium
Topics
Companies
SQL Schema
Pandas Schema
Table: Employee

+-------------+------+
| Column Name | Type |
+-------------+------+
| id | int |
| salary | int |
+-------------+------+
id is the primary key (column with unique values) for this table.
Each row of this table contains information about the salary of an employee.

Write a solution to find the second highest distinct salary from the Employee
table. If there is no second highest salary, return null (return None in Pandas).

The result format is in the following example.

Example 1:

Input:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
Output:
+---------------------+
| SecondHighestSalary |
+---------------------+
| 200 |
+---------------------+
Example 2:

Input:
Employee table:
+----+--------+
| id | salary |
+----+--------+
| 1 | 100 |
+----+--------+
Output:
+---------------------+
| SecondHighestSalary |
+---------------------+
| null |
+---------------------+
select case when max(salary)IS null then null
else max(salary)
end as SecondHighestSalary from employee
where salary < (select max(salary) from employee)

Write a solution to find the nth highest salary from the Employee table. If there
is no nth highest salary, return null.

The result format is in the following example.

CREATE FUNCTION getNthHighestSalary(@N INT) RETURNS INT AS


BEGIN
RETURN (
/* Write your T-SQL query statement below. */
select salary
from
(select id,salary, rank() over (order by salary desc) as rank_emp from employee)a
where a.rank_emp=@N

)
END

Table: Scores

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| score | decimal |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table contains the score of a game. Score is a floating point
value with two decimal places.

Write a solution to find the rank of the scores. The ranking should be calculated
according to the following rules:

The scores should be ranked from the highest to the lowest.


If there is a tie between two scores, both should have the same ranking.
After a tie, the next ranking number should be the next consecutive integer value.
In other words, there should be no holes between ranks.
Return the result table ordered by score in descending order.

/* Write your T-SQL query statement below */


select
Score,
dense_rank() over(order by Score desc) as rank
from Scores

/* Write your T-SQL query statement below */


select a.name as Employee from
Employee a
join Employee b on a.managerId=b.ID
where a.salary>b.salary

/* Write your T-SQL query statement below */


--Customers with no order

/* Write your T-SQL query statement below */


select c.name as customers
from Customers c
left outer join Orders o on o.CustomerId=c.id
where o.id is null

/* Department Wise highest salary */


with cte_emp_dept (Department,Employee,Salary,rank_Salary) as
(select d.name as Department,
e.name as Employee,
e.salary as Salary,
dense_rank() over (partition by e.departmentId order by e.salary desc) as
rank_Salary
from employee e
join department d on d.id=e.DepartmentId)
select Department,Employee,Salary from cte_emp_dept where rank_Salary=1;

/* Top 3 salaries in each dept */


with cte_emp_dept (Department,Employee,Salary,rank_Salary) as
(select d.name as Department,
e.name as Employee,
e.salary as Salary,
dense_rank() over (partition by e.departmentId order by e.salary desc) as
rank_Salary
from employee e
join department d on d.id=e.DepartmentId)
select Department,Employee,Salary from cte_emp_dept where rank_Salary<=3
--group by department
order by department,rank_Salary;

/* Delete duplicate email with max iD */


delete from person where id in
(select max(id) as id_to_delete from
person
group by email
having count(email)>1);
select id,email from person;

You might also like