Home > database >  Filtering a MariaDB SQL table for the sum of values in a column grouped by dept_name
Filtering a MariaDB SQL table for the sum of values in a column grouped by dept_name

Time:10-09

I have an SQL table that I'm trying to write a Query for. The table has four columns, course_id, title, dept_name and credits. Every course is different and has a unique name, but many of the courses share dept_name. Each course has between 1 and 4 credits.

Complete Table

The query I'm trying to write needs to first combine the rows that have the same dept_name, and then only display the resulting departments and credit amounts with a credit sum below 30.

By using the following Query I'm able to return the credit sum grouped by dept_name, but I can't seem to only return the dept_names with a sum of less than 30. Can anyone help me out here? I've tried adding additional WHERE statements, but I just receive errors. Please let me know if this is enough or if more info is needed.

Half-Filtered Table

CodePudding user response:

Use HAVING. For example:

select deptname, sum(credits) 
from bk_course 
group by dept_name
having sum(credits) < 30
  • Related