This is an excellent question and I am glad that you have asked this! This is one of the cool interesting facts about melanoma or skin cancer.
In regards to melanoma, the people who are at highest risk are those who have intermediate sun exposure, i.e. people who work in offices or are like me and have sweet moon tans (i.e. hardly see the sun and are as white as a ghost). This is a little surprising as you would logically think that people who are in the sun more often (such as those who work outdoors) would be at more risk since it is known that sun exposure is a key factor for causing the disease.
So, why is this? Well, the cells that melanoma starts from (called melanocytes) are the same cells that create pigment in your skin in response to UV- these are the cells that are responsible for a tan. The pigment that is produced can be considered a natural sunscreen that your body produces to protect you from damage. As you would know, those people who are in the sun more often have darker tans and this tan, to a point, protects their DNA from damage caused by the sun. This is not to say that people with tans are not still susceptible to getting melanoma (sorry guys, running outside and tanning everyday won’t protect you). And also, spending more time in the sun will age your skin quicker and will increase your risk of other types of skin cancer such as squamous cell carcinoma (SCC) or basal cell carcinoma (BCC). SCC and BCC are just as dangerous as melanoma but only account for about 30% of skin cancer related deaths compared to about 70% for melanoma.
Now the other side of the argument is that ‘too little’ sun is not healthy. We actually need UV exposure for a number of important biological functions such as regulating our sleep patterns and creating vitamin D. However, a healthy amount of time which is relatively small (say 15min a day), should be enough for the body to acquire its entire necessary daily amount to remain healthy. In most locations, the time you spend getting to work or school is more than sufficient. However, if you are staying out the sun for longer periods of time, using sun safe protection strategies- such as sunscreen and covering up your skin- is the best way to avoid skin cancer and stay healthy.