SQL

select P.FirstName,P.LastName,A.City,A.State
FROM Person P left join Address A ON P.personId = A.personId;

//第二高薪水
SELECT 
    IFNULL(
        (SELECT DISTINCT Salary
FROM Employee
order by Salary DESC limit 1,1),NULL
        
    )  as 'SecondHighestSalary';


CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
    SET N := N-1;
  RETURN (
      # Write your MySQL query statement below.
      SELECT IFNULL((SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT N,1),NULL) as NthHighestSalay
  );
END


//分数排名
# Write your MySQL query statement below
SELECT Score, 
    DENSE_RANK() OVER(ORDER BY Score DESC) AS 'Rank'
FROM Scores



//连续出现三次的数字

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;


// 重复邮箱

SELECT Email FROM Person
GROUP BY Email
HAVING COUNT(Email) > 1;


#部门工资最高的员工
SELECT d.name AS 'Department', e.name AS 'Employee', e.Salary
FROM Department d join Employee e
ON d.Id = e.DepartmentId
WHERE (e.DepartmentId,e.Salary) IN
(
    SELECT DepartmentId,MAX(Salary)
    FROM Employee
    GROUP BY DepartmentId
);

 

你可能感兴趣的:(LeetCode,&&,基础算法总结)