- Score ranking
Write a SQL query to achieve score ranking.
If the two scores are the same, the two scores rank (Rank) the same. Please note that the next place after the tie should be the next consecutive integer value. In other words, there should be no "gap" between the rankings.
MySQL commonly used window functions are rank, dense_rank, row_number. The difference is that the first one will rank the same data side by side, the latter will be +n, and the second will only be +1, and the third will be purely ordered and not tied.
# Write your MySQL query statement below
select score,
dense_rank() over(order by Score desc) as 'Rank'
from Scores;
- Maximum number
Given a set of non-negative integers, rearrange their order to form the largest integer.
The core of this question is: bool cmp(string s1, string s2){s1+s2> s2+s1}
bool cmp(string a, string b)//直接相加比较
{
return a+b > b+a;
}
class Solution {
public:
string largestNumber(vector<int>& nums) {
vector<string>v;
for (int i = 0; i < nums.size(); i++)
{
string temp = to_string(nums[i]);//比用stringstream要快很多!
v.push_back(temp);
}
sort(v.begin(), v.end(), cmp);
string ret;
for (int i = 0; i < v.size(); i++)
{
ret += v[i];
if (v[0] == "0")//考虑全零的情况
break;
}
return ret;
}
};
- Numbers
that appear consecutively Write an SQL query to find all numbers that appear at least three consecutive times.
1. Use Logs and check whether it is 3 times in a row 2. We need to add the keyword DISTINCT, because if a number appears more than 3 times in a row, repeated elements will be returned.
# Write your MySQL query statement below
SELECT DISTINCT
l1.Num AS ConsecutiveNums
FROM
Logs l1,
Logs l2,
Logs l3
WHERE
l1.Id = l2.Id - 1
AND l2.Id = l3.Id - 1
AND l1.Num = l2.Num
AND l2.Num = l3.Num
;
- Income employees than managers
given the Employee table, write a SQL query that you can get the names of managers earning more than their employees. In the table above, Joe is the only employee whose income exceeds his manager.
Use join to complete
# Write your MySQL query statement below
SELECT
a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
ON a.ManagerId = b.Id
AND a.Salary > b.Salary
;
- Find duplicate email addresses
Write a SQL query to find all duplicate email addresses in the Person table.
Mainly use group and having
# Write your MySQL query statement below
select Email
from Person
group by Email
having count(Email) > 1;
- Customers who never order
Use keyword NOT IN
# Write your MySQL query statement below
select customers.name as 'Customers'
from customers
where customers.id not in
(
select customerid from orders
);
- Employees
with the highest salary in the department Write a SQL query to find the employees with the highest salary in each department. For example, according to the table given above, Max has the highest salary in the IT department, and Henry has the highest salary in the Sales department.
Use JOIN and IN statements
# Write your MySQL query statement below
SELECT
Department.name AS 'Department',
Employee.name AS 'Employee',
Salary
FROM
Employee
JOIN
Department ON Employee.DepartmentId = Department.Id
WHERE
(Employee.DepartmentId , Salary) IN
( SELECT
DepartmentId, MAX(Salary)
FROM
Employee
GROUP BY DepartmentId
)
;
- Employees
with the top three highest salaries in the department Write a SQL query to find all the employees with the top three highest salaries in each department.
# Write your MySQL query statement below
SELECT
d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary
FROM
Employee e1
JOIN
Department d ON e1.DepartmentId = d.Id
WHERE
3 > (SELECT
COUNT(DISTINCT e2.Salary)
FROM
Employee e2
WHERE
e2.Salary > e1.Salary
AND e1.DepartmentId = e2.DepartmentId
)
;