1

I'm trying to run a query on a database that has 3 tables: COURSE, SECTION, ENROLL.

They are creating as such:

create table COURSE
(
    Cno     varchar(9) primary key, 
    Cname   varchar(50),
    Credit  int check (Credit > 0)
);

create table SECTION
(
    Cno     varchar(9) REFERENCES COURSE(cno),
    Sno     varchar(9),
    Semester    varchar(15) check(Semester in('Fall','Spring','Summer')), 
    Year    int, 
    Sid     varchar(9) primary key 
);

create table ENROLL
(
    Mno     varchar(9) REFERENCES STUDENT(Mno),
    Sid     varchar(9) REFERENCES SECTION(Sid),
    Grade   CHAR check(Grade in('A','B','C','D','F')),
    primary key(Mno,Sid)
);

I'm trying to run a SUM on all the credits for classes that a student is enrolled in. This is how I'm trying to accomplish this:

select 
    SUM(select Credit 
        from COURSE c 
        where c.Cno = (select s.Cno 
                       from SECTION s 
                       where s.Sid = (select Sid 
                                      from ENROLL 
                                      where Mno = @mNum));

However I'm getting an error in Visual Studio that says

Incorrect syntax near 'select' (the second select statement)

What is it that is wrong? Any help would be greatly appreciated.

1
  • Which dbms do you use? Commented Dec 3, 2018 at 5:36

3 Answers 3

1

You would need an IN rather than an = if for example a Mno can enroll in multiple sections

select sum(Credit) 
   from COURSE c 
  where c.Cno in (select s.Cno 
                    from SECTION s 
                   where s.Sid in (select Sid 
                                    from ENROLL where Mno = @mNum)
                  )
Sign up to request clarification or add additional context in comments.

5 Comments

Ah, you are very correct! That portion was overlooked and I have not tested my query yet! Thank you!!
if you are taking in you need to take group by c.Cno because there can be multiple c.Cno's
since all are primary keys c.Cno , s.Sid and Mno this query with in as well as = will work
@nikhil Have a look at the table structure of ENROLL table. It's the combination of (Mno,Did) which is a primary key. So that means single Mno can have multiple Sid and so the = will fail if there multiple sections a Mno is enrolled in
@GeorgeJoseph i also thought that at the first place thanks for clarification
1

try this:

select SUM((select Credit from COURSE c where c.Cno = 
(select s.Cno from SECTION s where s.Sid = (select Sid from ENROLL where Mno = @mNum)));

2 Comments

Hi! The issue was not the parentheses, rather the way I was trying to use the aggregate function. My issue has been solved already! Thanks for answering though!
@Ekha it would be a good idea to test out the query using online db-fiddle or any of the online database test websites
1

see if it works:

select sum(Credit) from COURSE c where c.Cno in 
(select s.Cno from SECTION s where s.Sid in (select Sid from ENROLL where Mno = @mNum)) 
group by c.Cno;

1 Comment

Thank you so much! Is the group by necessary though?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.