How can i fix 'the used select statements have a different number of columns' in sql?

Example:

SELECT id FROM users
UNION
SELECT id, name FROM anotherUsers;

This error occurs when using UNION and the SELECT statements fetch a different number of columns.

Solution:

Ensure that all SELECT statements combined with UNION return the same number of columns.


SELECT id FROM users
UNION
SELECT id FROM anotherUsers;

Beginner's Guide to SQL