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 8997511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:53:58+00:00 2026-06-15T23:53:58+00:00

I have this query which runs great on two tables company and question .

  • 0

I have this query which runs great on two tables company and question. Both tables are structured in the following way:

company   |  question
id        |  id
name      |  company_id
          |  sentiment

SELECT company.id as company_id, company.name,  question.sentiment,
(SELECT count(*) FROM question 
WHERE question.sentiment=0 AND question.company_id=company.id) AS count
FROM question
JOIN company ON company.id=question.company_id
 WHERE question.sentiment = 0 GROUP BY company_id ORDER BY count DESC LIMIT 5

I have been trying to figure out how to rewrite this in order to avoid the subquery which seems to slow the query execution.
The question tables have over 1000 records.
Also, using indexes have decreased the execution time from 545.037 sec to 180.228 sec, still I need improve this.

Thanks

  • 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-15T23:53:58+00:00Added an answer on June 15, 2026 at 11:53 pm
    ALTER TABLE question ADD INDEX (sentiment, company_id);
    
    SELECT c.id as company_id, c.name, q.sentiment, q.count
    FROM company c
    JOIN (
        SELECT company_id, MAX(sentiment) AS sentiment, COUNT(*) AS count
        FROM question
        WHERE sentiment = 0
        GROUP BY company_id
        ORDER BY NULL
    ) AS q ON c.id = q.company_id
    ORDER BY count DESC LIMIT 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query which runs perfectly well on both Oracle and SQL
I have this query. It matches anything which has South in its name. But
i have the following SQL Query which runs on SQL Server CE 4 SELECT
I have this query which runs a join on Books, TradingDesks and ProductInfos. The
I have the following oracle query which runs fine SELECT c.customercode, s.sales_id FROM customers
I have this query which only runs once per request. SELECT SUM(numberColumn) AS total,
I have this sql query which runs fine when I execute it in MSSQL
I have the following query which runs fine on all of my sql server
I currently have the following linq which runs and gets two strongly typed objects
I have this query which on executing in my sql command line client executes

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.