Analysis of Sleep Health and Lifestyle using SQL Part 1

Analysis of Sleep Health and Lifestyle using SQL Part 1

Sleep forms an integral part of our lives. A good night’s sleep has the potential to reduce our stress and increase our daily productivity. In this blog post, I have analyzed the factors influencing sleep and derived valuable insights regarding various parameters such as stress level, sleep duration, BMI category and Occupation using SQL.

About the Dataset:

I have used 200 rows of the following dataset:

https://www.kaggle.com/datasets/uom190346a/sleep-health-and-lifestyle-dataset

The columns in this dataset are as follows:

  1. Person ID

  2. Gender

  3. Age

  4. Occupation

  5. Sleep Duration in hours

  6. Quality of Sleep ranging from 1 to 10

  7. Physical Activity Level in minutes per day

  8. Stress Levels ranging from 1 to 10

  9. BMI Category of the person

  10. Blood Pressure (Systolic/Diastolic)

  11. Heart rate in beats per minute

  12. Daily Steps

  13. Sleep Disorder

These rows and columns have been stored in a table which I have named as ‘sleeptable'.

Data Cleaning:

Checking the number of rows in the table:

select count(*) from sleeptable;

The output will be 200.

Checking for NULL values in the table:


select * from sleeptable where Person_ID is NULL or Gender is NULL or Age is NULL or Occupation is NULL or Sleep_Duration is NULL or Quality_of_Sleep  is NULL or Physical_Activity_Level is NULL or Stress_Level is NULL or BMI_Category is NULL or Blood_Pressure is NULL or Heart_Rate is NULL or Daily_Steps is NULL or Sleep_Disorder is NULL;

No output is given after execution of this query, hence there are no tuples having NULL values in the sleeptable.

Checking the type of values the BMI Category column is taking:

select distinct BMI_Category  from sleeptable;

The result is :

Overweight

Normal

Obese

Normal Weight

Overweight and Obese have the same meaning, hence all entries in sleeptable pertaining to Overweight are changed to Obese.

update sleeptable set BMI_Category='Obese' where BMI_Category in('Overweight','obese');

Normal and Normal Weight also have the same meaning, so all instances of Normal Weight are changed to Normal.

update sleeptable set BMI_Category='Normal' where BMI_Category is 'Normal Weight';

In order to check if the required updates are made to the dataset or not, the following query is written:

select distinct BMI_Category  from sleeptable;

The result is :

Obese

Normal

Hence the changes made to the table have been successfully implemented.

Analysing the Sleep Duration attribute:

Determining the average sleep duration for each Occupation:

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

The result of this query is :

Lawyer|7.41063829787234

Engineer|7.36923076923077

Accountant|7.21935483870968

Doctor|6.88769230769231

Teacher|6.85

Nurse|6.75714285714286

Software Engineer|6.75

Salesperson|6.47272727272727

Scientist|6.0

Sales Representative|5.9

Lawyers and Engineers have the highest amount of average sleep duration whereas Scientists and Sales Representatives have the least amount of average sleep duration.

Determining the average sleep duration for each BMI category:

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

The output is:

Normal|7.14727272727273

Obese|6.49142857142857

Normal people have a higher amount of average sleep duration than obese people.

Checking the average sleep duration for each gender:

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

The result is:

Male|7.03835616438356

Female|7.01666666666666

Males and females have almost an equal sleep duration.

Finding out the average sleep duration for each quality of sleep rating:

The quality of sleep column takes in values on a scale of 1 to 10 where 1 is for the worst quality of sleep and 10 stands for the best quality of sleep.

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

The result is :

9|8.0

7|7.54761904761904

8|7.34301075268817

5|6.5

6|6.1921568627451

4|5.86

We can infer from the above result that, for the best quality of sleep, the average sleep duration is the highest whereas, for the worst quality of sleep, the average sleep duration is the least.

Analysing the Sleep Disorder attribute:

The sleep disorder attribute has three possible values:

  • None- a person is not suffering from any sleep disorders

  • Insomnia- a person faces difficulty falling asleep

  • Sleep Apnea- a person suffers from pauses in breathing during sleep

Finding out the count of people suffering from sleep disorders:

select distinct Sleep_Disorder,count(*) from sleeptable group by Sleep_Disorder;

The result is :

Insomnia|24

None|161

Sleep Apnea|15

The majority of the individuals under consideration do not suffer from any sleep disorders.

Determining the average sleep duration for each sleep disorder:

select Sleep_Disorder,avg(Sleep_Duration) from sleeptable group by Sleep_Disorder;

The result is :

Insomnia|6.5625

None|7.14099378881988

Sleep Apnea|6.62

The people not dealing with any sleep disorders have a higher average sleep duration as compared to those having sleep disorders.

Finding the average stress level for each type of sleep disorder:

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

The result is :

None|5.54037267080745

Insomnia|6.41666666666667

Sleep Apnea|6.66666666666667

People who are not dealing with any sleep disorders have a lower average stress level as compared to those suffering from insomnia and sleep apnea.

Checking the average physical activity level for each sleep disorder:

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

The output is :

Sleep Apnea|44.6

Insomnia|45.4166666666667

None|60.1490683229814

People suffering from sleep disorders spend less time on average for physical activity than those who are not dealing with sleep disorders.

Finding out the average number of steps for each type of sleep disorder:

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

The result is:

None|7078.88198757764

Insomnia|5641.66666666667

Sleep Apnea|5126.66666666667

People with no sleep disorders take higher number of steps each day on average as compared to those people dealing with sleeping disorders.

Analysing the Quality of Sleep Attribute:

This attribute takes in values on a scale of 1 to 10 where 1 is for the worst quality of sleep and 10 stands for the best quality of sleep.

Determining the average quality of sleep for each occupation:

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

The result is :

Accountant|8.06451612903226

Lawyer|7.8936170212766

Engineer|7.76923076923077

Teacher|6.9375

Software Engineer|6.5

Doctor|6.49230769230769

Salesperson|6.0

Nurse|6.0

Scientist|5.0

Sales Representative|4.0

Accountants have the best quality of sleep on average whereas Sales Representatives have the worst quality of sleep.

Checking the average quality of sleep for each BMI category:

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

Normal|7.33939393939394

Obese|5.88571428571429

Normal people have a better quality of sleep on average as compared to obese people.

Stress level, physical activity level and daily steps attributes will be analysed in the second part of the Analysis of Sleep Health and Lifestyle blog post.


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