、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.

分析:编写一个SQL查询从Person表中找出所有重复的邮箱地址。

解法一:(self join)

# Write your MySQL query statement below
select distinct a.Email from Person a, Person b where a.Email=b.Email and a.Id<>b.Id

一开始,写的时候没注意把distinct给漏了,导致出错:

Submission Result: Wrong AnswerMore Details

Input:{"headers": {"Person": ["Id", "Email"]}, "rows": {"Person": [[1, "paris@hilton.com"], [2, "paris@hilton.com"]]}}
Output:{"headers": ["Email"], "values": [["paris@hilton.com"], ["paris@hilton.com"]]}
Expected:{"headers": ["Email"], "values": [["paris@hilton.com"]]} 

解法二:

# Write your MySQL query statement below
Select Email
From Person
GROUP BY Email
Having count(Email)>1

二、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.

+----------+
| Employee |
+----------+
| Joe |
+----------+
分析:题意为雇员表记录了所有雇员的信息,包括他们的经理在内。每一个雇员都有一个Id,和他的经理的Id。
给定雇员表,编写一个SQL查询找出薪水大于经理的员工姓名。对于上表来说,Joe是唯一收入大于经理的员工。 使用自连接:
# Write your MySQL query statement below
select m.Name from Employee m,Employee n where m.ManagerId=n.Id and m.Salary>n.Salary;

 或:

Select emp.Name from
Employee emp inner join Employee manager
on emp.ManagerId = manager.Id
where emp.Salary > manager.Salary

 或: 

select a.name from Employee a left join Employee b on a.managerid=b.id where a.salary>b.salary

 

三、Second Highest Salary

Write a SQL query to get the second highest salary from the Employee table.

+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

For example, given the above Employee table, the second highest salary is 200. If there is no second highest salary, then the query should return null.

分析:题意为  从员工表中找到工资第二高的数据(即比最高工资少的里面工资最高的)

代码如下:

# Write your MySQL query statement below
select max(Salary) from Employee
where Salary < (select max(Salary) from Employee);

其他解法:

# Write your MySQL query statement below
SELECT Salary FROM Employee GROUP BY Salary
UNION ALL (SELECT null AS Salary)
ORDER BY Salary DESC LIMIT 1 OFFSET 1

 或:

select (select distinct Salary from Employee order by salary desc limit 1,1) as Salary;

注:LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 1);offset偏移量 

 

四、Combine Two Tables

able: Person

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.

Table: Address

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:

FirstName, LastName, City, State

分析:题意为
有两个数据表:Person表和Address表。Person(人员)表主键为PersonId,Address(地址)表主键是AddressId,通过PersonId与Person表关联。编写一个SQL查询,对于Person表中的每一个人,取出FirstName, LastName, City, State属性,无论其地址信息是否存在。
思路:Person表是主表,Address表是从表,通过Left Outer Join左外连接即可。
# Write your MySQL query statement below
select p.FirstName,p.LastName,a.City,a.State
from Person p left outer join Address a using (PersonId); (using()用于两张表的join查询,要求using()指定的列在两个表中均存在,并使用之用于join的条件。)

  等价于

# Write your MySQL query statement below
select p.FirstName,p.LastName,a.City,a.State
from Person p left outer join Address a on p.PersonId=a.PersonId

  其他解法:

SELECT FirstName, LastName, City, State FROM Person NATURAL LEFT JOIN Address;

  

 

 

  

leetcode Database2 (四)的更多相关文章

  1. LeetCode第四天

    leetcode 第四天 2018年1月4日 15.(628)Maximum Product of Three Numbers JAVA class Solution { public int max ...

  2. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  3. [LeetCode] 4Sum 四数之和

    Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...

  4. 【LeetCode】四数之和【排序,固定k1,k2,二分寻找k3和k4】

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满 ...

  5. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

  6. [LeetCode] 18. 四数之和

    题目链接:https://leetcode-cn.com/problems/4sum/ 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个 ...

  7. LeetCode 454.四数相加 II(C++)

    给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单化,所有的 A ...

  8. Leetcode 454.四数相加II

    四数相加II 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0. 为了使问题简单 ...

  9. LeetCode 18. 四数之和(4Sum)

    题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等? ...

随机推荐

  1. mysql修改数据库表权限

    ps:通常我用的是:1.“grant all on *.* to root@'%' identified by 'yourpassword';”——这个还可以顺带设置密码.2.“flush privi ...

  2. SGU 113

    113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...

  3. HDU 3790 最短路径问题(SPFA || Dijkstra )

    题目链接 题意 : 中文题不详述. 思路 :无论是SPFA还是Dijkstra都在更新最短路的那个地方直接将花费更新了就行,还有别忘了判重边,话说因为忘了判重边WA了一次. #include < ...

  4. ida GDB 远程调试

    在看雪上回答的问题,有人问在WinDbg下断KiDebugRoutine或者KdEnterDebugger函数会引发蓝屏!因为是在调试Windows的内核调试引擎,我给出的解决办法是用不依赖Windo ...

  5. 【poj3243-Clever Y】高次同余方程-拓展BabyStepGiantStep

    http://poj.org/problem?id=3243 题意:给定X,Z,K,求一个最小的Y满足XY mod Z = K. 关于拓展BSGS的详细解释我写了一篇博文:http://www.cnb ...

  6. Linux问题定位工具大放送

    我们在程序定位问题时,经常不知所错,但是在linux有很多强大的工具,只要我们合理利用,一定见奇效. 主要会遇到以下问题: 1 mem高 2 cpu高 3 io高 4 网络延迟高 vargrind:h ...

  7. ubuntu系统使用minicom终端操作说明

    http://blog.chinaunix.net/uid-22030783-id-3350834.html 在linux下,使用minicom作为串口终端工具,默认的串口设备是/dev/ttyS0, ...

  8. weblogic 安装与配置

    Weblogic 安装 从官网 下载 需要的weblogic 版本, 解压缩后得到 wls1036_generic.jar [fmw_12.1.3.0.0_wls.jar , fmw_12.2.1.1 ...

  9. Struts2笔记——文件上传

    文件上传概述 * 要想使用HTML 表单上传一个或多个文件, 必须把 HTML 表单的 enctype 属性设置为multipart/form-data,把它的 method 属性设置为post *  ...

  10. [vim]设置vim语法高亮显示和自动缩进

    1.配置文件的位置        在目录 /etc/vim下面,有个名为vimrc的文件,这是系统中公共的vim配置文件,对所有用户都有效.而在每个用户的主目录下,都可以自己建立私有的配置文件,命名为 ...