• 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)

     

     
  • 相关阅读:
    POJ3070 Fibonacci[矩阵乘法]【学习笔记】
    NOIP模拟赛20161023
    洛谷P2964 [USACO09NOV]硬币的游戏A Coin Game
    Jmeter之Bean shell使用
    jmeter 之 BeanShell PostProcessor跨线程全局变量使用
    详解JMeter函数和变量
    性能测试指标(图表)
    http协议基础(十一)http与https
    http协议进阶(六)代理
    http协议进阶(五)连接管理
  • 原文地址:https://www.cnblogs.com/coskaka/p/7137288.html
Copyright © 2020-2023  润新知