1、Rank scores

  • 对上表中的成绩由高到低排序,并列出排名。当两个人获得相同分数时,取并列名次,且名词中无断档。

    1. SELECT Score,
    2. DENSE_RANK() OVER(ORDER BY Score DESC) AS 'Rank'
    3. FROM Scores;

    窗口函数(OLAP)

  • 将表以窗口为单位进行分割,并在其中进行排序的函数。

  • 语法

    1. <窗口函数> over(
    2. PARTITION BY [column name1]
    3. ORDER BY [column name2])
    • PARTITION BY
      • 设定排序对象的范围(横向)
    • ORDER BY
      • 指定排序方式(纵向)
  • 排名函数

    • RANK()

      • 按照某字段的排序结果添加排名,但它是跳跃的、间断的排名,例如两个并列第一名后,下一个是第三名。
        1. SELECT Score,rank() over (ORDER BY Score desc) as 'Rank' FROM score;
    • DENSE_RANK()

      • 当有相同的分数时,它们的排名结果是并列的,例如,1,2,2,3
        1. SELECT Score,dense_rank() over (ORDER BY Score desc) as 'Rank' FROM score;
    • ROW_NUMBER()

      • 将某字段按照顺序依次添加行号
        1. SELECT Score,row_number() over(ORDER BY Score desc) as 'Rank' FROM score
  • 聚合函数

    • SUM\AVG\COUNT\MAX\MIN
      1. SELECT SUM(score) over(ORDER BY id,c_id) AS current_sum;

2、找出连续出现三次及以上的数字

  1. SELECT DISTINCT a.Num AS ConsecutiveNums
  2. From Logs as a
  3. INNER JOIN Logs AS b ON a.Id+1 = b.Id
  4. INNER JOIN Logs AS c on a.Id+2 = c.Id
  5. WHERE a.Num = b.Num
  6. AND b.Num = c.Num;

3、Employees earning more than their Managers

  • 自连接

    1. SELECT a.Name AS 'Employee'
    2. FROM Employee AS a,Employee AS b
    3. WHERE a.ManagerId = b.Id
    4. AND a.Salary > b.Salary;

    4、Duplicate Email

  • 查询重复的邮箱

    1. SELECT Email FROM Person
    2. GROUP BY Email
    3. HAVING COUNT(Id) >1;
    • 子查询 ```sql SELECT Email FROM ( SELECT Email,COUNT(id) AS num FROM Person GROUP BY Email

)WHERE num >1;

  1. - 自连结
  2. ```sql
  3. SELECT DISTINCT a.Email
  4. FROM Person AS a
  5. JOIN Person AS b
  6. ON a.Email = b.Email AND a.Id<>b.Id;

5、Customers Who Never Order

  1. select Name as 'Customer' from Customers
  2. left join Orders
  3. on Customers.Id = Orders.CustomerId
  4. where Customers.Id is NULL;
  1. select name as 'Customer' from Customers
  2. where Id not in
  3. (select CustomerId from Orders);

6、Department Highest Salary

  1. select b.Name as 'Department', a.Name as 'Employee',a.salary as 'Salary' from Employee as a
  2. inner join Department as b on a.DepartmentId = b.Id
  3. inner join
  4. (select DepartmentId,MAX(Salary) as Salary from Employee
  5. group by SepartmentId) as c
  6. on a.DepartmentId = c.DepartmentId
  7. where a.Salary = c.Salary;
  1. SELECT b.Name AS Department, a.Name AS Employee, a.Salary AS 'Salary' FROM Employee AS a
  2. INNER JOIN Department AS b
  3. ON a.DepartmentId = b.Id
  4. WHERE a.Salary IN
  5. (SELECT MAX(Salary) FROM Employee AS c
  6. WHERE a.DepartmentId = c.DepartmentId
  7. GROUP BY c.DepartmentId);

7、Delete Duplicate Emails

  1. delete p1 from Person p1,Person p2
  2. where p1.email=p2.email and p1.id>p2.id;

8、Rising Temperature

  1. select a.id from Weather as a
  2. join Weather as b
  3. on a.recordDate = ADDDATE(b.recordDate,INTERVAL 1 DAY) -- DATE_ADD/DATE_SUB/SUBDATE
  4. WHERE a.temperature>b.temperature;

9、Game Play Analysis I

  1. select player_id,event_date as first_login from Activity
  2. group by player_id
  3. order by event_date limit 0,1;