Consider a matrix $A \in \mathbb{C}^{n \times n}$. Suppose it's non-singular. Then it's columns $a_1, a_2, ..., a_n$ form a basis in $\mathbb{C}^n$.
Let's apply Gram-Schmidt process to them. We will obtain some basis $q_1, q_2, ..., q_n$. The transition matrix $S$ from the basis $a$ to the basis $q$ is upper-triangular(by construction). Now, consider the matrix $Q=\big[ q_1|q_2|...|q_n \big]$. It's columns are orthonormal $\implies$ $Q$ is unitary.
And now we know that the matrix $A$ can be represented as: $$ AS=Q \\ A=QS^{-1} $$
$S$ is invertible since it is a transition matrix. $S$ is upper-triangular $\implies$ $S^{-1}$ is upper-triangular as well.
E.g. we have shown that each non-degenerate matrix $A$ can be represented as a product of unitary and upper-triangular matrices.
But what if $A$ is singular? I've tried the following:
Let's select a basis of column space of $A$: $a_1, ..., a_r$ (they are columns of $A$). And also let's reorder columns of $A$ s.t. these linearly independent vectors $a_1, ..., a_r$ are the first $r$ vectors of the matrix. So, we've done permutation of columns of $A$. This operation can be represented by right-multiplication of A with some matrix $P$: $A \rightarrow AP$.
Now we apply Gram-Schmidt to the basis of column space(vectors $a_1,...,a_r$), and after extend the resulting basis to an ONB of $\mathbb{C}^n$:
$$q_1,q_2, ..., q_n$$
We have constructed matrix $Q=\big[ q_1 | q_2 | ... | q_n \big]$ that is unitary(since it's columns form an ONB). The matrix $S$ s.t. $AP=QS$ is upper-triangular. Moreover, it looks like: $$ \begin{bmatrix} s_{11} & s_{12} & s_{13} & ... & s_{1r} & ... & s_{1n} \\ 0 & s_{22} & s_{23} & ... & s_{2r} & ... & s_{2n} \\ 0 & 0 & s_{33} & ... & s_{3r} & ... & s_{3n} \\ ... & ... & ... & ... & .... & ... & ... \\ 0 & 0 & 0 & ... & s_{rr} & ... & s_{rn} \\ 0 & 0 & 0 & ... & 0 & ... & 0\\ 0 & 0 & 0 & ... & 0 & ... & 0\\ ... & ... & ... & ... & .... & ... & ... \\ 0 & 0 & 0 & ... & 0 & ... & 0\\ \end{bmatrix} $$
But how to show now that the matrix $SP^{-1}$ is upper-triangular? For me, it seems like it's not. Can you see a mistake in my thoughts? Maybe, you have other suggestions for construction of $QR$-decomposition in case of singular matrix? I'm stuck with this issue for a week now and I have no ideas what to do. Thanks!