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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:11:50+00:00 2026-06-18T12:11:50+00:00

I have three related tables: Course Student Teacher Each course is given by a

  • 0

I have three related tables:

  • Course
  • Student
  • Teacher

Each course is given by a teacher to many students.

I can find the number of students attending a course:

SELECT c.id, count(*) FROM course as c, student as s
WHERE c.id = s.course_id
GROUP BY c.id;

I can find all the courses given by a teacher:

SELECT t.id, c.id FROM course as c, teacher as t
WHERE t.id = c.teacher_id;

I want to find how many students were taught by each teacher. Is the following query correct one?

SELECT t.id, sum(x.count_s) 
FROM 
   (SELECT count(*) AS count_s FROM course as c2, student as s
      WHERE c2.id = s.course_id AND c2.id = c.id
      GROUP BY c2.id) as x, 
   course as c, teacher as t
WHERE t.id = c.teacher_id;

Unfortunately, I cannot test it directly because this is actually a simplification of the real problem at hand. I need to find out which solution works for the simplified problem.

  • 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-18T12:11:51+00:00Added an answer on June 18, 2026 at 12:11 pm

    To answer your question, no. You cannot reference c.id inside the inline view aliased as x. That should throw an error.

    But if you remove that, then your query has the potential to return an inflated count, due to a semi-Cartesian product, between the inline view aliased as x and c.

    So that predicate needs to be relocated, and you’d need to return c2.id from x (i.e. add it to the SELECT list, you already have it referenced in the GROUP BY).

    This is equivalent to your query, just rewritten to replace the comma join operators and relocate join predicates to ON clause. This statement is equivalent to yours, and is invalid):

    SELECT t.id
         , SUM(x.count_s) 
      FROM ( SELECT count(*) AS count_s 
               FROM course c2
               JOIN student s
                 ON c2.id = s.course_id
                AND c2.id = c.id        -- INVALID here
              GROUP
                 BY c2.id
           ) x
     CROSS                              -- no join predicate 
      JOIN course c
      JOIN teacher t
        ON t.id = c.teacher_id
    

    To fix that, add c2.id to the SELECT list in x, and relocate that predicate. Something like this:

    SELECT t.id
         , SUM(x.count_s) 
      FROM ( SELECT count(*) AS count_s
                  , c2.id                 -- added
               FROM course c2
               JOIN student s
                 ON c2.id = s.course_id
           --   AND c2.id = c.id          -- relocated (removed from here)
              GROUP
                 BY c2.id
           ) x
      JOIN course c
        ON x.id = c.id                    -- relocated (added here)
      JOIN teacher t
        ON t.id = c.teacher_id
    

    Assuming that id is UNIQUE and NOT NULL in course, that query will return a reasonable count (although counts of zero will be “missing”).

    To return the “zero” counts, you’d need to use an OUTER join. And as I always prefer to use LEFT JOIN, the tables/inline views in the outermost query would need to be re-ordered:

    SELECT t.id
         , IFNULL(SUM(x.count_s),0)
      FROM teacher t
      LEFT
      JOIN course c
        ON c.teacher_id = t.id
      LEFT
      JOIN ( SELECT count(*) AS count_s
                  , c2.id                 -- added
               FROM course c2
               JOIN student s
                 ON c2.id = s.course_id
           --   AND c2.id = c.id          -- relocated (removed from here)
              GROUP
                 BY c2.id
           ) x
        ON x.id = c.id                    -- relocated (added here)
    

    Assuming that id is a PRIMARY KEY (or equivalent UNIQUE and NOT NULL) on each table, then that will return a “correct” count.

    It’s not necessary to include the course table in the inline view aliased as x. It would be sufficient to GROUP BY s.course_id.

    SELECT t.id
         , IFNULL(SUM(x.count_s),0)
      FROM teacher t
      LEFT
      JOIN course c
        ON c.teacher_id = t.id
      LEFT
      JOIN ( SELECT count(*) AS count_s
                  , s.course_id 
               FROM student s
              GROUP 
                 BY s.course_id
           ) x
        ON x.course_id = c.id                 -- relocated (added here)
    

    That query will return a valid “count”.


    A simpler statement would easier to understand. Here’s how I would get the count:

    SELECT t.id        AS teacher_id
         , COUNT(s.id) AS how_many_students_taught
      FROM teacher t
      LEFT
      JOIN course c
        ON c.id = t.course_id
      LEFT
      JOIN student s
        ON s.course_id = c.id
     GROUP
        BY t.id
    

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

Sidebar

Related Questions

I have three tables related to each other. They represent a hierarchical object. A_Table->
i have three lists with the same number of elements in each other, i
I have a three database tables related for example: company( one - one) Contact(
i am using JSP. Now assume I have three tables in mysql related as
I have three table's student , course , student_course table student { student_id(PK) }
Let's consider that we have three tables in a hierarchical form: Course Topic Sub-Topic
i have three tables in my database book,author,publishing ... i want the user can
I have 3 tables that are unrelated (related that each contains data for a
I have three related tables. Calendar 1...* CalendarUser *...1 User. When I have edited
If I have three tables, A/B/C, A is related to B and B is

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.