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
按以上的数据格式查询,无论地址表有没有数据都要查询出来。
- 因为Person表中有的数据可能在Address表中没有数据,所以需要使用right join,无论在Address中有没有数据都回查询出来
select
p.FirstName,p.LastName,a.City,a.State
from
address a
right join
Person p
on a.PersonId = p.PersonId