Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9215655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:15:10+00:00 2026-06-18T02:15:10+00:00

Take a look at this query, please: SELECT (SELECT COUNT(id) FROM result WHERE `mterm1`

  • 0

Take a look at this query, please:

SELECT
    (SELECT COUNT(id) FROM result WHERE `mterm1` > r.mterm1 AND lesson_id = r.lesson_id) + 1 AS `pos_mt1_school`,
    (SELECT COUNT(id) FROM result WHERE `mterm1` > r.mterm1 AND lesson_id = r.lesson_id AND class_id = r.class_id) + 1 AS `pos_mt1_class`,

    (SELECT COUNT(id) FROM result WHERE `term1` > r.term1 AND lesson_id = r.lesson_id) + 1 AS `pos_t1_school`,
    (SELECT COUNT(id) FROM result WHERE `term1` > r.term1 AND lesson_id = r.lesson_id AND class_id = r.class_id) + 1 AS `pos_t1_class`,

    (SELECT COUNT(id) FROM result WHERE `mterm2` > r.mterm2 AND lesson_id = r.lesson_id) + 1 AS `pos_mt2_school`,
    (SELECT COUNT(id) FROM result WHERE `mterm2` > r.mterm2 AND lesson_id = r.lesson_id AND class_id = r.class_id) + 1 AS `pos_mt2_class`,

    (SELECT COUNT(id) FROM result WHERE `term2` > r.term2 AND lesson_id = r.lesson_id) + 1 AS `pos_t2_school`,
    (SELECT COUNT(id) FROM result WHERE `term2` > r.term2 AND lesson_id = r.lesson_id AND class_id = r.class_id) + 1 AS `pos_t2_class`,

    r.*, student.* FROM result r

LEFT JOIN lessons lesson ON r.lesson_id = lesson.id
LEFT JOIN students student ON r.student_id = student.id
LEFT JOIN classes class ON student.class_id = class.id

WHERE student.id = 217 ORDER BY lesson.id ASC

I want to show student exam scores. So, first select his results (exam scores) from table result, then join lessons to show lesson name, and finally calculate student position in each lesson in his class and school. (based on his exam score)

That query works fine, but it takes about 2 seconds to be executed. (performance problem)

Is there any optimization for that query? (and an alternative for its subselect queries)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:15:11+00:00Added an answer on June 18, 2026 at 2:15 am

    You are doing cumulative counts in the subselects.

    I cannot readily think of another way of doing this in MySQL in a single query. If the “>” where an “=”, this could be turned into a join. But that is not possible in this case.

    You can turn this into an explicit cross join with an aggregation, using a query such as this:

    SELECT sum(case when r2.mterm1  > r.mterm1 then 1 else 0 end)+1 as `pos_mt1_school`,
           sum(case when r2.mterm1 > r.mterm1 and r2.class_id = r.class_id) + 1 then 1 else 0 end) AS `pos_mt1_class`,
           . . .
        r.*, student.*
    FROM result r LEFT JOIN
         lessons lesson
         ON r.lesson_id = lesson.id LEFT JOIN
         students student
         ON r.student_id = student.id LEFT JOIN
         classes class
         ON student.class_id = class.id join
         result r2
         on r.lesson_id = r2.lesson_id
    WHERE student.id = 217
    group by r.result_id
    ORDER BY lesson.id ASC
    

    I’m not sure this will be faster than the original version. The original version, by the way, would be sped up by putting on two indexes: result(lesson_id, term2) and result(less_id, class_id, term2).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please take a look of this query: SELECT DATE(datetime), COUNT(1) as numVisits FROM .table_stats.
I have a weird problem please take a look at this query: select *
Please can you take a look at this query and point out what I'm
THis is myDB Class //Please take a look at public Cursor getplaces() --- Am
Please look at this SQL Fiddle for the tables and what query I have
please take a look at the following snippet: public IEnumerable<T> Query(Expression<Func<T, bool>> filter) {
Take a look at this awesome code from John P Bloch. What this code
Please take a look at the following code in a controller named test: $this->load->library('pagination');
Hi could some one please take a look at this and let me know
Please take a look at this code.... what is i have instead of average,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.