Objective: This study aims to examine the associations between various dimensions of social support and sleep quality in a hospital psychiatric outpatient population using canonical correlation analysis, while also investigating the potential mediating roles of anxiety and depressive symptoms. Methods: Employing a cross-sectional design, survey data were collected from 10,071 individuals visiting Hangzhou Seventh People’s Hospital between October 2020 and October 2023. After excluding invalid cases, a total of 10,063 participants were included for analysis. The survey encompassed gathering basic demographic information as well as administering the Social Support Rating Scale, Self-rating Depression Scale, Self-rating Anxiety Scale, and Pittsburgh Sleep Quality Index Scale. Data analysis was conducted using SPSS 27.0 statistical software with canonical correlation analysis employed to examine the relationships. Additionally, the mediating effects of anxiety and depression symptoms were examined using the SPSS PROCESS macro. Results: Canonical correlation analysis reveals a significant positive correlation between social support dimensions and various aspects of sleep quality. The first pair of canonical variables (r = 0.586, p < 0.001) accounts for an impressive 97% of the cumulative contribution. It is found that this pair of canonical variables explains 68.9% of the variance in social support and 49.0% in sleep quality. Mediation analysis revealed that anxiety and depressive symptoms significantly mediated the relationship between social support and sleep quality, accounting for 88% of the total effect. Specifically, anxiety accounted for 47% mediation while depression accounted for 41%. Conclusion: The study findings revealed a significant positive correlation between social support and sleep quality dimensions, indicating a robust association between increased social support and enhanced individual sleep quality. Furthermore, the results indicated that anxiety and depression partially mediated the relationship between social support and sleep quality, offering novel insights into the intricate connection between mental health and sleep quality.