the issue comes from a flawed data model and a misunderstanding of the relational concept.
SQL tables are no Excel tables. There is no "first row" or "top row", and you cannot leave fields empty in the sense of "see above". SQL tables represent sets, which means they are unordered
. And each row is a complete dataset standing for itself (it may contain references to other rows, though).
So the first step to fix the data model would be to add the missing data, namely the descriptions.
However, SQL tables represent a specific relation
. Something like "customer x bought y units of product z". Your table isn't like that. It's basically two relations merged into one: You have users uploading pictures. And you have users being friends with other users. Those are two completely different aspects. Having put them into a single table, you now face the problem of redundant data. You'd have to repeat all user-related data in every row belonging to that user. This violates the principle of normalization
To fix the problem, you need to revise your data model: You have two
relations, so you need two
tables. Call them "photo_uploads" and "friendships" or something like that. The photos table only
describes the uploads, nothing else. And the friendships table only
assigns users to other users, nothing else.
To associate the data, join the two tables. On one hand, you have a photo shot by a user. And on the other hand, you have all friends of this user:
- sql Code
JOIN friendships ON photo_uploads.user = friendships.user
Note that your GROUP BY
clause makes no sense and actually violates the SQL standard. Other database systems wouldn't even accept this. The GROUP BY
clause has a specific meaning: It's used for applying aggregate functions like COUNT()
to specific subsets rather than all rows. It's not
"grouping" in the colloquial sense. When people talk about "grouping", they usually want the ORDER BY
As a rule of thumb: If you have no aggregate function in your query, you can't have a GROUP BY