How to prove this integral inequality?

Here is a problem:

Let $B_r=\{ (x_1,x_2,\cdots,x_n)\in \mathbb{R}^n: x_1^2+x_2^2+\cdots+x_n^2<r^2\}.$ Let $f$ be a $C^1$ real function on $B_2$. Prove that

$$\inf_{a\in R}\int_{B_2} |f(x)-a|^2dx\leq C\sum_{j=1}^n \int_{B_2}\left|\frac{\partial f}{\partial x_j}\right|^2 dx\ \ \ \ (1)$$ for a constant $C$ independent of $f$.

I think we can choose $a=f(0)$. If $$\int_{B_2} |f(x)-f(0)|^2dx\leq C\sum_{j=1}^n \int_{B_2}\left|\frac{\partial f}{\partial x_j}\right|^2 dx\ \ \ \ \ (2)$$

then we are done. But how to prove (2), I want to use mean value theorem, but failed.

Setting $a=\langle f\rangle_{B_2}=\frac{1}{|B_2|} \int_{B_2} f(y)dy$ where $|B_2|$ is the Lebesgue measure of $B_2$, the claimed inequality is known as Poincaré inequality:

$$\|f-\langle f\rangle_{B_2}\|_{L^2(B_2)}\le C\|\nabla f\|_{L^2(B_2)}$$

This holds for Sobolev functions $f\in W^{1,2}(B_2)$. The proof uses compactness of the embedding $W^{1,2}(B_2)\hookrightarrow L^2(B_2)$ (follows from Rellich-Kondrachov theorem). You can find it for example here:

• L.C. Evans, Partial Differential Equations, Chapter 5.8.1

In the special case $n=1$, $f\in C^1(B_2)$ there is a proof using only the mean value theorem:

Namely, the mean value theorem (for integrals) implies that there exists a $x_0\in B_2=(-2,2)$ such that $f(x_0)=a=\langle f\rangle_{B_2}$.

Then by the fundamental theorem of calculus,

$$f(x)=f(x_0)+\int_{x_0}^x f^\prime(y)dy$$

From this we see for all $x\in(-2,2)$:

$$|f(x)-a|=\left| \int_{x_0}^x f^\prime(y)dy\right|\le \| f^\prime\|_{L^1(B_2)}\le \|f^\prime\|_{L^2(B_2)} \|1\|_{L^2(B_2)}$$

where we used the Cauchy-Schwarz inequality in the last step.

Squaring and integrating over $x\in(-2,2)$ we obtain

$$\int_{-2}^2 |f(x)-a|^2 dx\le C \int_{-2}^2 |f^\prime(x)|^2 dx$$

with $C=4\|1\|^2_{L^2(B_2)}=16$.

Of course all of this works just as well for balls with other radii than $2$.