-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_10_Task_10_Questions.sql
More file actions
30 lines (29 loc) · 964 Bytes
/
Day_10_Task_10_Questions.sql
File metadata and controls
30 lines (29 loc) · 964 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
--Questions
-- 10.1 Join table PEOPLE and ADDRESS, but keep only one address information for each person (we don't mind which record we take for each person).
-- i.e., the joined table should have the same number of rows as table PEOPLE
SELECT
PEOPLE.id AS PeopleID,
PEOPLE.name AS PeopleName,
ADDRESS.address AS PeopleAddressAtSomePoint
--ADDRESS.updatedate
FROM PEOPLE
JOIN ADDRESS
ON PEOPLE.id = ADDRESS.id
GROUP BY PEOPLE.id;
-- 10.2 Join table PEOPLE and ADDRESS, but ONLY keep the LATEST address information for each person.
-- i.e., the joined table should have the same number of rows as table PEOPLE
SELECT DISTINCT
a.id AS PeopleID,
a.name AS PeopleName,
a.LatestAddress AS PeopleLatestAddress
FROM
(SELECT
PEOPLE.id,
PEOPLE.name,
ADDRESS.address,
ADDRESS.updatedate,
max(ADDRESS.updatedate) OVER (PARTITION BY PEOPLE.id) AS LatestAddress
FROM PEOPLE
JOIN ADDRESS
ON PEOPLE.id = ADDRESS.id
) AS a;