-
Notifications
You must be signed in to change notification settings - Fork 12
/
SQLQuery_Airbnb_NewYork.sql
1357 lines (1230 loc) · 56.7 KB
/
SQLQuery_Airbnb_NewYork.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--Viewing the dataset
---------------------
select * from DataAnalysis..['listings New York$']
order by id
--Data Cleaning and checking (used substring, replace, cast functions)
select property_type,cast(replace(substring(price,2,10),',','') as float) as Pricing from DataAnalysis..['listings New York$']
--where property_type='Entire condominium (condo)'
where cast(replace(substring(price,2,10),',','') as float)>0
order by Pricing desc
--Overall Data
select count(*) as 'Listings',sum(accommodates) as Accomodations,sum(number_of_reviews) as 'Reviews'
,round(avg(review_scores_rating),2) as 'Average Ratings',concat('$',round(avg(Pricing),2)) as 'Average Pricing'
from
(
select *,cast(replace(substring(price,2,10),',','') as float) as Pricing from DataAnalysis..['listings New York$']
) t
where Pricing>0
--Querying hosts and details who live in the same neighborhood as their listings
--Using self INNER JOINS
-------------------------------------------------------------------------------------------------------
select distinct(a.host_id), a.HOST_NAME, a.host_neighbourhood from DataAnalysis..['listings New York$'] a inner join DataAnalysis..['listings New York$'] b
on a.host_neighbourhood=b.neighbourhood_cleansed
and a.host_name=b.host_name
order by a.host_neighbourhood, a.host_id
select a.host_neighbourhood, count(distinct(a.host_id)) as Hosts_live_here from DataAnalysis..['listings New York$'] a inner join DataAnalysis..['listings New York$'] b
on a.host_neighbourhood=b.neighbourhood_cleansed
and a.host_name=b.host_name
group by a.host_neighbourhood
order by a.host_neighbourhood
--Further details
select t.host_name, t.host_neighbourhood, t.name, t.neighborhood_overview, t.Neighbourhood, t.property_type, t.room_type, t.accommodates, t.price, t.review_scores_rating, t.number_of_reviews
from
(
select distinct(a.host_id), a.host_name, a.host_neighbourhood, a.name, a.neighborhood_overview, a.neighbourhood_group_cleansed as Neighbourhood, a.property_type, a.room_type, a.accommodates, a.price, a.review_scores_rating , a.number_of_reviews
from DataAnalysis..['listings New York$'] a inner join DataAnalysis..['listings New York$'] b
on a.host_neighbourhood=b.neighbourhood_cleansed
and a.host_name=b.host_name
where a.neighbourhood is not null and
a.review_scores_rating is not null
) t
order by t.host_neighbourhood asc, (t.review_scores_rating*t.number_of_reviews) desc
--Further details of specific Neighbourhoods
--Using PROCEDURES
--Using Temporary Tables
--------------------------------------------
drop procedure if exists dbo.HostInSameNeighbourhood
go
create procedure dbo.HostInSameNeighbourhood
@neighbourhood nvarchar(100) --PARAMETER LOCATION
as
drop table if exists hostinfo
create table hostinfo(
Host_Name varchar(100),
Host_Neighbourhood varchar(100),
Name varchar(400),
Neighbourhood_Overview varchar(5000),
Neighbourhood_Group varchar(100),
Property varchar(100),
Room varchar(100),
Accomodates int,
Price float,
Score float,
Reviews float
)
insert into hostinfo
select t.host_name, t.host_neighbourhood, t.name, t.neighborhood_overview, t.Neighbourhood, t.property_type, t.room_type, t.accommodates, t.price, t.review_scores_rating, t.number_of_reviews
from
(
select distinct(a.host_id), a.host_name, a.host_neighbourhood, a.name, a.neighborhood_overview, a.neighbourhood_group_cleansed as Neighbourhood, a.property_type, a.room_type, a.accommodates, cast(replace(substring(a.price,2,10),',','') as float) as price, a.review_scores_rating , a.number_of_reviews
from DataAnalysis..['listings New York$'] a inner join DataAnalysis..['listings New York$'] b
on a.host_neighbourhood=b.neighbourhood_cleansed
and a.host_name=b.host_name
where a.neighbourhood is not null and
a.review_scores_rating is not null
) t
where t.Neighbourhood=@neighbourhood --PARAMETER LOCATION
and t.price>0
and t.review_scores_rating>0
and t.number_of_reviews>10
order by t.host_neighbourhood asc, (t.review_scores_rating*t.number_of_reviews) desc
select * from hostinfo
go
exec HostInSameNeighbourhood @neighbourhood='Bronx'
exec HostInSameNeighbourhood @neighbourhood='Brooklyn'
exec HostInSameNeighbourhood @neighbourhood='Manhattan'
exec HostInSameNeighbourhood @neighbourhood='Queens'
exec HostInSameNeighbourhood @neighbourhood='Staten Island'
--For viewing the trend(rolling count) of listings by new hosts
--Using PARTITION BY
---------------------------------------------------------------
select
Pricing, Dates as HostDates,
ROW_NUMBER() over(partition by Dates order by Dates) as Counts
from
(
select *,
cast(replace(substring(price,2,10),',','') as float) as Pricing,
cast(host_since as date) as Dates
from
DataAnalysis..['listings New York$']
) t
where
Pricing>0
and Dates is not null
and year(Dates)>2000
order by Dates
--Quantitative details about units and prices
--Using COUNT, MIN, MAX, AVG
--Using TEMPORARY TABLES
----------------------------------------------------------
drop table if exists Temp_table1
create table Temp_table1(
category varchar(200),
Units int,
MinPrice float,
MaxPrice float,
AvgPrice float
)
insert into Temp_table1
select property_type,
count(property_type),
min(cast(replace(substring(price,2,10),',','') as float)),
max(cast(replace(substring(price,2,10),',','') as float)),
Round(AVG(cast(replace(substring(price,2,10),',','') as float)),2)
from DataAnalysis..['listings New York$']
where cast(replace(substring(price,2,10),',','') as float)>0
group by property_type
select * from Temp_table1
select category, concat(MinPrice,' - ', MaxPrice) as Range,AvgPrice from Temp_table1
drop table if exists Temp_table2
create table Temp_table2(
category varchar(200),
Units int,
MinPrice float,
MaxPrice float,
AvgPrice float
)
insert into Temp_table2
select room_type,
count(room_type),
min(cast(replace(substring(price,2,10),',','') as float)),
max(cast(replace(substring(price,2,10),',','') as float)),
Round(AVG(cast(replace(substring(price,2,10),',','') as float)),2)
from DataAnalysis..['listings New York$']
where cast(replace(substring(price,2,10),',','') as float)>0
group by room_type
select category, Units, concat(MinPrice,' - ', MaxPrice) as Range,AvgPrice from Temp_table2
/*
category Units Range AvgPrice
Entire home/apt 20063 10 - 10000 217.04
Hotel room 207 50 - 1351 371.84
Private room 16828 10 - 10000 102.95
Shared room 576 15 - 10000 129.66
*/
drop table if exists Temp_table3
create table Temp_table3(
category varchar(200),
Units int,
MinPrice float,
MaxPrice float,
AvgPrice float
)
insert into Temp_table3
select neighbourhood_group_cleansed,
count(neighbourhood_group_cleansed),
min(cast(replace(substring(price,2,10),',','') as float)),
max(cast(replace(substring(price,2,10),',','') as float)),
Round(AVG(cast(replace(substring(price,2,10),',','') as float)),2)
from DataAnalysis..['listings New York$']
where cast(replace(substring(price,2,10),',','') as float)>0
group by neighbourhood_group_cleansed
select category, Units, concat(MinPrice,' - ', MaxPrice) as Range,AvgPrice from Temp_table3
/*
category Units Range AvgPrice
Bronx 1058 11 - 2000 105.85
Brooklyn 14507 10 - 9999 136.48
Manhattan 16592 10 - 10000 212.14
Queens 5178 10 - 10000 113.37
Staten Island 339 10 - 1200 117.45
*/
drop table if exists Temp_table04
create table Temp_table04(
category varchar(200),
Units int,
MinPrice float,
MaxPrice float,
AvgPrice float
)
insert into Temp_table04
select neighbourhood_cleansed,
count(neighbourhood_cleansed),
min(cast(replace(substring(price,2,10),',','') as float)),
max(cast(replace(substring(price,2,10),',','') as float)),
Round(AVG(cast(replace(substring(price,2,10),',','') as float)),2)
from DataAnalysis..['listings New York$']
where cast(replace(substring(price,2,10),',','') as float)>0
and review_scores_rating>0
group by neighbourhood_cleansed
select category, Units, concat(MinPrice,' - ', MaxPrice) as Range,AvgPrice from Temp_table04
where category='Allerton'
--order by category
--Querying ratings info
-----------------------
drop table if exists Review_table1
create table Review_table1(
Name varchar(300),
Neighbourhood_group varchar(100),
Neighbourhood varchar(200),
Property_type varchar(100),
Room_type varchar(100),
Price float,
Reviews int,
Score float,
Score_Accuracy float,
Cleanliness float,
Checkin float,
Communication float,
Location float,
Value float
)
insert into Review_table1
select
name,
neighbourhood_group_cleansed,
neighbourhood_cleansed,
property_type,
room_type,
cast(replace(substring(price,2,10),',','') as float),
number_of_reviews,
review_scores_rating,
review_scores_accuracy,
review_scores_cleanliness,
review_scores_checkin,
review_scores_communication,
review_scores_location,
review_scores_value
from DataAnalysis..['listings New York$']
--Cleaning data
select * from Review_table1
where Score is not null
and Price>0
--and Reviews>0
--Categorizing
select Neighbourhood, round(avg(Score),2) as AvgScore from Review_table1
where Score is not null
and Price>0
group by Neighbourhood
order by Neighbourhood
select Property_type, round(avg(Score),2) as AvgScore from Review_table1
where Score is not null
and Price>0
group by Property_type
order by Property_type
select Room_type, round(avg(Score),2) as AvgScore from Review_table1
where Score is not null
and Price>0
group by Room_type
order by Room_type
select Room_type, round((sum(Score*Reviews)/sum(Reviews)),2) as AvgModScore from Review_table1
where Score is not null
and Price>0
group by Room_type
order by Room_type
--Property type wise
--Checking range and average of ratings
select Property_type, concat(min(Score),' - ', max(Score)) as Range_of_ratings, AVG(Score) as Aerage_ratings from Review_table1
where Score is not null
and Price>0
and Reviews>10 --for reliable scores
--and Reviews>0
group by Property_type
--Room type wise
--Checking range and average of ratings
select Room_type, concat(min(Score),' - ', max(Score)) as Range_of_ratings, AVG(Score) as Aerage_ratings from Review_table1
where Score is not null
and Price>0
and Reviews>10 --for reliable scores
--and Reviews>0
group by Room_type
/*
Room_type Range_of_ratings Aerage_ratings
Hotel room 3.37 - 5 4.43
Shared room 3.79 - 5 4.68255555555556
Private room 2.36 - 5 4.72813807144178
Entire home/apt 3.5 - 5 4.76333653978286
*/
--Neighbourhood wise
--Checking range and average of ratings
select Neighbourhood, concat(min(Score),' - ', max(Score)) as Range_of_ratings, AVG(Score) as Aerage_ratings from Review_table1
where Score is not null
and Price>0
and Reviews>10 --for reliable scores
--and Reviews>0
group by Neighbourhood
--Checking min
select top 1 Neighbourhood, min(Score) as Minimum from Review_table1
where Score is not null
and Price>0
and Reviews>10 --for reliable scores
--and Reviews>0
group by Neighbourhood
order by Minimum
--Neighbourhood wise
--Checking range and average of ratings
select Neighbourhood_group, concat(min(Score),' - ', max(Score)) as Range_of_ratings, AVG(Score) as Aerage_ratings from Review_table1
where Score is not null
and Price>0
and Reviews>10 --for reliable scores
--and Reviews>0
group by Neighbourhood_group
/*
Neighbourhood Range_of_ratings Aerage_ratings
Brooklyn 3.87 - 5 4.76833826794965
Bronx 3.83 - 5 4.7424838012959
Manhattan 2.36 - 5 4.72136335209502
Staten Island 4.08 - 5 4.76916666666667
Queens 3.64 - 5 4.74065934065933
*/
------------
--ANALYSIS |
------------
--Do HIGH REVIEWS tend to be associated with MORE EXPENSIVE or LESS EXPENSIVE LISTINGS?
---------------------------------------------------------------------------------------
--Checking Correlation of price and ratings using Pearson's formula
-------------------------------------------------------------------
/*
Pearson's formula
r = [ n(SUM(xy)-SUM(x)SUM(y) ] / [ sqrt (nSUM(x^2)-(SUM(x)^2))(nSUM(y^2)-(SUM(y)^2)) ]
r=correlation coefficient
if r-->1 strong correlation
if r-->-1 poor correaltion
r=(n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))
*/
-------------------------------------------------------------------
select n, Sx,Sy,Sxy,Sx2,Sy2,(n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from
(
select
count(*) as n,
sum(price) as Sx,
sum(Score) as Sy,
sum(price*Score) as Sxy,
sum(power(price,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table1
where Score is not null
and Price>0
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
---------------------------------------------------------------------------------------
--Overall there was a WEAK POSITIVE CORRELATION between MORE EXPENSIVE & HIGH REVIEWS |
---------------------------------------------------------------------------------------
--Displaying correlation Neighbourhood wise
select Neighbourhood, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Neighbourhood,
count(*) as n,
sum(price) as Sx,
sum(Score) as Sy,
sum(price*Score) as Sxy,
sum(power(price,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table1
where Score is not null
and Price>0
group by Neighbourhood
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
--------------------------------------------------------------------------------------------------
--There was a WEAK POSITIVE CORRELATION between MORE EXPENSIVE & HIGH REVIEWS for Neighbourhoods |
--------------------------------------------------------------------------------------------------
--Displaying correlation Property type wise
select Property_type, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Property_type,
count(*) as n,
sum(price) as Sx,
sum(Score) as Sy,
sum(price*Score) as Sxy,
sum(power(price,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table1
where Score is not null
and Price>0
and Score>0
group by Property_type
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
--Displaying correlation Room type type wise
select Room_type, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Room_type,
count(*) as n,
sum(price) as Sx,
sum(Score) as Sy,
sum(price*Score) as Sxy,
sum(power(price,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table1
where Score is not null
and Price>0
and Score>0
and Reviews>50 --to see more reliable reviews
group by Room_type
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
-------------------------------------------------------------------------------------------------------------------------------
--Overall there was a WEAK POSITIVE CORRELATION between MORE EXPENSIVE & HIGH REVIEWS for Hotels, Shared rooms & Entire Homes |
--& there was a VERY WEAK NEGATIVE CORRELATION between MORE EXPENSIVE & HIGH REVIEWS for Private Rooms |
-------------------------------------------------------------------------------------------------------------------------------
--Displaying DETAILED correlation Room type type wise in Neighbourhoods
select Neighbourhood, Room_type, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R
from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Neighbourhood,Room_type,
count(*) as n,
sum(price) as Sx,
sum(Score) as Sy,
sum(price*Score) as Sxy,
sum(power(price,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table1
where Score is not null
and Price>0
group by Neighbourhood, Room_type
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
order by Neighbourhood,Room_type
/*
Neighbourhood Room_type Pearson's R
Bronx Entire home/apt 0.0730143761665861
Bronx Private room -0.0806435070687292
Bronx Shared room 0.598768036753285
Brooklyn Entire home/apt 0.0146351679548435
Brooklyn Hotel room 0.0427414184105248
Brooklyn Private room 0.000879055653266063
Brooklyn Shared room 0.0188612179936523
Manhattan Entire home/apt 0.0250052568046383
Manhattan Hotel room 0.0669069528323807
Manhattan Private room 0.0127105849369331
Manhattan Shared room 0.0163970320537131
Queens Entire home/apt -0.00795699866522247
Queens Hotel room 0.615288655291931
Queens Private room 0.0162201290587321
Queens Shared room -0.0102678623254058
Staten Island Entire home/apt -0.00258930852369308
Staten Island Private room 0.0700163898636942
Staten Island Shared room 0.999999999999992
*/
--Do HIGH REVIEWS tend to be associated with MORE BEDROOMS & BATHROOMS or LESS?
-------------------------------------------------------------------------------
--Extracting bathroom and bedroom count
--Using PATINDEX
select
cast(
isnull(
replace(bathrooms_text,
(substring(bathrooms_text,PATINDEX('%[a-z]%',bathrooms_text),len(bathrooms_text))),
'')
,0) as float) as Bathrooms
from DataAnalysis..['listings New York$']
order by id
select cast(isnull(bedrooms,0) as float) as Bedrooms from DataAnalysis..['listings New York$']
order by id
--Querying bedrooms & bathrooms info
------------------------------------
drop table if exists Review_table2
create table Review_table2(
Name varchar(300),
Neighbourhood varchar(100),
Property_type varchar(100),
Room_type varchar(100),
Price float,
Reviews int,
Score float,
Score_Accuracy float,
Cleanliness float,
Checkin float,
Communication float,
Location float,
Value float,
Bedrooms float,
Bathrooms float
)
insert into Review_table2
select name,
neighbourhood_group_cleansed,
property_type, room_type,
cast(replace(substring(price,2,10),',','') as float),
number_of_reviews,
review_scores_rating,
review_scores_accuracy,
review_scores_cleanliness,
review_scores_checkin,
review_scores_communication,
review_scores_location,
review_scores_value,
cast(isnull(bedrooms,0) as float),
cast(
isnull(
replace(bathrooms_text,
(substring(bathrooms_text,PATINDEX('%[a-z]%',bathrooms_text),len(bathrooms_text))),
'')
,0) as float)
from DataAnalysis..['listings New York$']
--Validating the data
select * from Review_table2
select name,Bedrooms,Bathrooms from Review_table2
where name='BEST BET IN HARLEM' or
name ='Lovely Room 1, Garden, Best Area, Legal rental' or
name ='Midtown Pied-a-terre'
select name, bedrooms,bathrooms_text from DataAnalysis..['listings New York$']
where name='BEST BET IN HARLEM' or
name ='Lovely Room 1, Garden, Best Area, Legal rental' or
name ='Midtown Pied-a-terre'
--Checking Correlation of bedrooms and ratings using Pearson's formula
----------------------------------------------------------------------
select n, Sx,Sy,Sxy,Sx2,Sy2,(n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from
(
select
count(*) as n,
sum(Bedrooms) as Sx,
sum(Score) as Sy,
sum(Bedrooms*Score) as Sxy,
sum(power(Bedrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
--------------------------------------------------------------------------------------
--Overall there was a WEAK POSITIVE CORRELATION between MORE BEDROOMS & HIGH REVIEWS |
--------------------------------------------------------------------------------------
--Displaying correlation Neighbourhood wise
select Neighbourhood, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Neighbourhood,
count(*) as n,
sum(Bedrooms) as Sx,
sum(Score) as Sy,
sum(Bedrooms*Score) as Sxy,
sum(power(Bedrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
group by Neighbourhood
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
----------------------------------------------------------------------------------------------------------------
--There was a WEAK POSITIVE CORRELATION between MORE BEDROOMS & HIGH REVIEWS for Brooklyn, Manhattan, & Queens |
--There was a WEAK NEGATIVE CORRELATION between MORE BEDROOMS & HIGH REVIEWS for Bronx & Staten Island |
----------------------------------------------------------------------------------------------------------------
--Displaying correlation Property type wise
select Property_type, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Property_type,
count(*) as n,
sum(Bedrooms) as Sx,
sum(Score) as Sy,
sum(Bedrooms*Score) as Sxy,
sum(power(Bedrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
and Score>0
group by Property_type
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
--Displaying correlation Room type type wise
select Room_type, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Room_type,
count(*) as n,
sum(Bedrooms) as Sx,
sum(Score) as Sy,
sum(Bedrooms*Score) as Sxy,
sum(power(Bedrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
and Score>0
group by Room_type
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
----------------------------------------------------------------------------------------------------------------
--There was a WEAK POSITIVE CORRELATION between MORE BEDROOMS & HIGH REVIEWS for Private Rooms |
--There was a WEAK NEGATIVE CORRELATION between MORE BEDROOMS & HIGH REVIEWS for Hotels & Entire Homes |
----------------------------------------------------------------------------------------------------------------
----Displaying correlation Room type type wise in Neighbourhoods for better insights
select Neighbourhood, Room_type, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Neighbourhood,Room_type,
count(*) as n,
sum(Bedrooms) as Sx,
sum(Score) as Sy,
sum(Bedrooms*Score) as Sxy,
sum(power(Bedrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
group by Neighbourhood, Room_type
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
order by Neighbourhood,Room_type
/*
Neighbourhood Room_type Pearson's R
Bronx Entire home/apt -0.0401065219845552
Bronx Private room -0.0547417473532595
Brooklyn Entire home/apt -0.00632190668507133
Brooklyn Hotel room -0.192524380444772
Brooklyn Private room 0.0187287904572153
Manhattan Entire home/apt -0.00780423268295889
Manhattan Hotel room 0.000997277138749698
Manhattan Private room 0.0219087848092802
Queens Entire home/apt -0.0334220989052496
Queens Private room 0.0114437626131096
Staten Island Entire home/apt -0.0733728700814781
Staten Island Private room -0.0163007555423255
*/
--Checking Correlation of bathrooms and ratings using Pearson's formula
-----------------------------------------------------------------------
select n, Sx,Sy,Sxy,Sx2,Sy2,(n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from
(
select
count(*) as n,
sum(Bathrooms) as Sx,
sum(Score) as Sy,
sum(Bathrooms*Score) as Sxy,
sum(power(Bathrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
--------------------------------------------------------------------------------------------
--Overall there was a VERY WEAK POSITIVE CORRELATION between MORE BATHROOMS & HIGH REVIEWS |
--------------------------------------------------------------------------------------------
--Displaying correlation Neighbourhood wise
select Neighbourhood, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Neighbourhood,
count(*) as n,
sum(Bathrooms) as Sx,
sum(Score) as Sy,
sum(Bathrooms*Score) as Sxy,
sum(power(Bathrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
group by Neighbourhood
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
--------------------------------------------------------------------------------------------------------------
--There was a WEAK POSITIVE CORRELATION between MORE BATHROOMS & HIGH REVIEWS for Bronx, Manhattan, & Queens |
--There was a WEAK NEGATIVE CORRELATION between MORE BATHROOMS & HIGH REVIEWS for Brooklyn & Staten Island |
--------------------------------------------------------------------------------------------------------------
--Displaying correlation Room type type wise
select Room_type, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Room_type,
count(*) as n,
sum(Bathrooms) as Sx,
sum(Score) as Sy,
sum(Bathrooms*Score) as Sxy,
sum(power(Bathrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
and Score>0
group by Room_type
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
----------------------------------------------------------------------------------------------------------
--There was a WEAK POSITIVE CORRELATION between MORE BATHROOMS & HIGH REVIEWS for Hotels & Entire Homes |
--There was a WEAK NEGATIVE CORRELATION between MORE BATHROOMS & HIGH REVIEWS for Shared & Private Rooms |
----------------------------------------------------------------------------------------------------------
----Displaying correlation Room type type wise in Neighbourhoods for better insights
select Neighbourhood, Room_type, (n*Sxy-Sx*Sy)/sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2))) as R from --n, Sx,Sy,Sxy,Sx2,Sy2,
(
select
Neighbourhood,Room_type,
count(*) as n,
sum(Bedrooms) as Sx,
sum(Score) as Sy,
sum(Bedrooms*Score) as Sxy,
sum(power(Bedrooms,2)) as Sx2,
sum(power(Score,2)) as Sy2
from Review_table2
where Score is not null
and Price>0
group by Neighbourhood, Room_type
) as t
where sqrt((n*Sx2-power(Sx,2))*(n*Sy2-power(Sy,2)))>0 --to prevent divide by zero error
order by Neighbourhood,Room_type
/*
Neighbourhood Room_type Pearson's R
Bronx Entire home/apt -0.0401065219845552
Bronx Private room -0.0547417473532595
Brooklyn Entire home/apt -0.00632190668507133
Brooklyn Hotel room -0.192524380444772
Brooklyn Private room 0.0187287904572153
Manhattan Entire home/apt -0.00780423268295889
Manhattan Hotel room 0.000997277138749698
Manhattan Private room 0.0219087848092802
Queens Entire home/apt -0.0334220989052496
Queens Private room 0.0114437626131096
Staten Island Entire home/apt -0.0733728700814781
Staten Island Private room -0.0163007555423255
*/
--Looking at super hosts and non super hosts
--Using CASE STATEMENTS
--------------------------------------------
select * from Review_table2
select Superhost, round(AVG(cast(replace(substring(t.price,2,10),',','') as float)),2) as AvgPrice,
concat(min(rt2.price),' - ',max(rt2.price) ) as Range,
avg(Score) as Score, avg(Score_Accuracy) as ScoreAccuracy,avg(Cleanliness)as Cleanliness,avg(Checkin) as Checkin,
avg(Communication) as Communication, avg(Location) as Location, avg(Value) as Value
from(
select * ,
case
when host_is_superhost='f' then 0
else 1
end as Superhost
from DataAnalysis..['listings New York$']
) as t
inner join Review_table2 as rt2 on t.name=rt2.Name
where rt2.Price>0
and Reviews>10
and Score is not null
group by Superhost
/*
Superhost AvgPrice Range Score ScoreAccuracy Cleanliness Checkin Communication Location Value
0 163.49 10 - 10000 4.68101149176063 4.76159366869037 4.63780680832609 4.83250758889851 4.82775477016478 4.7380279705117 4.66666413703382
1 163.85 10 - 2943 4.83656661562021 4.86482542113321 4.81671822358346 4.90932618683 4.91292189892801 4.82120673813168 4.79342725880552
*/
------------------------------------------------------------------------------------------------
--Superhosts provide better services in all aspects at a similar price on an average |
--Non Superhosts should improve CLEANLINESS if they want to make it competitive for Superhosts |
------------------------------------------------------------------------------------------------
--Room type wise
--Checking range and average of ratings based on Cleanliness
select Room_type, concat(min(Cleanliness),' - ', max(Cleanliness)) as Range_of_ratings, AVG(Cleanliness) as Aerage_ratings from Review_table1
where Score is not null
and Price>0
and Reviews>10 --for reliable scores
--and Reviews>0
group by Room_type
/*
Room_type Range_of_ratings Aerage_ratings
Hotel room 3.62 - 5 4.59337662337662
Shared room 3.16 - 5 4.62155555555556
Private room 2 - 5 4.67910790301683
Entire home/apt 2.95 - 5 4.7247643259585
*/
--Neighbourhood wise
--Checking range and average of ratings
select Neighbourhood, concat(min(Cleanliness),' - ', max(Cleanliness)) as Range_of_ratings, AVG(Cleanliness) as Aerage_ratings from Review_table1
where Score is not null
and Price>0
and Reviews>10 --for reliable scores
--and Reviews>0
group by Neighbourhood
/*
Neighbourhood Range_of_ratings Aerage_ratings
Brooklyn 3.07 - 5 4.71807735011101
Bronx 3.74 - 5 4.74401727861771
Manhattan 2 - 5 4.665559724828
Staten Island 3.76 - 5 4.77422222222222
Queens 3.41 - 5 4.73763975155279
*/
--Looking at Response rate & Acceptance rate
select Superhost, avg(host_response_rate_num) as Avg_Response_rate, avg(host_acceptance_rate_num) as Avg_Acc_rate
from
(
select * ,
case
when host_is_superhost='f' then 0
else 1
end as Superhost,
isnull(host_response_rate,0) as host_response_rate_num,ISNULL( host_acceptance_rate,0) as host_acceptance_rate_num
from DataAnalysis..['listings New York$']
) as t
inner join Review_table2 as rt2 on t.name=rt2.Name
where rt2.Price>0
and Reviews>10
and Score is not null
group by Superhost
/*
Superhost Avg_Response_rate Avg_Acc_rate
0 0.530730702515179 0.492172593235041
1 0.810154670750384 0.764425727411948
*/
-----------------------------------------------------------------------------------------------------------------------------
--Superhosts are more responsive towards potential clients on an average; Non Superhosts should improve their response time |
--Superhosts have a higher acceptance rate; Non Superhosts should accept more frequently |
-----------------------------------------------------------------------------------------------------------------------------
--Looking at instant bookablity
select Superhost, sum(ib) as Instant_Bookable, count(ib) as Total,
concat(cast((cast(sum(ib) as float)*100/cast(COUNT(ib) as float)) as decimal(10,2)),'%') as AvgB
from
(
select * ,
case
when host_is_superhost='f' then 0
else 1
end as Superhost,
case
when instant_bookable='f' then 0
else 1
end as ib
from DataAnalysis..['listings New York$']
) as t
inner join Review_table2 as rt2 on t.name=rt2.Name
where rt2.Price>0
and Reviews>10
and Score is not null
group by Superhost
/*
Superhost Instant_Bookable Total AvgB
0 2865 9224 31.06%
1 2688 6530 41.16%
*/
--------------------------------------------------
--Superhosts are more instantly bookable by ~10% |
--------------------------------------------------
--Location of Superhosts
select neighbourhood_group_cleansed as Neighbourhood, count(*) as Totalhosts, sum(Superhost) Superhosts,
concat(cast((cast(sum(Superhost) as float)*100/cast(count(*) as float)) as decimal(10,2)),'%') as PercentSuperhosts
from
(
select * ,
case
when host_is_superhost='f' then 0
else 1
end as Superhost
from DataAnalysis..['listings New York$']
) as t
inner join Review_table2 as rt2 on t.name=rt2.Name
where rt2.Price>0
and Reviews>10
and Score is not null
group by neighbourhood_group_cleansed
order by neighbourhood_group_cleansed
/*
Neighbourhood Totalhosts Superhosts PercentSuperhosts
Bronx 518 225 43.44%
Brooklyn 5989 2368 39.54%
Manhattan 6727 2940 43.70%
Queens 2337 896 38.34%
Staten Island 183 101 55.19%
*/
--Room types owned by super hosts
select t.room_type as RoomType, count(*) as Totalhosts, sum(Superhost) Superhosts,
concat(cast((cast(sum(Superhost) as float)*100/cast(count(*) as float)) as decimal(10,2)),'%') as PercentSuperhosts
from
(
select * ,
case
when host_is_superhost='f' then 0
else 1
end as Superhost
from DataAnalysis..['listings New York$']
) as t
inner join Review_table2 as rt2 on t.name=rt2.Name
where rt2.Price>0
and Reviews>10
and Score is not null
group by t.room_type
order by t.room_type
/*
RoomType Totalhosts Superhosts PercentSuperhosts
Entire home/apt 7940 3108 39.14%
Hotel room 88 3 3.41%
Private room 7518 3380 44.96%
Shared room 208 39 18.75%
*/
--------------------------------------------------------------------------------------------------
--Very few Superhosts own Hotel rooms; They are more interested in renting Private rooms |
--Non Superhosts should try to move towards Private rooms or Entire homes for better results |
--------------------------------------------------------------------------------------------------