-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql_code.sql
More file actions
1755 lines (1598 loc) · 66.1 KB
/
Copy pathmysql_code.sql
File metadata and controls
1755 lines (1598 loc) · 66.1 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
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
-- ============================
-- Query 1
-- ============================
DROP TABLE IF EXISTS students
-- ============================
-- Query 2
-- ============================
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
inter_dom VARCHAR(10),
region VARCHAR(50),
stay INT,
todep DECIMAL(5,2),
tosc DECIMAL(5,2),
toas DECIMAL(5,2),
INDEX idx_inter_dom (inter_dom),
INDEX idx_stay (stay)
)
-- ============================
-- Query 3
-- ============================
-- FIRST NORMAL FORM (1NF): Eliminate repeating groups and ensure atomic values
-- Current students table is already in 1NF, but let's create an enhanced version
CREATE TABLE IF NOT EXISTS students_1nf (
student_id INT AUTO_INCREMENT PRIMARY KEY,
student_name VARCHAR(100) NOT NULL,
email VARCHAR(150) UNIQUE NOT NULL,
enrollment_date DATE NOT NULL,
graduation_date DATE NULL,
status ENUM('Active', 'Graduated', 'Withdrawn') DEFAULT 'Active',
stay_duration_years INT NOT NULL,
student_type ENUM('International', 'Domestic') NOT NULL,
home_country VARCHAR(100) NOT NULL,
program_name VARCHAR(150) NOT NULL,
program_level ENUM('Undergraduate', 'Graduate', 'PhD') NOT NULL,
faculty VARCHAR(100) NOT NULL,
advisor_name VARCHAR(100) NOT NULL,
advisor_email VARCHAR(150) NOT NULL,
advisor_department VARCHAR(100) NOT NULL,
phq9_score DECIMAL(4,2) NOT NULL CHECK (phq9_score >= 0 AND phq9_score <= 27),
scs_score DECIMAL(4,2) NOT NULL CHECK (scs_score >= 8 AND scs_score <= 48),
anxiety_score DECIMAL(4,2) NOT NULL CHECK (anxiety_score >= 0 AND anxiety_score <= 80),
assessment_date DATE NOT NULL,
assessment_semester VARCHAR(20) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
-- Constraints
INDEX idx_student_type (student_type),
INDEX idx_stay_duration (stay_duration_years),
INDEX idx_assessment_date (assessment_date),
INDEX idx_program (program_name, program_level)
);
-- ============================
-- Query 4
-- ============================
-- Comprehensive Data Quality Assessment for Student Wellbeing Analysis
SELECT
'Data Completeness Analysis' as assessment_type,
COUNT(*) as total_records,
COUNT(CASE WHEN stay IS NOT NULL THEN 1 END) as stay_complete,
COUNT(CASE WHEN todep IS NOT NULL THEN 1 END) as phq_complete,
COUNT(CASE WHEN tosc IS NOT NULL THEN 1 END) as scs_complete,
COUNT(CASE WHEN toas IS NOT NULL THEN 1 END) as anxiety_complete,
COUNT(CASE WHEN inter_dom IS NOT NULL THEN 1 END) as status_complete,
ROUND(
(COUNT(CASE WHEN stay IS NOT NULL AND todep IS NOT NULL
AND tosc IS NOT NULL AND toas IS NOT NULL
AND inter_dom IS NOT NULL THEN 1 END) * 100.0 / COUNT(*)), 2
) as overall_completeness_pct
FROM students
UNION ALL
SELECT
'Data Range Validation' as assessment_type,
COUNT(*) as total_records,
COUNT(CASE WHEN stay BETWEEN 1 AND 10 THEN 1 END) as valid_stay_range,
COUNT(CASE WHEN todep BETWEEN 0 AND 50 THEN 1 END) as valid_phq_range,
COUNT(CASE WHEN tosc BETWEEN 0 AND 100 THEN 1 END) as valid_scs_range,
COUNT(CASE WHEN toas BETWEEN 0 AND 100 THEN 1 END) as valid_anxiety_range,
COUNT(CASE WHEN inter_dom IN ('Inter', 'Dom') THEN 1 END) as valid_status,
ROUND(
(COUNT(CASE WHEN stay BETWEEN 1 AND 10 AND todep BETWEEN 0 AND 50
AND tosc BETWEEN 0 AND 100 AND toas BETWEEN 0 AND 100
AND inter_dom IN ('Inter', 'Dom') THEN 1 END) * 100.0 / COUNT(*)), 2
) as overall_validity_pct
FROM students;
-- ============================
-- Query 5
-- ============================
-- Advanced Demographic Segmentation Analysis with Statistical Functions
WITH demographic_stats AS (
SELECT
inter_dom as student_status,
COUNT(*) as population_size,
ROUND(AVG(todep), 2) as avg_depression_score,
ROUND(AVG(tosc), 2) as avg_social_connectedness,
ROUND(AVG(toas), 2) as avg_anxiety_score,
ROUND(STDDEV(todep), 2) as stddev_depression,
ROUND(STDDEV(tosc), 2) as stddev_social,
ROUND(STDDEV(toas), 2) as stddev_anxiety,
ROUND(MIN(todep), 2) as min_depression,
ROUND(MAX(todep), 2) as max_depression,
ROUND(MIN(tosc), 2) as min_social,
ROUND(MAX(tosc), 2) as max_social,
ROUND(MIN(toas), 2) as min_anxiety,
ROUND(MAX(toas), 2) as max_anxiety
FROM students
GROUP BY inter_dom
),
percentile_analysis AS (
SELECT
inter_dom as student_status,
ROUND(AVG(CASE WHEN depression_percentile <= 25 THEN todep END), 2) as q1_depression,
ROUND(AVG(CASE WHEN depression_percentile >= 50 AND depression_percentile <= 50 THEN todep END), 2) as median_depression,
ROUND(AVG(CASE WHEN depression_percentile >= 75 THEN todep END), 2) as q3_depression,
ROUND(AVG(CASE WHEN social_percentile <= 25 THEN tosc END), 2) as q1_social,
ROUND(AVG(CASE WHEN social_percentile >= 50 AND social_percentile <= 50 THEN tosc END), 2) as median_social,
ROUND(AVG(CASE WHEN social_percentile >= 75 THEN tosc END), 2) as q3_social,
ROUND(AVG(CASE WHEN anxiety_percentile <= 25 THEN toas END), 2) as q1_anxiety,
ROUND(AVG(CASE WHEN anxiety_percentile >= 50 AND anxiety_percentile <= 50 THEN toas END), 2) as median_anxiety,
ROUND(AVG(CASE WHEN anxiety_percentile >= 75 THEN toas END), 2) as q3_anxiety
FROM (
SELECT
inter_dom,
todep,
tosc,
toas,
PERCENT_RANK() OVER (PARTITION BY inter_dom ORDER BY todep) * 100 as depression_percentile,
PERCENT_RANK() OVER (PARTITION BY inter_dom ORDER BY tosc) * 100 as social_percentile,
PERCENT_RANK() OVER (PARTITION BY inter_dom ORDER BY toas) * 100 as anxiety_percentile
FROM students
) ranked_data
GROUP BY inter_dom)
SELECT
ds.student_status,
ds.population_size,
ROUND((ds.population_size * 100.0 / SUM(ds.population_size) OVER()), 2) as population_percentage,
ds.avg_depression_score,
pa.q1_depression,
pa.median_depression,
pa.q3_depression,
ds.stddev_depression,
ds.avg_social_connectedness,
pa.q1_social,
pa.median_social,
pa.q3_social,
ds.stddev_social,
ds.avg_anxiety_score,
pa.q1_anxiety,
pa.median_anxiety,
pa.q3_anxiety,
ds.stddev_anxiety,
-- Risk indicator flags
CASE
WHEN ds.avg_depression_score > 15 THEN 'HIGH_RISK'
WHEN ds.avg_depression_score > 10 THEN 'MODERATE_RISK'
ELSE 'LOW_RISK'
END as depression_risk_level,
CASE
WHEN ds.avg_anxiety_score > 50 THEN 'HIGH_ANXIETY'
WHEN ds.avg_anxiety_score > 35 THEN 'MODERATE_ANXIETY'
ELSE 'LOW_ANXIETY'
END as anxiety_risk_level
FROM demographic_stats as ds
JOIN percentile_analysis as pa ON ds.student_status = pa.student_status
ORDER BY ds.population_size DESC;
-- ============================
-- Query 6
-- ============================
-- SECTION 4.1: Stay Duration Segmentation Analysis
-- CTE: duration_segments
WITH duration_segments AS (
SELECT
CASE
WHEN stay BETWEEN 1 AND 2 THEN 'Short-stay (1-2yr)'
WHEN stay BETWEEN 3 AND 4 THEN 'Medium-stay (3-4yr)'
WHEN stay >= 5 THEN 'Long-stay (5+yr)'
END as duration_segment,
stay,
COUNT(*) as student_count,
COUNT(CASE WHEN inter_dom = 'Inter' THEN 1 END) as intl_count,
COUNT(CASE WHEN inter_dom = 'Dom' THEN 1 END) as domestic_count,
ROUND(AVG(todep), 2) as avg_depression,
ROUND(AVG(tosc), 2) as avg_social,
ROUND(AVG(toas), 2) as avg_anxiety,
ROUND(MIN(todep), 2) as min_dep,
ROUND(MAX(todep), 2) as max_dep,
ROUND(STDDEV(tosc), 2) as social_variance,
ROUND(STDDEV(toas), 2) as anxiety_variance
FROM students
WHERE inter_dom = 'Inter'
GROUP BY duration_segment, stay
),
-- CTE: segment_risks
segment_risks AS (
SELECT
duration_segment,
student_count,
intl_count,
domestic_count,
ROUND((intl_count * 100.0 / SUM(intl_count) OVER()), 1) as segment_percentage,
avg_depression,
avg_social,
avg_anxiety,
CASE
WHEN avg_depression > 18 THEN 'CRITICAL'
WHEN avg_depression > 14 THEN 'HIGH'
WHEN avg_depression > 10 THEN 'MODERATE'
ELSE 'LOW'
END as depression_risk,
CASE
WHEN avg_anxiety > 65 THEN 'CRITICAL'
WHEN avg_anxiety > 50 THEN 'HIGH'
WHEN avg_anxiety > 35 THEN 'MODERATE'
ELSE 'LOW'
END as anxiety_risk,
CASE
WHEN avg_social < 20 THEN 'CRITICAL_ISOLATION'
WHEN avg_social < 35 THEN 'HIGH_ISOLATION'
WHEN avg_social < 50 THEN 'MODERATE_ISOLATION'
ELSE 'GOOD_CONNECTEDNESS'
END as social_connectedness_level,
social_variance,
anxiety_variance
FROM duration_segments
)
-- To view the combination of the both CTE
-- select *
-- from segment_risks
-- Anayisis to view the Stay Duration Segmentation Analysis
SELECT
duration_segment,
student_count,
segment_percentage,
ROUND(intl_count * 100.0 / student_count, 1) as intl_percentage,
avg_depression,
depression_risk,
avg_anxiety,
anxiety_risk,
avg_social,
social_connectedness_level,
ROUND(social_variance, 2) as social_variance,
ROUND(anxiety_variance, 2) as anxiety_variance,
CASE
WHEN depression_risk = 'CRITICAL' OR anxiety_risk = 'CRITICAL' THEN 'URGENT_SUPPORT'
WHEN depression_risk IN ('HIGH', 'MODERATE') OR anxiety_risk IN ('HIGH', 'MODERATE') THEN 'PRIORITY_SUPPORT'
ELSE 'STANDARD_SUPPORT'
END as recommended_support_level
FROM segment_risks
ORDER BY
CASE
WHEN duration_segment = 'Short-stay (1-2yr)' THEN 1
WHEN duration_segment = 'Medium-stay (3-4yr)' THEN 2
WHEN duration_segment = 'Long-stay (5+yr)' THEN 3
END;
-- ============================
-- Query 7
-- ============================
-- SECTION 4.2: International vs Domestic Comparative Studies
-- CTE: population_stats
WITH population_stats AS (
SELECT
inter_dom as student_population,
COUNT(*) as n,
COUNT(DISTINCT region) as distinct_regions,
ROUND(AVG(stay), 2) as avg_stay_years,
ROUND(MIN(stay), 0) as min_stay,
ROUND(MAX(stay), 0) as max_stay,
-- Depression metrics
ROUND(AVG(todep), 2) as depression_mean,
ROUND(STDDEV(todep), 2) as depression_sd,
ROUND(MIN(todep), 2) as depression_min,
ROUND(MAX(todep), 2) as depression_max,
-- Social Connectedness metrics
ROUND(AVG(tosc), 2) as social_mean,
ROUND(STDDEV(tosc), 2) as social_sd,
ROUND(MIN(tosc), 2) as social_min,
ROUND(MAX(tosc), 2) as social_max,
-- Anxiety metrics
ROUND(AVG(toas), 2) as anxiety_mean,
ROUND(STDDEV(toas), 2) as anxiety_sd,
ROUND(MIN(toas), 2) as anxiety_min,
ROUND(MAX(toas), 2) as anxiety_max
FROM students
GROUP BY inter_dom
),
-- CTE: risk_prevalence
risk_prevalence AS (
SELECT
inter_dom,
COUNT(*) as n,
COUNT(CASE WHEN todep >= 15 THEN 1 END) as high_depression_count,
ROUND(COUNT(CASE WHEN todep >= 15 THEN 1 END) * 100.0 / COUNT(*), 1) as high_depression_pct,
COUNT(CASE WHEN toas >= 50 THEN 1 END) as high_anxiety_count,
ROUND(COUNT(CASE WHEN toas >= 50 THEN 1 END) * 100.0 / COUNT(*), 1) as high_anxiety_pct,
COUNT(CASE WHEN tosc < 40 THEN 1 END) as low_social_count,
ROUND(COUNT(CASE WHEN tosc < 40 THEN 1 END) * 100.0 / COUNT(*), 1) as low_social_pct,
COUNT(CASE WHEN todep >= 15 AND toas >= 50 THEN 1 END) as comorbid_count,
ROUND(COUNT(CASE WHEN todep >= 15 AND toas >= 50 THEN 1 END) * 100.0 / COUNT(*), 1) as comorbid_pct
FROM students
GROUP BY inter_dom
)
-- ANnalysis of International vs Domestic Comparative Studies
SELECT
ps.student_population,
ps.n as total_population,
ROUND(ps.n * 100.0 / SUM(ps.n) OVER(), 1) as population_pct,
ps.distinct_regions,
ps.avg_stay_years,
CONCAT(ps.min_stay, '-', ps.max_stay, ' yrs') as stay_range,
CONCAT(ps.depression_mean, ' ± ', ps.depression_sd) as depression_summary,
CONCAT(ps.depression_min, '-', ps.depression_max) as depression_range,
ps.depression_mean as depression_mean_numeric,
CONCAT(ps.anxiety_mean, ' ± ', ps.anxiety_sd) as anxiety_summary,
CONCAT(ps.anxiety_min, '-', ps.anxiety_max) as anxiety_range,
ps.anxiety_mean as anxiety_mean_numeric,
CONCAT(ps.social_mean, ' ± ', ps.social_sd) as social_summary,
CONCAT(ps.social_min, '-', ps.social_max) as social_range,
ps.social_mean as social_mean_numeric,
rp.high_depression_pct,
rp.high_anxiety_pct,
rp.low_social_pct,
rp.comorbid_pct,
CASE
WHEN ps.depression_mean > 14 AND ps.anxiety_mean > 50 THEN 'HIGH_BURDEN'
WHEN ps.depression_mean > 12 OR ps.anxiety_mean > 45 THEN 'MODERATE_BURDEN'
ELSE 'LOW_BURDEN'
END as overall_burden_level
FROM population_stats as ps
JOIN risk_prevalence rp ON ps.student_population = rp.inter_dom
ORDER BY
CASE WHEN ps.student_population = 'Inter' THEN 1 ELSE 2 END;
-- ============================
-- Query 8
-- ============================
-- SECTION 4.3: Mental Health Metric Scoring & Risk Stratification
WITH normalized_scores AS (
SELECT
id,
inter_dom,
stay,
region,
todep,
tosc,
toas,
-- Normalize depression to 0-100 scale (PHQ-9: 0-50 -> 0-100)
ROUND((todep / 50.0) * 100, 1) as depression_normalized,
-- Social connectedness already 0-100
tosc as social_normalized,
-- Anxiety already 0-100
toas as anxiety_normalized,
-- Depression risk component (higher = worse)
CASE
WHEN todep >= 20 THEN 4
WHEN todep >= 15 THEN 3
WHEN todep >= 10 THEN 2
WHEN todep >= 5 THEN 1
ELSE 0
END as depression_risk_score,
-- Anxiety risk component (higher = worse)
CASE
WHEN toas >= 75 THEN 4
WHEN toas >= 50 THEN 3
WHEN toas >= 35 THEN 2
WHEN toas >= 20 THEN 1
ELSE 0
END as anxiety_risk_score,
-- Social connectedness protective component (higher = better)
CASE
WHEN tosc >= 75 THEN 4
WHEN tosc >= 50 THEN 3
WHEN tosc >= 35 THEN 2
WHEN tosc >= 20 THEN 1
ELSE 0
END as social_protection_score
FROM students
WHERE inter_dom = 'Inter'
),
composite_scores AS (
SELECT
id,
inter_dom,
stay,
region,
todep,
tosc,
toas,
depression_normalized,
social_normalized,
anxiety_normalized,
-- Composite risk score (0-8, higher = more at risk)
(depression_risk_score + anxiety_risk_score - social_protection_score) as composite_risk_score,
-- Overall wellbeing score (0-100, higher = better)
ROUND((100 - (depression_normalized * 0.3 + anxiety_normalized * 0.35 + (100 - social_normalized) * 0.35)), 1) as wellbeing_score,
CASE
WHEN (depression_risk_score + anxiety_risk_score) >= 6 OR social_protection_score <= 1 THEN 'CRITICAL'
WHEN (depression_risk_score + anxiety_risk_score) >= 4 OR social_protection_score = 2 THEN 'HIGH'
WHEN (depression_risk_score + anxiety_risk_score) >= 2 THEN 'MODERATE'
ELSE 'LOW'
END as risk_level
FROM normalized_scores
)
SELECT
inter_dom,
COUNT(*) as n,
ROUND(AVG(wellbeing_score), 2) as avg_wellbeing_score,
ROUND(STDDEV(wellbeing_score), 2) as wellbeing_sd,
COUNT(CASE WHEN risk_level = 'CRITICAL' THEN 1 END) as critical_count,
ROUND(COUNT(CASE WHEN risk_level = 'CRITICAL' THEN 1 END) * 100.0 / COUNT(*), 1) as critical_pct,
COUNT(CASE WHEN risk_level = 'HIGH' THEN 1 END) as high_count,
ROUND(COUNT(CASE WHEN risk_level = 'HIGH' THEN 1 END) * 100.0 / COUNT(*), 1) as high_pct,
COUNT(CASE WHEN risk_level = 'MODERATE' THEN 1 END) as moderate_count,
ROUND(COUNT(CASE WHEN risk_level = 'MODERATE' THEN 1 END) * 100.0 / COUNT(*), 1) as moderate_pct,
COUNT(CASE WHEN risk_level = 'LOW' THEN 1 END) as low_count,
ROUND(COUNT(CASE WHEN risk_level = 'LOW' THEN 1 END) * 100.0 / COUNT(*), 1) as low_pct,
-- Overall risk burden
ROUND((COUNT(CASE WHEN risk_level IN ('CRITICAL', 'HIGH') THEN 1 END) * 100.0 / COUNT(*)), 1) as at_risk_pct,
-- Average composite risk
ROUND(AVG(composite_risk_score), 2) as avg_composite_risk,
CASE
WHEN AVG(composite_risk_score) >= 4 THEN 'HIGH_BURDEN'
WHEN AVG(composite_risk_score) >= 2 THEN 'MODERATE_BURDEN'
ELSE 'LOW_BURDEN'
END as population_burden_level
FROM composite_scores
GROUP BY inter_dom;
-- ============================
-- Query 9
-- ============================
-- SECTION 4.4: Cross-Cultural Wellbeing Patterns
WITH regional_profiles AS (
SELECT
region,
COUNT(*) as n,
COUNT(CASE WHEN inter_dom = 'Inter' THEN 1 END) as intl_count,
ROUND(AVG(stay), 2) as avg_stay,
ROUND(AVG(todep), 2) as depression_mean,
ROUND(STDDEV(todep), 2) as depression_sd,
COUNT(CASE WHEN todep >= 15 THEN 1 END) as elevated_depression,
ROUND(COUNT(CASE WHEN todep >= 15 THEN 1 END) * 100.0 / COUNT(*), 1) as depression_prevalence_pct,
ROUND(AVG(toas), 2) as anxiety_mean,
ROUND(STDDEV(toas), 2) as anxiety_sd,
COUNT(CASE WHEN toas >= 50 THEN 1 END) as elevated_anxiety,
ROUND(COUNT(CASE WHEN toas >= 50 THEN 1 END) * 100.0 / COUNT(*), 1) as anxiety_prevalence_pct,
ROUND(AVG(tosc), 2) as social_mean,
ROUND(STDDEV(tosc), 2) as social_sd,
COUNT(CASE WHEN tosc < 40 THEN 1 END) as low_social,
ROUND(COUNT(CASE WHEN tosc < 40 THEN 1 END) * 100.0 / COUNT(*), 1) as isolation_prevalence_pct
FROM students
WHERE inter_dom = 'Inter'
GROUP BY region
)
SELECT
region,
n as region_student_count,
ROUND(n * 100.0 / SUM(n) OVER(), 1) as region_pct_of_intl,
avg_stay as avg_years_enrolled,
-- Depression profile
depression_mean,
CONCAT(depression_mean, ' ± ', depression_sd) as depression_profile,
depression_prevalence_pct as depression_at_risk_pct,
-- Anxiety profile
anxiety_mean,
CONCAT(anxiety_mean, ' ± ', anxiety_sd) as anxiety_profile,
anxiety_prevalence_pct as anxiety_at_risk_pct,
-- Social connectedness profile
social_mean,
CONCAT(social_mean, ' ± ', social_sd) as social_profile,
isolation_prevalence_pct as isolation_pct,
-- Overall risk classification
CASE
WHEN depression_prevalence_pct > 50 AND anxiety_prevalence_pct > 40 THEN 'HIGH_RISK_CULTURE'
WHEN depression_prevalence_pct > 40 OR anxiety_prevalence_pct > 35 THEN 'MODERATE_RISK_CULTURE'
ELSE 'STANDARD_RISK_CULTURE'
END as cultural_risk_profile,
-- Adaptation indicators
CASE
WHEN avg_stay > 4 AND social_mean > 50 THEN 'STRONG_ADAPTATION'
WHEN avg_stay > 4 AND social_mean <= 50 THEN 'MIXED_ADAPTATION'
WHEN avg_stay <= 3 THEN 'EARLY_PHASE'
ELSE 'DEVELOPING_ADAPTATION'
END as adaptation_trajectory,
-- Support need intensity
ROUND((depression_prevalence_pct + anxiety_prevalence_pct + isolation_prevalence_pct) / 3, 1) as avg_risk_prevalence
FROM regional_profiles
ORDER BY n DESC;
-- ============================
-- Query 10
-- ============================
-- SECTION 4.5: Duration-Based Risk Prediction Models
WITH risk_factors AS (
SELECT
CASE
WHEN stay BETWEEN 1 AND 2 THEN 'Phase1_0-2yr'
WHEN stay BETWEEN 3 AND 4 THEN 'Phase2_3-4yr'
WHEN stay >= 5 THEN 'Phase3_5+yr'
END as enrollment_phase,
stay,
region,
COUNT(*) as n,
COUNT(CASE WHEN todep >= 15 THEN 1 END) as depression_cases,
COUNT(CASE WHEN toas >= 50 THEN 1 END) as anxiety_cases,
COUNT(CASE WHEN tosc < 40 THEN 1 END) as isolation_cases,
COUNT(CASE WHEN (todep >= 15 AND toas >= 50) THEN 1 END) as comorbid_cases,
ROUND(AVG(CASE WHEN todep >= 15 THEN 1 ELSE 0 END) * 100, 1) as depression_risk_rate,
ROUND(AVG(CASE WHEN toas >= 50 THEN 1 ELSE 0 END) * 100, 1) as anxiety_risk_rate,
ROUND(AVG(CASE WHEN tosc < 40 THEN 1 ELSE 0 END) * 100, 1) as isolation_rate,
-- Risk score (weighted combination)
ROUND(
(COUNT(CASE WHEN todep >= 15 THEN 1 END) * 0.3 +
COUNT(CASE WHEN toas >= 50 THEN 1 END) * 0.35 +
COUNT(CASE WHEN tosc < 40 THEN 1 END) * 0.35) * 100.0 / COUNT(*),
1
) as overall_risk_index
FROM students
WHERE inter_dom = 'Inter'
GROUP BY enrollment_phase, stay, region
),
risk_tiers AS (
SELECT
enrollment_phase,
stay,
region,
n,
depression_risk_rate,
anxiety_risk_rate,
isolation_rate,
overall_risk_index,
CASE
WHEN overall_risk_index >= 50 THEN 'TIER1_CRITICAL'
WHEN overall_risk_index >= 35 THEN 'TIER2_HIGH'
WHEN overall_risk_index >= 20 THEN 'TIER3_MODERATE'
ELSE 'TIER4_LOW'
END as risk_tier,
CASE
WHEN stay <= 1 THEN 'EARLY_INTERVENTION_PRIORITY'
WHEN stay BETWEEN 2 AND 3 THEN 'ADAPTATION_SUPPORT'
WHEN stay BETWEEN 4 AND 5 THEN 'INTEGRATION_SUPPORT'
ELSE 'MAINTENANCE_SUPPORT'
END as support_type,
-- Predictive probability of any mental health concern
ROUND((anxiety_risk_rate + depression_risk_rate) / 2, 1) as mental_health_concern_prob,
-- Social isolation as protective factor concern
isolation_rate as social_isolation_prob
FROM risk_factors
)
SELECT
enrollment_phase,
stay as stay_years,
region,
n as cohort_size,
risk_tier,
support_type,
ROUND(depression_risk_rate, 1) as depression_risk_pct,
ROUND(anxiety_risk_rate, 1) as anxiety_risk_pct,
ROUND(isolation_rate, 1) as isolation_pct,
ROUND(overall_risk_index, 1) as risk_index_0_100,
mental_health_concern_prob as concern_probability,
social_isolation_prob as isolation_probability,
CASE
WHEN n >= 10 THEN 'RELIABLE'
WHEN n >= 5 THEN 'MODERATE_RELIABILITY'
ELSE 'SMALL_SAMPLE'
END as prediction_reliability
FROM risk_tiers
ORDER BY
CASE
WHEN enrollment_phase = 'Phase1_0-2yr' THEN 1
WHEN enrollment_phase = 'Phase2_3-4yr' THEN 2
WHEN enrollment_phase = 'Phase3_5+yr' THEN 3
END,
overall_risk_index DESC,
stay ASC;
-- ============================
-- Query 11
-- ============================
-- Advanced Stay Duration Impact Analysis with Window Functions
WITH duration_base_stats AS (
SELECT
stay,
inter_dom,
COUNT(*) as student_count,
ROUND(AVG(todep), 2) as avg_depression,
ROUND(AVG(tosc), 2) as avg_social_connectedness,
ROUND(AVG(toas), 2) as avg_anxiety,
ROUND(STDDEV(todep), 2) as depression_volatility,
ROUND(STDDEV(tosc), 2) as social_volatility,
ROUND(STDDEV(toas), 2) as anxiety_volatility
FROM students
WHERE inter_dom = 'Inter' -- Focus on international students for this analysis
GROUP BY stay, inter_dom
),
duration_trends AS (
SELECT
*,
-- Window functions for trend analysis
LAG(avg_depression, 1) OVER (ORDER BY stay) as prev_depression,
LEAD(avg_depression, 1) OVER (ORDER BY stay) as next_depression,
LAG(avg_social_connectedness, 1) OVER (ORDER BY stay) as prev_social,
LEAD(avg_social_connectedness, 1) OVER (ORDER BY stay) as next_social,
LAG(avg_anxiety, 1) OVER (ORDER BY stay) as prev_anxiety,
LEAD(avg_anxiety, 1) OVER (ORDER BY stay) as next_anxiety,
-- Ranking functions
RANK() OVER (ORDER BY avg_depression DESC) as depression_risk_rank,
RANK() OVER (ORDER BY avg_anxiety DESC) as anxiety_risk_rank,
RANK() OVER (ORDER BY avg_social_connectedness ASC) as social_isolation_rank,
-- Percentile functions
PERCENT_RANK() OVER (ORDER BY avg_depression) as depression_percentile,
PERCENT_RANK() OVER (ORDER BY avg_anxiety) as anxiety_percentile,
PERCENT_RANK() OVER (ORDER BY avg_social_connectedness DESC) as social_percentile,
-- Moving averages for trend smoothing
ROUND(AVG(avg_depression) OVER (ORDER BY stay ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING), 2) as depression_3yr_avg,
ROUND(AVG(avg_social_connectedness) OVER (ORDER BY stay ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING), 2) as social_3yr_avg,
ROUND(AVG(avg_anxiety) OVER (ORDER BY stay ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING), 2) as anxiety_3yr_avg
FROM duration_base_stats
),
risk_classification AS (
SELECT
*,
-- Calculate change rates
CASE
WHEN prev_depression IS NOT NULL THEN
ROUND(((avg_depression - prev_depression) / prev_depression * 100), 2)
ELSE NULL
END as depression_change_rate,
CASE
WHEN prev_social IS NOT NULL THEN
ROUND(((avg_social_connectedness - prev_social) / prev_social * 100), 2)
ELSE NULL
END as social_change_rate,
CASE
WHEN prev_anxiety IS NOT NULL THEN
ROUND(((avg_anxiety - prev_anxiety) / prev_anxiety * 100), 2)
ELSE NULL
END as anxiety_change_rate,
-- Risk level classifications
CASE
WHEN depression_risk_rank <= 3 THEN 'CRITICAL'
WHEN depression_risk_rank <= 6 THEN 'HIGH'
ELSE 'MODERATE'
END as depression_risk_category,
CASE
WHEN anxiety_risk_rank <= 3 THEN 'CRITICAL'
WHEN anxiety_risk_rank <= 6 THEN 'HIGH'
ELSE 'MODERATE'
END as anxiety_risk_category,
CASE
WHEN social_isolation_rank <= 3 THEN 'SEVERE_ISOLATION'
WHEN social_isolation_rank <= 6 THEN 'MODERATE_ISOLATION'
ELSE 'CONNECTED'
END as social_connection_category
FROM duration_trends
)
SELECT
stay as years_of_stay,
student_count,
avg_depression,
depression_3yr_avg as depression_trend,
depression_change_rate as depression_change_pct,
depression_risk_category,
avg_social_connectedness,
social_3yr_avg as social_trend,
social_change_rate as social_change_pct,
social_connection_category,
avg_anxiety,
anxiety_3yr_avg as anxiety_trend,
anxiety_change_rate as anxiety_change_pct,
anxiety_risk_category,
depression_volatility,
social_volatility,
anxiety_volatility,
ROUND(depression_percentile * 100, 1) as depression_percentile_rank,
ROUND(anxiety_percentile * 100, 1) as anxiety_percentile_rank,
ROUND(social_percentile * 100, 1) as social_connectedness_percentile_rank
FROM risk_classification
ORDER BY stay;
-- ============================
-- Query 12
-- ============================
-- SECTION 5.1: Window Functions for Longitudinal Trend Analysis
WITH longitudinal_data AS (
SELECT
id,
stay,
region,
todep,
tosc,
toas,
-- Row number for identification
ROW_NUMBER() OVER (ORDER BY stay, todep DESC) as overall_rank,
ROW_NUMBER() OVER (PARTITION BY region ORDER BY todep DESC) as regional_depression_rank,
-- Ranking (handling ties)
RANK() OVER (ORDER BY toas DESC) as anxiety_rank,
DENSE_RANK() OVER (ORDER BY tosc) as social_connectivity_rank,
-- Percentile rankings
PERCENT_RANK() OVER (ORDER BY todep) as depression_percentile,
PERCENT_RANK() OVER (ORDER BY toas) as anxiety_percentile,
PERCENT_RANK() OVER (ORDER BY tosc DESC) as social_percentile,
-- LAG and LEAD for trend analysis
LAG(todep) OVER (PARTITION BY region ORDER BY stay) as prev_depression,
LEAD(todep) OVER (PARTITION BY region ORDER BY stay) as next_depression,
-- Trend detection
CASE
WHEN LAG(todep) OVER (PARTITION BY region ORDER BY stay) IS NOT NULL
AND todep > LAG(todep) OVER (PARTITION BY region ORDER BY stay)
THEN 'WORSENING'
WHEN LAG(todep) OVER (PARTITION BY region ORDER BY stay) IS NOT NULL
AND todep < LAG(todep) OVER (PARTITION BY region ORDER BY stay)
THEN 'IMPROVING'
ELSE 'STABLE'
END as depression_trend
FROM students
WHERE inter_dom = 'Inter'
),
window_analytics AS (
SELECT
stay,
region,
COUNT(*) as cohort_size,
overall_rank,
-- Top risk students
COUNT(CASE WHEN overall_rank <= 10 THEN 1 END) as top_10_risk_count,
COUNT(CASE WHEN anxiety_rank <= 20 THEN 1 END) as high_anxiety_count,
COUNT(CASE WHEN social_connectivity_rank <= 15 THEN 1 END) as low_social_count,
-- Worsening trends
COUNT(CASE WHEN depression_trend = 'WORSENING' THEN 1 END) as worsening_trend_count,
COUNT(CASE WHEN depression_trend = 'IMPROVING' THEN 1 END) as improving_trend_count,
ROUND(AVG(CASE WHEN depression_percentile >= 0.75 THEN todep END), 2) as p75_depression,
ROUND(AVG(CASE WHEN depression_percentile >= 0.50 AND depression_percentile < 0.75 THEN todep END), 2) as p50_75_depression,
ROUND(AVG(anxiety_percentile * 100), 1) as avg_anxiety_percentile
FROM longitudinal_data
GROUP BY stay, region
)
SELECT
stay,
region,
cohort_size,
top_10_risk_count,
ROUND(top_10_risk_count * 100.0 / cohort_size, 1) as top_risk_pct,
high_anxiety_count,
low_social_count,
worsening_trend_count,
improving_trend_count,
ROUND(worsening_trend_count * 100.0 / cohort_size, 1) as worsening_trend_pct,
p75_depression as severe_depression_cohort_avg,
p50_75_depression as moderate_depression_cohort_avg,
avg_anxiety_percentile
FROM window_analytics
ORDER BY stay ASC, worsening_trend_pct DESC;
-- ============================
-- Query 13
-- ============================
-- SECTION 5.2: Common Table Expressions (CTEs) for Multi-Metric Analysis
-- Hierarchical CTE structure demonstrating modular query construction
WITH base_metrics AS (
-- First CTE: Normalize all metrics to 0-100 scale
SELECT
id,
stay,
region,
todep,
tosc,
toas,
ROUND((todep / 50.0) * 100, 1) as depression_normalized,
tosc as social_normalized,
toas as anxiety_normalized
FROM students
WHERE inter_dom = 'Inter'
),
risk_components AS (
-- Second CTE: Calculate individual risk dimensions
SELECT
id,
stay,
region,
depression_normalized,
social_normalized,
anxiety_normalized,
CASE
WHEN depression_normalized >= 70 THEN 3
WHEN depression_normalized >= 50 THEN 2
WHEN depression_normalized >= 30 THEN 1
ELSE 0
END as depression_component,
CASE
WHEN anxiety_normalized >= 70 THEN 3
WHEN anxiety_normalized >= 50 THEN 2
WHEN anxiety_normalized >= 30 THEN 1
ELSE 0
END as anxiety_component,
CASE
WHEN social_normalized < 30 THEN 3
WHEN social_normalized < 50 THEN 2
WHEN social_normalized < 70 THEN 1
ELSE 0
END as isolation_component
FROM base_metrics
),
composite_scores AS (
-- Third CTE: Aggregate components into composite wellbeing score
SELECT
id,
stay,
region,
depression_normalized,
anxiety_normalized,
social_normalized,
depression_component + anxiety_component + isolation_component as composite_risk_score,
ROUND(
100 - (depression_normalized * 0.35 + anxiety_normalized * 0.35 + (100 - social_normalized) * 0.30),
1
) as wellbeing_score,
CASE
WHEN (depression_component + anxiety_component + isolation_component) >= 7 THEN 'CRITICAL'
WHEN (depression_component + anxiety_component + isolation_component) >= 5 THEN 'HIGH'
WHEN (depression_component + anxiety_component + isolation_component) >= 3 THEN 'MODERATE'
ELSE 'LOW'
END as risk_tier
FROM risk_components
),
cohort_summaries AS (
-- Fourth CTE: Summarize by stay duration and risk tier
SELECT
stay,
region,
risk_tier,
COUNT(*) as n_students,
ROUND(AVG(wellbeing_score), 2) as avg_wellbeing,
ROUND(STDDEV(wellbeing_score), 2) as wellbeing_sd,
ROUND(AVG(depression_normalized), 2) as avg_depression_norm,
ROUND(AVG(anxiety_normalized), 2) as avg_anxiety_norm,
ROUND(AVG(social_normalized), 2) as avg_social_norm,
ROUND(AVG(composite_risk_score), 2) as avg_risk_score
FROM composite_scores
GROUP BY stay, region, risk_tier
),
final_summary AS (
-- Fifth CTE: Final analytical output with rankings and comparisons
SELECT
stay,
region,
risk_tier,
n_students,
avg_wellbeing,
wellbeing_sd,
avg_depression_norm,
avg_anxiety_norm,
avg_social_norm,
avg_risk_score,
RANK() OVER (PARTITION BY stay ORDER BY n_students DESC) as regional_rank_by_size,
RANK() OVER (PARTITION BY risk_tier ORDER BY avg_risk_score DESC) as risk_intensity_rank,
ROUND(n_students * 100.0 / SUM(n_students) OVER (PARTITION BY stay), 1) as pct_of_cohort
FROM cohort_summaries
)
SELECT
stay as enrollment_year,
region,
risk_tier,
n_students,
pct_of_cohort,
regional_rank_by_size,
risk_intensity_rank,
avg_wellbeing,
wellbeing_sd,
CONCAT(avg_depression_norm, '/', avg_anxiety_norm, '/', avg_social_norm) as normalized_metrics,
avg_risk_score,
CASE
WHEN avg_risk_score >= 6 THEN 'URGENT_SUPPORT'
WHEN avg_risk_score >= 4 THEN 'PRIORITY_SUPPORT'
ELSE 'STANDARD_SUPPORT'
END as support_recommendation
FROM final_summary
ORDER BY stay ASC, risk_tier DESC;
-- ============================
-- Query 14
-- ============================
-- SECTION 5.3: Advanced Aggregations by Duration Cohorts (ROLLUP)
SELECT
COALESCE(
CASE
WHEN stay BETWEEN 1 AND 2 THEN 'Short-stay (1-2yr)'
WHEN stay BETWEEN 3 AND 4 THEN 'Medium-stay (3-4yr)'
WHEN stay >= 5 THEN 'Long-stay (5+yr)'
END,
'TOTAL'
) as duration_cohort,
COALESCE(region, 'All_Regions') as region,
COUNT(*) as n_students,
ROUND(AVG(todep), 2) as avg_depression,
ROUND(AVG(toas), 2) as avg_anxiety,
ROUND(AVG(tosc), 2) as avg_social,
ROUND(
100 - (AVG(todep)/50 * 100 * 0.35 + AVG(toas) * 0.35 + (100 - AVG(tosc)) * 0.30),
2
) as composite_wellbeing_score,
COUNT(CASE WHEN todep >= 15 THEN 1 END) as n_elevated_depression,
COUNT(CASE WHEN toas >= 50 THEN 1 END) as n_elevated_anxiety,
ROUND(COUNT(CASE WHEN todep >= 15 THEN 1 END) * 100.0 / COUNT(*), 1) as pct_depression_risk,
ROUND(COUNT(CASE WHEN toas >= 50 THEN 1 END) * 100.0 / COUNT(*), 1) as pct_anxiety_risk
FROM students
WHERE inter_dom = 'Inter'
GROUP BY
CASE
WHEN stay BETWEEN 1 AND 2 THEN 'Short-stay (1-2yr)'
WHEN stay BETWEEN 3 AND 4 THEN 'Medium-stay (3-4yr)'
WHEN stay >= 5 THEN 'Long-stay (5+yr)'
END,
region
WITH ROLLUP
ORDER BY
CASE
WHEN duration_cohort = 'Short-stay (1-2yr)' THEN 1
WHEN duration_cohort = 'Medium-stay (3-4yr)' THEN 2
WHEN duration_cohort = 'Long-stay (5+yr)' THEN 3
WHEN duration_cohort = 'TOTAL' THEN 4
END,
n_students DESC;
-- ============================
-- Query 15
-- ============================
-- SECTION 5.4: Statistical SQL Functions for Mental Health Analytics
WITH statistical_profiles AS (