标签:exist mysql desc contain view person eve anything duplicate
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)
标签:exist mysql desc contain view person eve anything duplicate
原文地址:http://www.cnblogs.com/coskaka/p/7137288.html