"all the users and their site-wide role (I suppose every user has a site role no matter whether they logged in or not"
Actually, it is more complicated.
Three things to understand are: 1) Users who are not logged in are treated as though the were in the "Guest role" but is it not actually assigned to them properly speaking: the code checks whether a user is logged in yet and treats them accordingly: there is only one Guest just as there is only one anonymous; 2) Actual roles are not assigned until a user logs in; and 3) Not all role assignments are in the role_assignments table.
When a user logs in they are immediately assigned one of two roles: Authenticated user or Site administrator. Neither of these appear in the role_assignments table.
Next, after logging in, if the user has been assigned an explicit system level role, they will get that too: the role_assignments table holds the roles that you assign at the site level in SIte administration > Users > Permissions > Assign system roles. You will know this when the contextid for that assignment is 1, which means the site level. This is how the default role of Manager works, for example.
The role_assignments table also holds assignments for categories, courses,
activities, and user to user. It's the contextid that tells you exactly where the assignment is made. You will see many examples of how to use this over in the Ad-hoc contributed reports collection:
https://docs.moodle.org/400/en/ad-hoc_contributed_reports
Therefore, as Howard says, basing your query on the role_assignments table will only return those users how have some explicit system level role assigned to them:
SELECT u.username, r.shortname
FROM prefix_role_assignments ra
JOIN prefix_user u ON u.id = ra.userid
JOIN prefix_role r ON r.id = ra. roleid
WHERE ra.contextid = 1
Any user when logging in who is not assigned one of those System level roles or is not a Site admin, will be an Authenticated user only. But that is not stored anywhere in the
database: the Moodle code just checks to see if the user is logged in or not, and if so, already knows they are an Authenticated user.