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

  • SEARCH
  • Home
  • 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 7968451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:04:47+00:00 2026-06-04T07:04:47+00:00

Which query will execute faster and which is perfect query ? SELECT COUNT(*) AS

  • 0

Which query will execute faster and which is perfect query ?

SELECT
    COUNT(*) AS count
FROM
    students
WHERE
    status = 1
AND
    classes_id IN(
                    SELECT
                        id
                    FROM
                        classes
                    WHERE
                        departments_id = 1
                );

Or

SELECT
    COUNT(*) AS count
FROM
    students s
LEFT JOIN
    classes c
ON
    c.id = s.classes_id
WHERE
    status = 1
AND
    c.departments_id = 1

I have placed two queries both will output same result. Now I want to know which method will execute faster and which method is correct way ?

  • 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-04T07:04:48+00:00Added an answer on June 4, 2026 at 7:04 am

    You should always use EXPLAIN to determine how your query will run.

    Unfortunately, MySQL will execute your subquery as a DEPENDENT QUERY, which means that the subquery will be ran for each row in the outer query. You’d think MySQL would be smart enough to detect that the subquery isn’t a correlated subquery and would run it just once, alas, it’s not yet that smart.

    So, MySQL will scan through all of the rows in students, running the subquery for each row, and not utilizing any indexes on the outer query whatsoever.

    Writing the query as a JOIN would allow MySQL to utilize indexes, and the following query would be the optimum way to write it:

    SELECT COUNT(*) AS count
    FROMstudents s
    JOIN classes c
      ON c.id = s.classes_id
      AND c.departments_id = 1
    WHERE s.status = 1
    

    This would utilize the following indexes:

    students(`status`)
    classes(`id`, `departements_id`) : multi-column index
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a QUERY which is like SELECT COUNT(*) as cnt FROM tbl_docatrtypegroupdoctype, tbl_doctype,
I need to write a SQL query which will get me those rows from
so suppose I have this query SELECT COUNT(*) FROM (SELECT * FROM k JOIN
I am trying to execute a sql query which will create a new table
I'm trying to execute a SOQL query using salesforce REST API which will return
Here is the query which will get me all the contacts have HD quality
I have a quite complicated query which will by built up dynamically and is
Im trying to write a query which will emit : the date/s which Overlapped
I am trying to build a query which will do this: Lets say for
I have set up an update query which will update values entered into text

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.