Retrieve the previous employee's salary within each department, ordered by employee ID.
CREATE TABLE employees (emp_id INTEGER, dept_id INTEGER, salary INTEGER); INSERT INTO employees (emp_id, dept_id, salary) VALUES (1, 101, 50000), (2, 101, 60000), (3, 102, 55000), (4, 102, 65000);
```sql
WITH RankedEmployees AS (
    SELECT emp_id, dept_id, salary,
           ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY emp_id) as rn
    FROM employees
)
SELECT e.emp_id, e.dept_id, e.salary, r.salary AS prev_salary
FROM employees e
LEFT JOIN RankedEmployees r ON e.dept_id = r.dept_id AND e.emp_id = r.emp_id + 1;
```
```sql
WITH RankedEmployees AS (
    SELECT emp_id, dept_id, salary,
           ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY emp_id) as rn
    FROM employees
)
SELECT e.emp_id, e.dept_id, e.salary, r.salary AS prev_salary
FROM employees e
LEFT JOIN RankedEmployees r ON e.dept_id = r.dept_id AND e.emp_id = r.emp_id + 1;
```