Analysis of Sleep Health and Lifestyle using SQL Part 2

Analysis of Sleep Health and Lifestyle using SQL Part 2

In my previous blog post, https://analyticswithamruha.hashnode.dev/analysis-of-sleep-health-and-lifestyle-using-sql-part-1 , I performed the following operations on the data stored in 'sleeptable' :

  • data cleaning

  • analysis of the Sleep Duration attribute

  • analysis of the Sleep Disorder attribute

  • analysis of the Quality of Sleep attribute

In this blog post, I will be concentrating on the analysis of stress level, physical activity level and daily steps attributes.

Analysing the Stress Level Attribute:

The stress level attribute accepts values on a scale of 1 to 10, where 1 indicates the lowest stress level and 10 indicates the highest stress level.

Discerning the average stress level of all individuals under consideration:

select avg(Stress_Level) from sleeptable;

The result is :

5.73

The stress levels of all individuals under consideration tends towards higher stress level by a small margin.

Checking the average stress level for each occupation:

select Occupation,avg(Stress_Level) from sleeptable group by Occupation order by avg(Stress_Level) asc;

The result is :

Accountant|4.12903225806452

Engineer|4.30769230769231

Teacher|5.0625

Lawyer|5.06382978723404

Nurse|6.0

Software Engineer|6.0

Salesperson|7.0

Scientist|7.0

Doctor|7.01538461538462

Sales Representative|8.0

The average stress level for accountants and engineers is the least and it is highest for doctors and sales representatives.

Determining the average stress level for each gender:

select Gender,avg(Stress_Level) from sleeptable group by Gender order by avg(Stress_Level) desc;

The result is:

Male|6.11643835616438

Female|4.68518518518519

The male individuals under consideration have a higher average stress level as compared to females.

Checking the average stress level for each age:

select Age,avg(Stress_Level) from sleeptable group by Age order by avg(Stress_Level) desc;

The result is:

34|8.0

28|8.0

33|7.69230769230769

29|7.15384615384615

32|6.94117647058824

31|6.22222222222222

43|6.21428571428571

30|6.15384615384615

27|6.0

42|5.66666666666667

40|5.5

41|5.25

39|4.93333333333333

38|4.6

36|4.5

35|4.41666666666667

37|4.25

People aged 34 and 28 years have the highest average stress levels and people aged 37 have the lowest average stress levels.

Determining the average stress level for each quality of sleep rating:

select Quality_of_Sleep,avg(Stress_Level) from sleeptable group by Quality_of_Sleep order by avg(Stress_Level) desc;

The result is:

4|8.0

6|7.56862745098039

5|7.0

7|5.76190476190476

8|4.54838709677419

9|3.0

Individuals having the worst quality of sleep of 4 out of 10 have the highest amount of average stress levels whereas individuals having the best quality sleep of 9 out of 10 have the least amount of stress on average.

Checking the average stress level for each BMI category:

select BMI_Category,avg(Stress_Level) from sleeptable group by BMI_Category order by avg(Stress_Level);

The output is:

Normal|5.56969696969697

Obese|6.48571428571429

Obese people have a higher stress level on average as compared to normal people.

Analysing the Physical Activity Level attribute:

The physical activity level attribute keeps track of the number of minutes devoted by an individual to physical activity each day.

Checking the average Physical Activity Level of all individuals under consideration:

select avg(Physical_Activity_Level) from sleeptable;

The result is:

57.215 (minutes per day)

Determining the Physical Activity Level for each occupation:

select Occupation,avg(Physical_Activity_Level) from sleeptable group by Occupation order by avg(Physical_Activity_Level) desc;

The result is:

Lawyer|70.4255319148936

Engineer|60.7692307692308

Accountant|60.6451612903226

Doctor|53.0769230769231

Teacher|48.4375

Software Engineer|48.0

Nurse|46.7142857142857

Salesperson|45.0

Scientist|41.0

Sales Representative|30.0

Lawyers have the highest average Physical Activity Level of 70.4 minutes each day whereas sales representatives have the least average Physical Activity Level of 30 minutes each day.

Discerning the average Physical Activity Level for each stress level:

select Stress_Level,avg(Physical_Activity_Level) from sleeptable group by Stress_Level order by avg(Physical_Activity_Level) desc;

The output is :

5|70.2040816326531

6|69.1627906976744

3|62.5

4|59.7777777777778

7|42.1428571428571

8|31.6842105263158

People with a stress level of 8 out of 10 have the least amount of average physical activity level of 31.68 minutes each day whereas people having a stress level of 5 out of 10 have the highest average physical activity level of 70.24 minutes each day.

Checking the average Physical Activity Level for each gender:

select Gender,avg(Physical_Activity_Level) from sleeptable group by Gender order by avg(Physical_Activity_Level) desc;

The output is :

Male|57.7191780821918

Female|55.8518518518519

The average physical activity level for men and women is comparatively the same.

Determining the average Physical Activity Level for each BMI Category:

select BMI_Category,avg(Physical_Activity_Level) from sleeptable group by BMI_Category order by avg(Physical_Activity_Level) desc;

The result is:

Normal|60.1515151515151

Obese|43.3714285714286

Normal people devote more time to physical activity every day than obese people.

Discerning the average Physical Activity Level for each Quality of Sleep rating:

select Quality_of_Sleep, avg(Physical_Activity_Level) from sleeptable group by Quality_of_Sleep order by avg(Physical_Activity_Level) asc;

The result is:

4|30.8

6|36.4509803921569

5|37.1428571428571

8|65.752688172043

7|68.9285714285714

9|80.0

People having the lowest quality of sleep i.e. 4 out of 10 have the least amount of average physical activity. People with the best quality of sleep(9 out of 10) have the highest average physical activity level. The better the sleep quality, the higher the physical activity level.

Analysing the Daily Steps attribute:

The daily steps attribute contains the number of steps taken by an individual each day.

Checking the ID of the individuals taking the maximum number of steps each day :

select Person_ID from sleeptable where Daily_Steps=(select max(Daily_Steps) from sleeptable);

The result is :

2

3

Determining the average number of steps taken by people every day:

select avg(Daily_Steps) from sleeptable;

The output is:

6760.0

Finding the daily steps on average for each occupation type:

select Occupation,avg(Daily_Steps) from sleeptable group by Occupation order by avg(Daily_Steps) desc ;

The result is:

Lawyer|7661.70212765957

Accountant|7051.61290322581

Doctor|6907.69230769231

Engineer|6015.38461538462

Salesperson|6000.0

Teacher|5893.75

Software Engineer|5800.0

Scientist|5350.0

Nurse|5028.57142857143

Sales Representative|3000.0

Lawyers take the highest steps every day on average whereas sales representatives take the least steps.

Checking the daily steps on average taken by people with different stress levels:

select Stress_Level,avg(Daily_Steps) from sleeptable group by Stress_Level order by avg(Daily_Steps) asc;

The output is:

7|5204.7619047619

8|5589.47368421053

4|6684.44444444444

6|7458.13953488372

5|7710.20408163265

3|7750.0

People with a stress level of 7 out of 10 take the least amount of steps every day on average whereas people with the lowest stress level of 3 out of 10 take the most amount of steps.

Determining the daily steps on average for each gender:

select Gender,avg(Daily_Steps) from sleeptable group by Gender order by avg(Daily_Steps) ;

The result is:

Female|6425.92592592593

Male|6883.56164383562

Males take a higher number of steps daily on average as compared to females.

Finding the daily steps on average for each quality of sleep rating:

select Quality_of_Sleep,avg(Daily_Steps) from sleeptable group by Quality_of_Sleep order by avg(Daily_Steps) desc;

The output is:

9|7500.0

8|7382.79569892473

7|7364.28571428571

6|5723.52941176471

5|4257.14285714286

4|3880.0

People with the best quality of sleep take the most number of steps each day on average as compared to the people with the worst quality of sleep.


Found this article interesting? Give it a like and share it with your friends !!

#datascience #data #dataanalysis #datanalytics #sleep #health #lifestyle #sleephealth #sql #datacleaning #insight #insights