zoukankan      html  css  js  c++  java
  • leetcode_sql_3,181,182,183

    181. Employees Earning More Than Their Managers

    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  | Joe   | 70000  | 3         |
    | 2  | Henry | 80000  | 4         |
    | 3  | Sam   | 60000  | NULL      |
    | 4  | Max   | 90000  | NULL      |
    +----+-------+--------+-----------+
    

    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.

    # Write your MySQL query statement below
    SELECT A.Name Employee
    FROM Employee A,Employee B
    WHERE A.ManagerId=B.Id AND A.Salary >B.Salary;

    182. Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person.

    +----+---------+
    | Id | Email   |
    +----+---------+
    | 1  | a@b.com |
    | 2  | c@d.com |
    | 3  | a@b.com |
    +----+---------+
    

    For example, your query should return the following for the above table:

    +---------+
    | Email   |
    +---------+
    | a@b.com |
    +---------+
    

    Note: All emails are in lowercase.

    # Write your MySQL query statement below
    SELECT Distinct(a.Email)
    FROM Person a,Person b
    WHERE a.Id<>b.Id and a.Email=b.Email

    or

    select Email
    from Person
    group by Email
    having count(*) > 1

    183. Customers Who Never Order

    Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything.

    Table: Customers.

    +----+-------+
    | Id | Name  |
    +----+-------+
    | 1  | Joe   |
    | 2  | Henry |
    | 3  | Sam   |
    | 4  | Max   |
    +----+-------+
    

    Table: Orders.

    +----+------------+
    | Id | CustomerId |
    +----+------------+
    | 1  | 3          |
    | 2  | 1          |
    +----+------------+
    

    Using the above tables as example, return the following:

    +-----------+
    | Customers |
    +-----------+
    | Henry     |
    | Max       |
    +-----------+
    

     SELECT A.Name from Customers A LEFT JOIN Orders B on a.Id = B.CustomerId WHERE b.CustomerId is NULL;

    select c.Name from Customers c
    where (select count(*) from Orders o where o.customerId=c.id)=0

    select c.Name from Customers c
    where not exists (select * from Orders o where o.customerId=c.id)

     

     
  • 相关阅读:
    格式化日期---获取年月日升级版
    时间格式转换
    Python求两个有序数组的中位数的几种方法
    pyinstaller打包时包含资源文件
    PyQt5自定义组件之飞机水平仪
    Python获取磁盘剩余空间
    PyQt5自定义组件之信号强度
    Python字典的实现原理
    获取元素相对浏览器窗口的偏移坐标
    HTML/JavaScript实现地图以鼠标为圆心缩放和移动
  • 原文地址:https://www.cnblogs.com/coskaka/p/7137288.html
Copyright © 2011-2022 走看看