181. Employees Earning More Than Their Managers

https://leetcode.com/problems/employees-earning-more-than-their-managers/#/description

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 |
+----------+
SELECT
a.Name AS 'Employee'
FROM
Employee AS a,
Employee AS b
WHERE
a.ManagerId = b.Id
AND a.Salary > b.Salary
;

Actually, JOIN is a more common and efficient way to link tables together, and we can use ON to specify some conditions.

SELECT
a.NAME AS Employee
FROM Employee AS a JOIN Employee AS b
ON a.ManagerId = b.Id
AND a.Salary > b.Salary
; SELECT Name As Employee
FROM Employee A
WHERE EXISTS(
SELECT Name
FROM Employee B
WHERE Id=A.ManagerId AND Salary<=A.Salary 
)

196. Delete Duplicate Emails

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
Id is the primary key column for this table.

For example, after running your query, the above Person table should have the following rows:

+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
+----+------------------+

DELETE P
FROM Person P,Person P1
WHERE P.Id>P1.Id AND P.Email=P1.Email;

 

197. Rising Temperature

Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.

+---------+------------+------------------+
| Id(INT) | Date(DATE) | Temperature(INT) |
+---------+------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------+------------------+

For example, return the following Ids for the above Weather table:

+----+
| Id |
+----+
| 2 |
| 4 |
+----+

# Write your MySQL query statement below
SELECT W1.Id
FROM Weather w1,Weather w2
Where DATEDIFF(w1.Date,w2.Date)=1 AND W1.Temperature>W2.Temperature;

SELECT wt1.Id

FROM Weather wt1, Weather wt2

WHERE wt1.Temperature > wt2.Temperature AND TO_DAYS(wt1.DATE)-TO_DAYS(wt2.DATE)=1;

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.

SELECT Distinct(a.Email)
FROM Person a,Person b
WHERE a.Id<>b.Id and a.Email=b.Email;

 

sql_自连接,181,182,196,197的更多相关文章

  1. leetcode_sql_3,181,182,183

    181. Employees Earning More Than Their Managers The Employee table holds all employees including the ...

  2. [SQL]3.26--175+176+177+178+180+181+182

    175.组合两个表 题目 Code SELECT FirstName, LastName, City, State FROM Person LEFT JOIN Address --由于需要Person ...

  3. 搞定C系语言的的swap

    http://www.cs.utsa.edu/~wagner/CS2213/swap/swap.html 原地址 Parameters, by value and by reference: Both ...

  4. iOS---iOS10适配iOS当前所有系统的远程推送

    一.iOS推送通知简介 众所周知苹果的推送通知从iOS3开始出现, 每一年都会更新一些新的用法. 譬如iOS7出现的Silent remote notifications(远程静默推送), iOS8出 ...

  5. 基于TQ2440的SPI驱动学习(OLED)

    平台简介 开发板:TQ2440 (NandFlash:256M  内存:64M) u-boot版本:u-boot-2015.04 内核版本:Linux-3.14 作者:彭东林 邮箱:pengdongl ...

  6. Java的修饰符

    转自:http://blog.csdn.net/manyizilin/article/details/51926230#L42 修饰符: 像其他语言一样,Java可以使用修饰符来修饰类中方法和属性.主 ...

  7. C# DBHelper 第二版

    1. [代码][C#]代码     跳至 [1] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

  8. C# 调用webservice 几种办法(转载)

    原文地址: http://www.cnblogs.com/eagle1986/archive/2012/09/03/2669699.html //=========================== ...

  9. ***CodeIgniter集成微信支付(转)

    微信支付Native扫码支付模式二之CodeIgniter集成篇  http://www.cnblogs.com/24la/p/wxpay-native-qrcode-codeigniter.html ...

随机推荐

  1. C# Winform 窗体传值 利用委托 子窗体传值给父窗体

    常用的Winform窗体传值有两种方式. 1.更改Form.designer.cs文件,将控件的设为Public,供子窗体访问. 在designer.cs文件的最后,找到你的控件声明. private ...

  2. Gradle命令详解与导入第三方包--快速打包

    快速打包app:gradlew assembleRelease --console plain (好使) 下边的方法暂时不好使,可以用的兄弟请教下哈! Android Studio + Gradle的 ...

  3. File类之常用方法

    package IoDemo; import java.io.File; import java.io.IOException; /** * @Title:FileTest * @Descriptio ...

  4. JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体

    1. struts 工作流程图 超链接 2. 入门案例 struts入门案例: 1.写一个注册页面,把请求交给 struts处理 <form action="${pageContext ...

  5. phpstrom ctrl+s无法上传的问题 解决

    首先这个不教你怎么配置同步FTP,这个教程网上太多了. 主要是配置好了上传,上传的好好的,突然某一天不能上传了,或者配置好了上传不了. 我遇到的问题是,如果你没有点击Use this server a ...

  6. java加载jdbc驱动三种方式的比较

    一.引言 平时连接数据库的时候首先要加载jdbc驱动,这一步骤其实有三种方式,他们的区别?优劣? 二.快速了解三种加载方式 Class.forName(“com.mysql.jdbc.Driver”) ...

  7. mysql 转移数据目录

    由于MySql的数据库文件和日志文件比较大,导致磁盘空间不够,在添加新的磁盘之后,需要把MySql的数据转移到新挂载的目录下. 1.停止MySql服务: /etc/rc.d/init.d/mysql ...

  8. CSS自定义字体(@font-face选择符)

    @font-face是CSS中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体. 语法规则: @f ...

  9. linux中的kill命令

    一. 定义 kill命令用来删除执行中的程序或工作.kill可将指定的信息送至程序.预设的信息为SIGTERM(15),可将指定程序终止.若仍无法终止该程序,可使用SIGKILL(9)信息尝试强制删除 ...

  10. [转]linux将一个服务器上的文件或者文件夹复制到另一台服务器上

    本文转载自<linux 将一个服务器上的文件或者文件夹复制到另一台服务器上>,有时间实践一把 使用scp将一个Linux系统中的文件或文件夹复制到另一台Linux服务器上 复制文件或文件夹 ...