The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
| Id | Name | Salary | ManagerId |
|---|---|---|---|
| 1 | Piyush | 70000 | 3 |
| 2 | Nitesh | 80000 | 4 |
| 3 | Venky | 60000 | 3 |
| 4 | Rakesh | 90000 | 4 |
Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
| Employee |
| Piyush |
SELECT e.Name as Employee
from Employee e
join Employee m
on e.ManagerId = m.Id
where e.salary > m.Salary;
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no “holes” between ranks.
| Id | Score |
|---|---|
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
For example, given the above Scores table, your query should generate the following report (order by highest score):
| score | Rank |
|---|---|
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
| Column Name | Type |
| id num | int varchar |
id is the primary key for this table.
Write an SQL query to find all numbers that appear at least three times consecutively.
Return the result table in any order.
The query result format is in the following example:
Logs table:
| Id | Score |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
Result table:
| ConsecutiveNums |
| 1 |
1 is the only number that appears consecutively for at least three times.
WITH cons_table AS(
SELECT *,
LAG(Num, 1) OVER(ORDER BY Id) AS prev1,
LEAD(Num, 1) Over(ORDER BY Id) AS next1
from Logs
)
select distinct Num as ConsecutiveNums
from cons_table
where Num = prev1 and Num = next1