[CareerCup] 15.1 Renting Apartment 租房

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

-- TABLE Apartments

+-------+------------+------------+
| AptID | UnitNumber | BuildingID |
+-------+------------+------------+
|   101 | A1         |          1 |
|   102 | A2         |          2 |
|   103 | A3         |          3 |
|   201 | B1         |          4 |
|   202 | B2         |          5 |
+-------+------------+------------+

-- TABLE Buildings

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

-- TABLE Tenants

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

-- TABLE Complexes

+-----------+---------------+
| ComplexID | ComplexName   |
+-----------+---------------+
|        11 | Luxuary World |
|        12 | Paradise      |
|        13 | Woderland     |
|        14 | Dreamland     |
|        15 | LostParis     |
+-----------+---------------+

-- TABLE AptTenants

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

-- TABLE Requests

+-----------+---------+-------+-------------+
| RequestID | Status  | AptID | Description |
+-----------+---------+-------+-------------+
|        50 | Rented  |   101 |             |
|        60 | Pending |   103 |             |
+-----------+---------+-------+-------------+

这道题让我们租了不止一间公寓的人,那么我们需要两个表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);

CareerCup All in One 题目汇总

时间: 2024-11-09 17:39:45

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

[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 status equals 'Open'). -- TABLE Apartments +-------+------------+------------+ | AptID | UnitNumber | BuildingID | +-------+------------+------------+

[CareerCup] 15.7 Student Grade 学生成绩

15.7 Imagine a simple database storing information for students' grades. Design what this database might look like and provide a SQL query to return a list of the honor roll students (top 10%), sorted by their grade point average. 在一个简化的数据库中我们有三个表,St

[CareerCup] 15.5 Denormalization 逆规范化

15.5 What is denormalization? Explain the pros and cons. 逆规范化Denormalization是一种通过添加冗余数据的数据库优化技术,可以帮助我们减少关系数据库中耗时的交Join.在一般的规范化的数据库中,我们将数据存在不同的表中是为了减少冗余数据,所以我们会尝试着每条数据在数据库中只有一份. 比如说,在一个规范化的数据库中,我们有Courses表和Teachers表,每个Courses表的一项都会保存teacherID,但是没有teac

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

15.6 Draw an entity-relationship diagram for a database with companies, people, and professionals (people who work for companies). 在公司Companies工作的人是专家Proferssionals,所以人和专家是ISA("is a")的关系,每个专家有例如等级,和工作经验和其他从人Person继承来的属性.一个专家一次只能为一个公司工作,而一个公司可以雇佣

CareerCup All in One 题目汇总

Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation String 1.4 Replace Spaces 1.5 Compress String 1.6 Rotate Image 1.7 Set Matrix Zeroes 1.8 String Rotation Chapter 2. Linked Lists 2.1 Remove Duplicates

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

2016中国APP分类排行榜参选入围产品公示 由中国科学院<互联网周刊>.中国社会科学院信息化研究中心.eNet硅谷动力共同主办的2016中国APP分类排行榜发布暨颁奖晚宴即将举行.此次颁奖晚宴主题是:惊喜的构想只需要1分钟,而惊人的产品却需要许多共性内涵--天下高见,多有相合.此次将要发布的APP分类排行榜,深刻剖析市场动态,解读新时代创新定义,分析用户真实需求,从不同的角度以产品实例为众人作答.以下为迄今的入围产品名单(注:现仍在查遗补缺中). 2016中国APP分类排行榜参选入围产品公示

高德API+.NET解决租房问题(可能是最可靠房源:上海互助租房)

作者:李国宝链接:https://zhuanlan.zhihu.com/p/22113421来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. PS:最近点赞和关注的小伙伴有点多,感觉我都变得勤奋多了. 次标题:上海专版-互助租房高德搜房 房源来源: 互助租房(http://www.huzhumaifang.com/Renting/index.html) 互助租房微博(http://weibo.com/u/5389952376) 微博简介: 简介: 这里是新浪微博“互

[CareerCup] 18.4 Count Number of Two 统计数字2的个数

18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字

租房记

po主刚租好了房子,记录下心得神马的 po主对生活品质要求较高,因此,之前一直期望租的房子是精装或豪装,网上也看了些租房子的经验,都说,最好8要精装,原因列了非常多blablabla,当时是没往心里去的,还是要精装  上周日,和璇玑去看房子,先找的文二路上的我爱我家,接待我们的妹纸貌似是个新人,对业务各种不熟>_<.这里我吐槽下中介的用户体验-..为虾米木有双屏!妈蛋!仅仅有一个屏幕,中介和客户都是歪着头看啊!!这特么科学嘛!!!中介妹纸对周围的小区不熟悉,还是偶们告诉她要哪些小区,后来她介绍