[Leetcode|SQL] Combine Two Tables
Table: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
Table: Address
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
FirstName, LastName, City, State
Solution:
Whenever we need to combine records from two or more tables, we need to join the tables. There are two common types of join and it is important to understand their differences:
- Inner Join - Selects only records from both tables that have matching values. This is also the default join.
- Outer Join - Does not require each record in the two joined tables to have a matching record.
- Left Outer Join - Returns all values from the left table, even if there is no match with the right table.
Since the question requires information for each person regardless if there is an address for that person, the answer is to use an outer join.
You may use either a LEFT JOIN
(Person LEFT JOIN Address) or a RIGHT JOIN
(Address RIGHT JOIN Person).
Ans:
# Write your MySQL query statement below
SELECT Person.FirstName, Person.LastName, Address.City, Address.State FROM Person LEFT JOIN Address ON Person.PersonId=Address.PersonId
[Leetcode|SQL] Combine Two Tables的更多相关文章
- LeetCode 175 Combine Two Tables mysql,left join 难度:0
https://leetcode.com/problems/combine-two-tables/ Combine Two Tables Table: Person +-------------+-- ...
- LeetCode 175. Combine Two Tables 【MySQL中连接查询on和where的区别】
一.题目 175. Combine Two Tables 二.分析 连接查询的时候要考虑where和on的区别 where : 查询时,连接的时候是必须严格满足条件的,满足了才会加入到临时表中. on ...
- sql 中多表查询-leetcode : Combine Two Tables
因为对数据库的内容早都忘得差不多了,所以我的第一感觉是: select Person.FirstName, Person.LastName, Address.City from Person, Add ...
- leetcode 175 Combine Two Tables join用法
https://leetcode.com/problemset/database/ ACM退役,刚好要学sql,一定要刷题才行,leetcode吧. 这一题,说了两个表,一个左一个右吧,左边的pers ...
- Leetcode 175. Combine Two Tables
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- LeetCode 175. Combine Two Tables (组合两个表)
题目标签: 题目给了我们两个table,让我们合并,根据Person为主. 因为题目说了 提供person 信息,不管这个人有没有地址.所以这里用Left Join. Java Solution: R ...
- LeetCode SQL题目(第一弹)
LeetCode SQL题目 注意:Leetcode上的SQL编程题都提供了数据表的架构程序,只需要将它贴入本地数据库即可调试自己编写的程序 不管是MS-SQL Server还是MySQL都需要登陆才 ...
- 175. Combine Two Tables【LeetCode】-LEFT JON 和RIGHT JOIN,两张表关联查询-java -sql入门
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- [LeetCode] Combine Two Tables 联合两表
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
随机推荐
- C#的索引器
using System; using System.Collections; using System.Collections.Generic; using System.IO; namespace ...
- ***PHP中error_reporting()用法详解(含codeigniter框架中屏蔽错误提示的解决方案)
php中我们对错误的处理会常用到error_reporting函数了,大家可以看到最多的是error_reporting(E_ALL ^ E_NOTICE)了,这个到底什么意思呢,下面我来来看看. e ...
- html5移动Web开发实战
1.解决横竖屏字体大小变化 html{ -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; text-size-adjust:100 ...
- php调用phpqrcode.php生成二维码
下载phpqrcode.php 下载地址: http://files.cnblogs.com/files/qhorse/phpqrcode.rar qrcode.php文件: <?php inc ...
- win8.1/win10 UEFI + GPT 安装(测试机型:华硕S56CM)
本教程简要介绍在UEFI 启动模式下在GPT分区表中,最简单的方法安装 Windows 10 x64 位系统.(并非傻瓜教程,安装者总要有一定的经验基础)下面先简单介绍一下UEFI和GTP. UEFI ...
- gdb调试小结
gdb最基本的调试命令. 1以调试程序test.cpp为例: 进入调试环境 gdb test 2.b 12 在文件的第12行设置断点. 删除断点: info b 列出所有的断点信息 (gdb) inf ...
- 汇编指令CLI/STI
CLI禁止中断发生STL允许中断发生 这两个指令只能在内核模式下执行,不可以在用户模式下执行:而且在内核模式下执行时,应该尽可能快的恢复中断,因为CLI会禁用硬件中断,若长时间禁止中断会影响其他动作的 ...
- android加载大量图片内存溢出的三种方法
android加载大量图片内存溢出的三种解决办法 方法一: 在从网络或本地加载图片的时候,只加载缩略图. /** * 按照路径加载图片 * @param path 图片资源的存放路径 * @para ...
- Java学习随笔1:Java是值传递还是引用传递?
Java always passes arguments by value NOT by reference. Let me explain this through an example: publ ...
- 【转】Java时间日期包 JodaTime
maven: http://mvnrepository.com/artifact/joda-time/joda-time 1, 获取每天的零点 DateTime dt=new DateTime().w ...