-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhouse property sales analysis.sql
More file actions
63 lines (48 loc) · 1.4 KB
/
Copy pathhouse property sales analysis.sql
File metadata and controls
63 lines (48 loc) · 1.4 KB
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
-- Taking a look at the dataset
select * from raw_sales;
-- Checking for inappropriate values in the propertytype column
select distinct propertytype
from raw_sales;
-- Checking for inappropriate values in the bedrooms column
select distinct bedrooms
from raw_sales;
-- HOUSE PROPERTY SALES ANALYSIS QUESTIONS
-- Which date corresponds to the highest number of sales?
select datesold as date, count(price) as highest_sales
from raw_sales
group by datesold
order by highest_sales desc
limit 1;
-- Find out the postcode with the highest average price per sale? (Using Aggregate Functions)
select postcode, avg(price) as avg_price
from raw_sales
group by postcode
order by avg_price desc
limit 1;
-- Which year witnessed the lowest number of sales?
select year(datesold) as year, count(*) as lowest_sales
from raw_sales
group by year(datesold)
order by lowest_sales asc
limit 1;
-- Use the window function to deduce the top six postcodes by year's price
WITH sales_cte AS (
SELECT
year(datesold) as year,
postcode,
price,
dense_rank() OVER (PARTITION BY year(datesold), postcode ORDER BY price DESC) rnk
FROM raw_sales
)
SELECT
year,
postcode,
price
FROM (
SELECT
*,
row_number() OVER (PARTITION BY year ORDER BY price DESC) as row_num
FROM sales_cte
WHERE rnk < 2
) subquery
WHERE row_num BETWEEN 1 AND 6;