美文网首页
sql 相关2

sql 相关2

作者: lmem | 来源:发表于2016-12-11 17:19 被阅读2次

1.Employees Earning More Than Their Managers

+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

找出比mananger工资多的雇员

+----------+
| Employee |
+----------+
| Joe      |
+----------+

select a.Name as Employee  from Employee a join Employee b 
on a.ManagerId = b.Id where a.Salary > b.Salary 

2.Customers Who Never Order

查找没有购物的用户名字

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 as Customers from Customers a left join Orders b 
on a.id = b.CustomerId  where b.id is NULL

3.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 FROM p1 USING Person p1 INNER JOIN Person p2 
WHERE p1.Email = p2.Email AND p1.Id > p2.Id
# delete by min
delete from Person where ID not in (
select * from (select MIN(ID) from Person group by Email) t
)

4.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 |
+----+
#高TO_DAYS
SELECT w1.Id FROM Weather w1 INNER JOIN Weather w2 
ON TO_DAYS(w1.Date) = TO_DAYS(w2.Date) + 1 AND w1.Temperature > w2.Temperature

相关文章

  • sql 相关2

    1.Employees Earning More Than Their Managers 找出比mananger工...

  • oracle使用技巧

    数据库相关-客户端 PL/SQL developer 1. 记住密码 2.自动执行光标所在的SQL 3. SQL自...

  • IntelliJ 搭建SSM框架

    1). 创建项目 按图片填写相关内容 2). 填写相关配置 3). 配置工具 Web -> web,SQL -> ...

  • sql相关

  • SQL相关

    查 SELECT * FROM where 限定搜索范围 = 等于<> 不等于 大于< 小于= 大于...

  • MySQL慢查询日志相关笔记

    MySQL优化SQL,针对慢SQL语句的查询及相关配置。相关命令: 需要永久开启慢SQL查询日志,需要在my.in...

  • 「十六天计划」第七天

    @2.12 1. Sqlite 数据库存储。 2. 相关api,SqliteaOpenHelper。 3. Sql...

  • SQL相关知识

    (not)exists 用于检查有无元组存在 聚集函数 不允许用于where子句中 COUNT SUM AVG M...

  • sql 优化相关

    选择合适的数据库引擎 Myisam存储引擎:如果表对事物要求不高,同时是以查询和添加为主的,我们考虑使用myisa...

  • sql 相关3

    1.Nth Highest Salary 2. Rank Scores 3.找出连续出现的字段Consecutiv...

网友评论

      本文标题:sql 相关2

      本文链接:https://www.haomeiwen.com/subject/zbjpmttx.html