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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:44:26+00:00 2026-06-14T09:44:26+00:00

Very simple question (for some): is it possible to use select subquery result in

  • 0

Very simple question (for some): is it possible to use select subquery result in where clause?

I’m currently using something like this:

SELECT companies.id
    , companies.name
    , (SELECT COUNT(*)
    FROM orders
    WHERE
        orders.company_id = companies.id
        AND
        orders.scheduled_at BETWEEN '2012-08-01 00:00:00' AND '2012-10-31 23:59:59'
    ) AS number_of_rides
FROM companies
WHERE
    (SELECT COUNT(*)
    FROM orders
    WHERE
        orders.company_id = companies.id
        AND
        orders.scheduled_at BETWEEN '2012-08-01 00:00:00' AND '2012-10-31 23:59:59'
    ) != 0
    AND
    companies.affiliate = 0
    AND
    companies.id = 346

This makes the same query run twice, once in select and the other in where. Is it possible at all to reference the select subquery in where to look something like this:

SELECT companies.id
    , companies.name
    , (SELECT COUNT(*) AS number_of_rides
    FROM orders
    WHERE
        orders.company_id = companies.id
        AND
        orders.scheduled_at BETWEEN '2012-08-01 00:00:00' AND '2012-10-31 23:59:59'
    ) AS x
FROM companies
WHERE
    x.number_of_rides != 0
    AND
    companies.affiliate = 0

This doesn’t work at the moment, neither does this:

SELECT companies.id
    , companies.name
    , (SELECT COUNT(*)
    FROM orders
    WHERE
        orders.company_id = companies.id
        AND
        orders.scheduled_at BETWEEN '2012-08-01 00:00:00' AND '2012-10-31 23:59:59'
    ) AS number_of_rides
FROM companies
WHERE
    number_of_rides != 0
    AND
    companies.affiliate = 0

Is it possible at all without running it twice in the query?

Edit: OK. Although using GROUP BY wouldn’t be possible in some cases, it did what I needed to achieve in this case. That’s what I did:

SELECT companies.id
    , companies.name
    , (SELECT COUNT(*)
    FROM orders
    WHERE
        orders.company_id = companies.id
        AND
        orders.scheduled_at BETWEEN '2012-08-01 00:00:00' AND '2012-10-31 23:59:59'
    ) AS number_of_rides
FROM companies
WHERE
    companies.affiliate = 0
GROUP BY companies.id
HAVING number_of_rides != 0

I would still like to know if there’s a technical explanation as to why WHERE can’t access the subquery result in SELECT and HAVING can…

  • 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-14T09:44:27+00:00Added an answer on June 14, 2026 at 9:44 am

    Try this

    Select 
        companies.id  , 
        companies.name,
        Tcount as number_of_rides
    FROM companies
    left join (
            select 
                id , 
                COUNT(*) as Tcount 
            FROM orders 
            where orders.scheduled_at BETWEEN '2012-08-01 00:00:00' AND '2012-10-31 23:59:59'
            ) as orders on orders.company_id = companies.id
    WHERE companies.affiliate = 0 AND companies.id = 346
    group by companies.id 
    having number_of_rides != 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Question might be very simple, I have a class Result with some inner
Forgive what might seem to some to be a very simple question, but I
Very simple question, but I want to start using a consistent naming convention for
This may be a simple question, but for some reason I don't know this
Possible Duplicate: VBA collection: list of keys Maybe it is a very simple question,
The question is very simple, does update_attributes validates every possible validation of the model,
Very simple question... I have an array of pixels, how do I display them
Very simple question: Specifically in Python (since Python actually has strongly recommended style guidelines
Very simple question. I need to perform a task when the user closes his
Very simple question, I made the following program : #include <stdlib.h> int main(int argc,

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.