-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSubqueries Exercise 1 - Self Contained.sql
64 lines (53 loc) · 1.64 KB
/
Subqueries Exercise 1 - Self Contained.sql
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Subqueries Exercise 1 - Self Contained
This exercise will use two related tables
* PricePaidSW12 - sales of properties in London SW12 from 1995 to 2019.
* PropertyTypeLookup - a lookup table on the PropertyType column of PricePaidSW12. This contains a one letter code e.g. 'D'.
The PropertyTypeLookup has a column PropertyTypeCode with matching values and a column PropertyTypeName with the description e.g. 'Detached'
In this example we will focus sales in a particular street, Ranmere Street
*/
-- List properties sold in Ranmere Street
SELECT
pp.TransactionID
, pp.TransactionDate
, pp.Price
, pp.PropertyType
, pp.PAON
, pp.Street
FROM
PricePaidSW12 pp
WHERE
pp.Street = 'Ranmere Street'
-- Get the average price of properties sold in Ranmere Street
SELECT
AVG(pp.Price)
FROM
PricePaidSW12 pp
WHERE
pp.Street = 'Ranmere Street';
-- Which property types have not been sold in the Ranmere Street? (use a self-contained subquery to answer this)
SELECT
ptl.PropertyTypeName
FROM
PropertyTypeLookup ptl
-- complete the query below here
-- List properties sold for more than the average price
-- Use a simple subquery in the WHERE clause and in the column list
SELECT
pp.TransactionID
, pp.TransactionDate
, pp.Price
, pp.PropertyType
, pp.PAON
, pp.Street
, 'write the subquery here' AS AveragePriceInRanmereStreet
FROM
PricePaidSW12 pp
WHERE
pp.Street = 'Ranmere Street'
-- add the subquery here
ORDER BY pp.Price ;
-- Optional - Advanced task
-- Calculate the price difference over the average price
-- Use a simple subquery in the WHERE clause and in the column list
-- Do this for all sales rather than a single street