It is known that a polynomial $P$ with integer coefficients has degree $2022$. What is the maximum $n$ such that there exist integers $a_1, a_2, \cdots a_n$ with $P(a_i)=i$ for all $1\le i\le n$? 

[Extra: What happens if $P \in \mathbb{Q}[X]$ and $a_i\in \mathbb{Q}$ instead?]
1. **Understanding the problem**: We need to find the maximum number \( n \) such that there exist integers \( a_1, a_2, \ldots, a_n \) with \( P(a_i) = i \) for all \( 1 \le i \le n \), given that \( P \) is a polynomial of degree 2022 with integer coefficients.

2. **Analyzing the polynomial**: Since \( P \) has integer coefficients, we can use properties of polynomials and integer sequences to derive constraints on \( a_i \).

3. **Difference of consecutive terms**: Consider the difference \( a_i - a_{i-1} \). Since \( P(a_i) = i \) and \( P(a_{i-1}) = i-1 \), we have:
   \[
   P(a_i) - P(a_{i-1}) = i - (i-1) = 1
   \]
   By the properties of polynomials, \( a_i - a_{i-1} \) must divide \( P(a_i) - P(a_{i-1}) \). Therefore:
   \[
   a_i - a_{i-1} \mid 1
   \]
   This implies:
   \[
   a_i - a_{i-1} \in \{1, -1\}
   \]

4. **Sequence construction**: Since \( a_i \neq a_j \) for \( i \neq j \), the sequence \( a_i \) must be strictly increasing or strictly decreasing. Without loss of generality, assume \( a_i \) is strictly increasing. Thus:
   \[
   a_{i+1} = a_i + 1
   \]

5. **Polynomial construction**: To construct such a polynomial \( P \), consider:
   \[
   P(x) = x + \prod_{j=1}^{2022} (x - j)
   \]
   This polynomial has degree 2022 and satisfies \( P(j) = j \) for \( j = 1, 2, \ldots, 2022 \).

6. **Proof of optimality**: Suppose there exists a polynomial \( P \) of degree 2022 that satisfies \( P(a_i) = i \) for \( i = 1, 2, \ldots, n \) with \( n > 2022 \). Then \( P(x) - x \) would have more than 2022 roots, which contradicts the fact that a polynomial of degree 2022 can have at most 2022 roots. Therefore, the maximum \( n \) is 2022.

7. **Extra: Rational coefficients**: If \( P \in \mathbb{Q}[X] \) and \( a_i \in \mathbb{Q} \), the same reasoning applies. The polynomial \( P(x) = x + \prod_{j=1}^{2022} (x - j) \) still works, and the maximum \( n \) remains 2022.

The final answer is \( \boxed{2022} \)