Skip to content
Advertisement

Update and renew data based on data in other tables

There are 3 tables student, course, and takes as following

tot_cred column data in the student table now is assigned with random values (not correct), I want to perform the query that updates and renews those data based on the course’s grade each student has taken. For those students who received F grade will be excluded and those who didn’t take any course will be assigned 0 as tot_cred.

I came up with two approaches, one is

This query meets all my needs, but for those students who didn’t take any course, it does assign NULL value instead of 0.

The second is using case when

But it assigned 0 to all students. Is any way to achieve the above requirement?

Advertisement

Answer

If the 1st query meets your requirement and the only problem is that it returns NULL for the students that did not take any course then the easiest solution would be to use instead of SUM() aggregate function the function TOTAL() which will return 0 instead of NULL:

The same could be done with COALESCE():

Also, use a proper join with an ON clause and aliases for the tables to improve readability.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement