Maher Elaissi
Maher Elaissi

Reputation: 35

SQL Query Optimization - eliminate subquery

I have this query and I want to optimize it. I use a SELECT and a subquery, and I'd like to use only one SELECT if possible.

Does anyone have any idea about it?

Thank you in advance

SELECT c.*
FROM course AS c
JOIN student_course AS sc ON c.id = uc.course_id
WHERE sc.student_id = '5' AND c.level = (SELECT MAX(level) 
                                         FROM course  
                                         JOIN student_course ON c.id = sc.course_id)
GROUP BY c.category_id 

Upvotes: 1

Views: 63

Answers (1)

Vikram Jain
Vikram Jain

Reputation: 5588

SELECT c.*
FROM course AS c
JOIN student_course AS sc
ON c.id = sc.course_id
WHERE sc.student_id = '5' 
GROUP BY c.category_id 
having c.level = MAX(sc.level)

Upvotes: 3

Related Questions