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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:22:15+00:00 2026-05-26T04:22:15+00:00

In the most general of cases, I have a query like below: SELECT tutor_school.name,

  • 0

In the most general of cases, I have a query like below:

SELECT tutor_school.name, count(*), tutor_school.schoolid 

FROM tutor_school, tutor_attends, tutor_tutors_in 

WHERE 
tutor_school.schoolid = tutor_attends.schoolid and 
tutor_school.schoolid in ('1', '2', '3') and 
tutor_attends.userid=tutor_tutors_in.userid

group by tutor_school.schoolid LIMIT 0, 10

In essence, I want:

Name of School, Number of Students attending that school that tutor in any subject, Schoolid

What I’m actually getting is

Name of School, sum of all subjects taught by students at that school, schoolid — in other words, if student 1 tutors 3 subjects, student 2 tutors 5, then instead of returning 2 I get 8!

I’ve realized that the issue is with the following statement:

tutor_attends.userid=tutor_tutors_in.userid

This isn’t checking the existence of a given foreign key in a remote table, it’s giving a result for each instance of that key.

What I’m trying to figure out is how to bind it to limit it to simply the existence of the given key, not the number of times that key occurs. I know I’ve seen a case similar to this in my SQL class, but I can’t remember what the solution was.

  • 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-05-26T04:22:16+00:00Added an answer on May 26, 2026 at 4:22 am

    Andrew is correct and here’s a concrete example of his solution (had to make assumptions on your table design):

    SQL> SELECT * FROM tutor_school;
    
      SCHOOLID NAME
    ---------- --------------------
             1 School A
             2 School B
             3 School C
    
    
    SQL> SELECT * FROM tutor_attends;
    
        USERID   SCHOOLID
    ---------- ----------
             1          1
             2          1
             3          2
    
    
    SQL> SELECT * FROM tutor_tutors_in;
    
        USERID SUBJECT
    ---------- --------------------
             1 Math
             1 Science
             1 English
             2 English
             3 Math
    
    
    SQL> SELECT tutor_school.name, COUNT(DISTINCT tutor_tutors_in.userid)
      2  FROM tutor_school, tutor_attends, tutor_tutors_in
      3  WHERE tutor_school.schoolid=tutor_attends.schoolid
      4  AND tutor_attends.userid=tutor_tutors_in.userid
      5  GROUP BY tutor_school.name
      6  /
    
    NAME                 COUNT(DISTINCTTUTOR_TUTORS_IN.USERID)
    -------------------- -------------------------------------
    School A                                                 2
    School B                                                 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Most of the ASP.NET MVC paging solutions I have found by googling look like
This may be for most languages in general, but I'm not sure. I'm a
Most of us write conditionals like: if (resultIndex == 0) ...but occaisionally I come
Most databases support some form of insert into select... statement. insert into a select
I have to match each record in TABLE1 with at most 1 record in
In WPF most controls have MouseUp and MouseDown events (and the mouse-button-specific variations) but
I see that most people who have been playing with ScalaSigParser, in an effort
The database which I report from most often is not properly typed. Almost every
Most of my user stories end up with tasks for create code review, carry
Most things are dynamic in SSRS i.e you can create a custom expression for

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.