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. canvas笔记1

    w3c定义: <canvas> 标签定义图形,比如图表和其他图像. <canvas> 标签只是图形容器,您必须使用脚本来绘制图形. canvas 对象 属性: width he ...

  2. Asp.Net Web API 2 官网菜鸟学习系列导航

    链接地址: http://www.cnblogs.com/aehyok/p/3446289.html

  3. 20145219 《Java程序设计》实验一 Java开发环境的熟悉(Linux + Eclipse)实验报告

    20145219 <Java程序设计>实验一 Java开发环境的熟悉(Windws + IDEA)实验报告 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用IDEA 编辑. ...

  4. Wyx20162314 2016-2017-2 《程序设计与数据结构》课程总结

    20162314 2016-2017-2 <程序设计与数据结构>课程总结 一.每周作业.结对编程博客的链接汇总 预备作业一01 20162314:专业的期许.浅谈师生关系.对未来学习任务的 ...

  5. Could not find com.android.support:appcompat-v7:23.1.1

    在刚接触Android Studio的时候,这玩意整起来确实费劲,现在接触多了,感觉还好,毕竟还有一段提升的空间,以后的必然趋势,所以还是潜心下来好好搞搞. 废话少说,切入正题. 如图所示的error ...

  6. ThinkPHP3.2添加scws中文分词

    前言 前一段时间,公司网站做站内搜索,只简单针对输入的文字进行搜索,作全匹配检索,搜索出来的内容很少.如何达到模糊搜索,匹配到更多的内容成了需要解决的问题.于是,今天想到可以做分词检索,如何对输入的一 ...

  7. SSL证书是“盾牌“还是”鸡肋“?

    德国联邦安全与IT办公室(BSI,职能相当于美国的国家安全与信息技术局)近日发布公告警告:网络攻击者冒充其发布了“关于Meltdown与Spectre攻击信息”的垃圾邮件,该邮件中包含指向修复补丁的页 ...

  8. linux service start|stop|restart

    用了这么些日子的linux/unix系统,也和别人一起合作开发了不少程序,发现高手都喜欢在命令行上操作,而且控制程序的运行偏好于使用脚本,加上参数如:start.restart.stop等. 后来自己 ...

  9. Amazon SES介绍 - SES发送邮件的过程

     Amazon SES,  全称Amazon Simple Email Service,即Amazon简单邮件服务,它是Amazon提供的一款供开发人员或公司企业用来处理邮件相关业务的服务,也就是说, ...

  10. 一块网卡多个IP实现

    ////////////////////////////写在前面//////////////////////////////////////////// 需要注意,这里我们是一块网卡多个IP,而并非是 ...