So I'm trying to union two queries and then order by a column. However, whenever I try and run the query it gives an error that doesn't make sense to me.

Example data

```
CREATE TABLE test (
    Company_code    VARCHAR(120) NOT NULL,
    operating_year  INT          NOT NULL,
    Profit          INT          NOT NULL
);

INSERT INTO test (Company_code, operating_year, Profit)
VALUES  ('A', 1999, 2000),
        ('A', 2000, 3000),
        ('B', 1999, 1600),
        ('B', 2000, 4000);

```

Query

```
SELECT 
    t.company_code,
    t.profit
FROM
    test t
WHERE
    t.company_code = 'A'

UNION

SELECT
    t.company_code,
    t.profit
FROM
    test t
WHERE
    t.company_code = 'B'

ORDER BY
--    t.profit; --- Does *not* work
--    profit;   --- Does work

```

Ignore the very basic example, and how just adding an OR to the WHERE statement resolves this.

My question is why does having an alias in the `ORDER BY` throw an error when a `UNION` is involved. But not when run individually?