Can anyone explain to me why they use AS id and AS r2 and USING id on the codes :
SELECT id, username FROM friendship JOIN (SELECT CEIL(RAND() * (SELECT MAX(id) FROM friendship)) AS id) AS r2 USING (id);
I just want to know the purpose of using them, why using them?
The As is just to create an Alias for the subquery, that way you can use that same alias ‘id’ with the using command.
Basically the query wants the friendship table to join the subquery on the id column, since the subquery only returns one result it can join with the id column in the friendship table.