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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:05:21+00:00 2026-05-30T02:05:21+00:00

For a homework assignment, I have to write a MySQL query to calculate the

  • 0

For a homework assignment, I have to write a MySQL query to calculate the GPA of every student in the database table. I broke the problem down into 3 parts: (1) calculating the number of grade points earned by each student, (2) calculating the number of credits taken, and then (3) dividing grade points by credits. Here are the queries I’ve written for steps 1 and 2:

  1. Calculate grade points earned:

    SELECT ID, SUM( credits ) AS credits_taken
    FROM takes
    NATURAL JOIN course 
    GROUP BY ID
    

2 Find grade points earned:

    SELECT ID, SUM( credits * ( SELECT points FROM gradepoint WHERE letter = grade ) ) AS tot_grade_points
    FROM takes NATURAL JOIN course
    GROUP BY ID 

I manually evaluated each query and they return the correct results. But I can’t figure out how to return (credits_taken / tot_grade_points) for each student. Here is what I have tried:

    SELECT ID, GPA
    FROM student AS S NATURAL JOIN
            (SELECT ID,( 'credits_taken' / SUM( credits * ( SELECT points FROM gradepoint WHERE letter = grade ) )) AS GPA
             FROM takes AS T1 NATURAL JOIN course
             WHERE S.ID = T1.ID
             AND EXISTS (
                           SELECT ID, SUM( credits ) AS 'credits_taken'
                           FROM takes AS T2 NATURAL JOIN course
                           WHERE S.ID = T2.ID
                           GROUP BY ID
                          ) 
             GROUP BY ID) Z
    GROUP BY ID

But this gives me the error ” Unknown column ‘S.ID’ in ‘where clause'”. From what I’ve read, you can’t reference the alias of a table from a subquery in a join operation. Does anyone have another way of doing the calculation of these two subqueries and returning them bound to the student ID?

The ‘takes’ table maps student IDs to information about the courses they’ve taken, most importantly the course_id and grade. The ‘course’ table contains the ‘credits’ field, the number of credits the course is worth.

EDIT

Here are the relevant table structures:

takes:

    Field     | Type         | Null | Key | Default | Extra |
   +-----------+--------------+------+-----+---------+-------+
   | ID        | varchar(5)   | NO   | PRI |         |       |
   | course_id | varchar(8)   | NO   | PRI |         |       |
   | sec_id    | varchar(8)   | NO   | PRI |         |       |
   | semester  | varchar(6)   | NO   | PRI |         |       |
   | year      | decimal(4,0) | NO   | PRI | 0       |       |
   | grade     | varchar(2)   | YES  |     | NULL    |       |
   +-----------+--------------+------+-----+---------+-------+

course:

   +-----------+--------------+------+-----+---------+-------+
   | Field     | Type         | Null | Key | Default | Extra |
   +-----------+--------------+------+-----+---------+-------+
   | course_id | varchar(8)   | NO   | PRI |         |       |
   | title     | varchar(50)  | YES  |     | NULL    |       |
   | dept_name | varchar(20)  | YES  | MUL | NULL    |       |
   | credits   | decimal(2,0) | YES  |     | NULL    |       |
   +-----------+--------------+------+-----+---------+-------+
  • 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-30T02:05:22+00:00Added an answer on May 30, 2026 at 2:05 am

    I would try:

    SELECT takes.sec_id, 
      SUM( course.credits * gradepoint.points ) / SUM( course.credits ) AS GPA
    FROM takes
    JOIN gradepoint ON takes.grade = gradepoint.letter
    JOIN course ON takes.course_id = course.course_id
    GROUP BY takes.sec_id
    

    Since your table structure description is incomplete I had to guess gradepoint schema and I assumed sec_id identifies a student in takes table, if there is another column for that just replace it in the query in both SELECT and GROUP BY parts. Maybe it is ID, but a column name like that is usually used for primary keys. Or maybe there are no primary keys defined at all, which is a bad practise anyway. Also you would need to join student table if you wanted any student info other than id, like name and so on.

    I would also recommend using JOIN ... ON ... syntax instead of NATURAL JOIN, not only it is more readable, it also gives you more flexibility, for example see how gradepoint is joined instead of using costly dependent subquery.

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

Sidebar

Related Questions

I have done a homework assignment, here is the problem statement: Your program should
As part of a homework assignment, I have to write a C program in
For a homework assignment I have to write a basic shell including redirection. The
Continuing on this problem , but I'll reiterate: For a homework assignment I have
This is part of a homework assignment. What we have to do is write
I have a homework assignment to write a multi-threaded sudoku solver, which finds all
For my c++ homework assignment, I have to write a Tic-Tac-Toe Win32 console application.
(this is indirectly a part of a much larger homework assignment) I have something
I have a homework assignment where I need to take input from a file
My kiddo had a homework assignment to write Blackjack in Java. I helped him

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.