I have two MySql table user and marks
User table:
+-----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | firstName | varchar(255) | YES | | NULL | | | lastName | varchar(255) | YES | | NULL | | | email | varchar(55) | YES | | NULL | | +-----------+--------------+------+-----+---------+----------------+
Marks Table
+--------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | user_id | int | NO | | NULL | | | subject_id | varchar(255) | YES | | NULL | | | score | int | YES | | NULL | | | subject_name | varchar(225) | YES | | NULL | | +--------------+--------------+------+-----+---------+----------------+
I want to fetch details(userid, firstName and lastName) of top 10 users with the highest marks in descending order. Marks of the user is defined as sum of all scores a user has in different subjects.
I am really confused which join is to be used here, I am new to MySql and this query is kind of challenging for me, Hope you understood the problem. Please let me know if you have any suggestion, Thank You