Write a SQL query to get a list of tenants who are renting more than one apartment.

-- TABLE Apartments

+-------+------------+------------+
| AptID | UnitNumber | BuildingID |
+-------+------------+------------+
| 101 | A1 | 11 |
| 102 | A2 | 12 |
| 103 | A3 | 13 |
| 201 | B1 | 14 |
| 202 | B2 | 15 |
+-------+------------+------------+

-- TABLE Buildings

+------------+-----------+---------------+---------------+
| BuildingID | ComplexID | BuildingName | Address |
+------------+-----------+---------------+---------------+
| 11 | 1 | Eastern Hills | San Diego, CA |
| 12 | 2 | East End | Seattle, WA |
| 13 | 3 | North Park | New York |
| 14 | 4 | South Lake | Orlando, FL |
| 15 | 5 | West Forest | Atlanta, GA |
+------------+-----------+---------------+---------------+

-- TABLE Tenants

+----------+------------+
| TenantID | TenantName |
+----------+------------+
| 1000 | Zhang San |
| 1001 | Li Si |
| 1002 | Wang Wu |
| 1003 | Yang Liu |
+----------+------------+

-- TABLE Complexes

+-----------+---------------+
| ComplexID | ComplexName |
+-----------+---------------+
| 1 | Luxuary World |
| 2 | Paradise |
| 3 | Woderland |
| 4 | Dreamland |
| 5 | LostParis |
+-----------+---------------+

-- TABLE AptTenants

+----------+-------+
| TenantID | AptID |
+----------+-------+
| 1000 | 102 |
| 1001 | 102 |
| 1002 | 101 |
| 1002 | 103 |
| 1002 | 201 |
| 1003 | 202 |
+----------+-------+

-- TABLE Requests

+-----------+--------+-------+-------------+
| RequestID | Status | AptID | Description |
+-----------+--------+-------+-------------+
| 50 | Open | 101 | |
| 60 | Closed | 103 | |
| 70 | Closed | 102 | |
| 80 | Open | 201 | |
| 90 | Open | 202 | |
+-----------+--------+-------+-------------+

这道题让我们租了不止一间公寓的人,那么我们需要两个表Tenants和AptTenants,其他的表都不需要,那么我们可以用Inner Join来关联两个表,关于SQL的各种Join请参见我之前的博客SQL Left Join, Right Join, Inner Join, and Natural Join 各种Join小结,然后我们还需要用Group by和Count关键字来表示在AptTenants表中出现的次数大于1的TenantID,然后在Tenants表中找到名字返回:

解法一:

SELECT TenantName FROM Tenants
INNER JOIN
(SELECT TenantID FROM AptTenants
GROUP BY TenantID HAVING COUNT(*) > 1) C
ON Tenants.TenantID = C.TenantID;

下面这种解法用了Using关键字指定了相同列TenantID:

解法二:

SELECT TenantName FROM Tenants
INNER JOIN
(SELECT TenantID FROM AptTenants
GROUP BY TenantID HAVING COUNT(*) > 1) C
USING (TenantID);

运行结果:

+------------+
| TenantName |
+------------+
| Wang Wu |
+------------+

CareerCup All in One 题目汇总

[CareerCup] 15.1 Renting Apartment 租房的更多相关文章

  1. [CareerCup] 15.3 Renting Apartment III 租房之三

    Building #11 is undergoing a major renovation. Implement a query to close all requests from apartmen ...

  2. [CareerCup] 15.2 Renting Apartment II 租房之二

    Write a SQL query to get a list of all buildings and the number of open requests (Requests in which ...

  3. [CareerCup] 15.7 Student Grade 学生成绩

    15.7 Imagine a simple database storing information for students' grades. Design what this database m ...

  4. [CareerCup] 15.6 Entity Relationship Diagram 实体关系图

    15.6 Draw an entity-relationship diagram for a database with companies, people, and professionals (p ...

  5. [CareerCup] 15.5 Denormalization 逆规范化

    15.5 What is denormalization? Explain the pros and cons. 逆规范化Denormalization是一种通过添加冗余数据的数据库优化技术,可以帮助 ...

  6. [CareerCup] 15.4 Types of Join 各种交

    15.4 What are the different types of joins? Please explain how they differ and why certain types are ...

  7. CareerCup All in One 题目汇总

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  8. 2016中国APP分类排行榜参选入围产品公示

    2016中国APP分类排行榜参选入围产品公示   由中国科学院<互联网周刊>.中国社会科学院信息化研究中心.eNet硅谷动力共同主办的2016中国APP分类排行榜发布暨颁奖晚宴即将举行.此 ...

  9. 程序员Y先生投保案例分享

    大家好,我是闲鱼君.我在2018年底搞了个副业,做了保险经纪人.保险经纪人是为用户服务的第三方机构,找经纪人买保险省钱.省力.保险一次就买对,而且还能提供后续理赔服务,具体可以看我的文章<201 ...

随机推荐

  1. 6个值得推荐的Android开源框架简介(转)

    虽然我们在做app的时候并不一定用到框架,但是一些好框架的思想是非常有学习价值的 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo  ...

  2. Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构

    分享两篇Win 10应用开发的XML文档结构:Win 10 开发中Adaptive磁贴模板的XML文档结构,Win10 应用开发中自适应Toast通知的XML文档结构. Win 10 开发中Adapt ...

  3. LoadRunner上传文件脚本

  4. Liferay 6.2 改造系列之六:修改系统初始化信息

    将初始化过程修改为:中文语言 在/portal-master/portal-impl/src/system.properties文件中,有如下配置: # # Set the default local ...

  5. 使用jQuery要注意的问题

    1. $.find()与$.children()的区别 有如下HTML片段: 复制代码代码如下: <div id="div_four"> <input id=&q ...

  6. css/js(工作中遇到的问题)-3

    设置宽高比 使用padding/margin-top/bottom; 设置出教准确的自适应布局; 用于预加载图片; 关于数据库设置 添加extra对象用于扩展; 添加type类型; 对于字体 使用百分 ...

  7. Android和SQLite版本对应关系

    Android和SQLite版本对应关系 今天Xamarin群有人问到Android和SQLite版本如何对应,顺手查了一下,贴出来. SQLite 3.8.4.3: • 21-5.0-Lollipo ...

  8. 快消品销售管理系统,PDA销售管理系统,销售拜访PDA,进销存管理PDA系统 移动扫描打印开单POS

    各种ERP软件的移动订单及移动车销解决方案是针对各个需要快速.便捷的,通过智能PDA移动智能终端设备实现销售订单下达及快速车销的应用解决方案.通过将移动订单及移动车销集成到ERP的移动解决方案,可以帮 ...

  9. CSS3-基于浮动的布局,响应式WEB设计,定位网页上的元素,设计打印页面的css技术

    基于浮动的布局: 1.除非图片设置了宽度,否则始终应该要对浮动的图片设置一个宽度,这样可以让浏览器给其他内容腾出环绕的空间 2.当侧边栏的高度与主内容区的高度不一致的时候,可以用个margin进行调整 ...

  10. http://www.roncoo.com/article/detail/124661

    http://www.roncoo.com/article/detail/124661