When to use which formula for sample variance?
I know that sample variance has the formula
$$s^2 = \frac{\sum (x_i - \bar{x})^2}{n - 1}$$
I also know that sample variance has the formula "Mean of the squares minus the mean squared".
While calculating the sample variance of a given sample, I used both the formula and realised that they give two different answers, hence I wanted to ask, when do I use the first one, and when do I use the second one?
Dividing by $n-1$ is designed to give an unbiased estimator of the population variance because you do not know the population mean $\mu$ and using $\bar x$ instead might otherwise give a estimate which is on average too low.
The following are correct, though you rarely see the second: $$\frac{\sum (x_i - \bar x)^2}{n} = \frac{\sum x_i^2}{n} - \bar x^2 = \frac{\sum x_i^2}{n} - \left(\frac{\sum x_i}{n} \right)^2$$ $$\frac{\sum (x_i - \bar x)^2}{n-1} = \frac{\sum x_i^2}{n-1} - \frac{n}{n-1}\bar x^2 = \frac{\sum x_i^2}{n-1} - \frac{\left(\sum x_i\right)^2}{n(n-1)} $$
Dividing by $n-1$ rather than by $n$ is done ONLY when $s^2$ is used as an estimate, based on a random sample, of the variance of a population from which one has only a small random sample of size $n.$ If one knew the value of $\mu,$ the population mean, rather than only of $\overline x,$ the sample mean, then one would use $\mu$ rather than $\overline x$ and one would divide by $n$ rather than by $n-1.$
When you use the first you are computing an unbiased estimator for a data sample (estimator). Although you can compute the second, it will be biased.
When using the second you are are computing the variance of a population (descriptive statistic), for which the first is not a good expression.
I also know that sample variance has the formula "Mean of the squares
minus the mean squared".
No. A phrase like "mean of squares minus the square of means" is a description of a formula for the population variance â not the sample variance. (E.g., you can see it suggested here, with a recommended mnemonic of "MOSSOM"). We see:
$$\sigma^2 = \frac{\sum (x_i - \bar{x})^2}{n} = \frac{\sum x_i^2}{n} - \bar{x}^2$$
This formula, as described in the rightmost expression, is equivalent to the definition of population variance. It's called the "calculating formula" because it takes fewer operations to produce, and was an important technique when all such calculations were done by hand (not so now with available technology). To be clear, these produce the exact same number â if you were double-checking with both, and get different results, then that indicates an error in some hand calculation.
However, this being a definition for population variance (it has $n$ in the denominator), it will of course produce a different value than the formula for sample variance noted by the OP (which has $n - 1$ in the denominator, that is, Bessel's correction).
Note that sample variance also has an analogous calculating formula, but it cannot be accurately described by the same MOSSUM-style mnemonic:
$$\bar{x}^2 = \frac{\sum (x_i - \bar{x})^2}{n - 1} = \frac{\sum x_i^2 - (\sum x_i)^2/n}{n - 1}$$