、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. Application, JDBC, 数据库连接池, Session, 数据库的关系

    RT,这几个东东已经困扰我很长一段时间了... 这次争取把她们理清楚了! 参考资料: 1. 数据库连接池:http://www.cnblogs.com/shipengzhi/archive/2011/ ...

  2. Segment Tree with Lazy 分类: ACM TYPE 2014-08-29 11:28 134人阅读 评论(0) 收藏

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; stru ...

  3. fiddler 代理不成功调试

    按照 http://jingyan.baidu.com/article/03b2f78c7b6bb05ea237aed2.html 设置. 之前可以用, 现在不能用. 1. 关闭 系统防火墙 2. 换 ...

  4. safeseh+dep保护绕过

    [文章作者]       :h_one [漏洞程序名称]:mplayer.exe [漏洞类型]       :缓冲区溢出 [保护方式]       :safeseh+dep [操作平台]       ...

  5. UML 2.0(装载)

    在世界上统一建模语言UML2.0是完全不同的维度.它在本质上更加复杂和广泛. 与UML1.5版本相比,文件的程度也增加了. UML2.0中还增加了新的功能,所以它的使用可以更广泛. UML2.0将正式 ...

  6. WPF编程学习——样式

    本文目录 1.引言 2.怎样使用样式? 3.内联样式 4.已命名样式 5.元素类型样式 6.编程控制样式 7.触发器 1.引言 样式(Style),主要是用来让元素或内容呈现一定外观的属性.WPF中的 ...

  7. CSS兼容问题大全

    1.chorme 最小字体的兼容性. 问题描述:ff和IE最小字体可设置为1px,可是chorme中文版最小字体是12px,小于12px的字体全部显示为12px.解决方案:chorme支持CSS3的, ...

  8. css display visibility

    当visibility被设置为"hidden"的时候,元素虽然被隐藏了,但它仍然占据它原来所在的位置.注意,当元素被隐藏之后,就不能再接收到其它事件了. display属性就有一点 ...

  9. Andoid自动判断输入是电话,网址或者Email的方法----Linkify的应用!

    本节要讲的是,当我们在一个EditText输入电话或者网址还是Email的时候,让Android自动判断,当我们输入的是电话,我们点击输入内容将调用打电话程序,当我们输入是网址点击将打开浏览器程序.而 ...

  10. android:scaleType属性

    android:scaleType是控制图片如何resized/moved来匹对ImageView的size. ImageView.ScaleType / android:scaleType值的意义区 ...