Purpose: To investigate the association between insomnia and the risk of various cancers using the Korean National Health Insurance Service database.Materials and methods: Patients who underwent a national health examination in 2009 were followed-up until 2018. Newly-diagnosed cancers were collected one year after the baseline. Insomnia was defined as having a diagnosis of F510 or G470 within one year prior to enrollment. The incidence of various cancers was compared between patients with and without insomnia.Results: In the overall study population (N = 3,982,012), the risk for any type of cancer was not different between controls and insomnia patients (adjusted hazard ratio [aHR]: 0.990). However, it was different by age; insomnia increased the risk of any cancer in younger age groups (20–39y and 40–59y, aHR:1.310 and 1.139, respectively) but it significantly decreased the risk in the 60–79y age group (aHR: 0.939). In cancer type, colorectal cancer risk was lower (aHR: 0.872,P< 0.0001), whereas leukemia risk was higher (aHR: 1.402,P< 0.0001) in patients with insomnia than in those without it, regardless of sex. In men, the risk of stomach cancer was lower (aHR: 0.882,P= 0.0003), and the risks of lung (aHR:1.114,P= 0.0005), kidney (aHR 1.226,P= 0.0107), and prostate (aHR:1.101,P= 0.0028) cancers were higher in insomnia patients than in control patients. In women, insomnia patients compared to control patients showed a lower risk of ovarian cancer (aHR:0.856,P= 0.0344, respectively), while they had a higher risk of oral (aHR:1.616,P= 0.002), thyroid (aHR:1.072,P= 0.0192), and nerve (aHR: 1.251,P= 0.016) cancers.Conclusion: Insomnia is associated with an increased or decreased risk of some cancers, depending on age, cancer type and sex.