1

I am new to SQL and am learning joins of tables now.

I am stuck at joining three tables.

(I have given the rows I have inserted to the tables also for your reference.)

My tables are

--Table1

    create table sql_students(
    stu_studentid int not null primary key,
    stu_name varchar(100) not null,
    stu_regnnumber bigint unique not null
    )

--Rows inserted to Table1

insert into sql_students (stu_studentid,stu_name,stu_regnnumber) values (1,'John',194300)
insert into sql_students (stu_studentid,stu_name,stu_regnnumber) values (2,'Joy',959595)
insert into sql_students (stu_studentid,stu_name,stu_regnnumber) values (3,'Lucy',474848)

--Table2

create table sql_exam(
exa_examid bigint not null primary key,
exa_name varchar(100) not null,
exa_maxmark decimal(5,2) not null,
exa_minmarkreqdforpass decimal(5,2) not null,
exa_examscheduletime datetime not null
)

--Rows inserted into Table2

insert into sql_exam(exa_examid,exa_name,exa_maxmark,exa_minmarkreqdforpass,exa_examscheduletime) values (1,'Maths',100,40,'2012-10-10 10:00')
insert into sql_exam(exa_examid,exa_name,exa_maxmark,exa_minmarkreqdforpass,exa_examscheduletime) values (2,'English',75,35,'2012-10-11 10:00')

--Table3

create table sql_studentmarks(
stm_studentid int foreign key references sql_students(stu_studentid),
stm_examid bigint foreign key references sql_exam(exa_examid),
stm_mark decimal(5,2)
)

--Rows inserted into Table3
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (1,1,80)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (2,1,90)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (3,1,40)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (1,2,70)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (2,2,60)
insert into sql_studentmarks(stm_studentid,stm_examid,stm_mark) values (3,2,17)

I need guidelines to get

  1. All students who passed all exams
  2. All students who attended all exams
  3. All students and their mark difference in Maths and English.

Thanks in advance.

6
  • 4
    What have you tried so far, you need to show your efforts so we can help, not just right queries for you..
    – Sparky
    Jan 18, 2014 at 13:23
  • 1
    All you need are 2 INNER JOIN clauses to join the 3 Tables. Add the WHERE clause at the end to solve problems 1, 2 and 3. Show us something using what I said and we'll correct it for you. Jan 18, 2014 at 13:30
  • Hi Sparky, for the first one, i tried "select stm_studentid from sql_exam join sql_studentmarks on exa_examid=stm_examid and stm_mark>exa_minmarkreqdforpass inner join sql_students on stu_studentid=stm_studentid". It didnt work as required. Jan 18, 2014 at 13:31
  • Your joins look OK, check out the SQL fiddle I created: sqlfiddle.com/#!3/555bca/6 SQLFiddle lets you experiment with queries and gives us an easy way to help with what you tried.
    – Sparky
    Jan 18, 2014 at 13:37
  • 1
    Hi Chris, As you have told, for the second one, i tried "select stm_studentid,stu_name from sql_exam inner join sql_studentmarks on exa_examid=stm_examid where count(exa_examid)=(select COUNT(*) from sql_exam) inner join sql_students on stm_studentid=stu_studentid" . But it does not look good. Jan 18, 2014 at 13:43

2 Answers 2

2

For #1

SELECT s.stu_name 
FROM sql_studentmarks AS m 
JOIN sql_students AS s ON m.stm_studentid = s.student_id 
JOIN sql_exam AS x ON COUNT(m.stm_examid) = COUNT (x.stm_examid)
WHERE m.stm_mark >= x.exa_minmarkreqdforpass GROUP BY s.stu_studentid;

For #2 you can base on the exam count, perhaps creating a var in the select statement:

SELECT s.stu_name 
FROM sql_studentmarks AS m 
JOIN sql_students AS s ON m.stm_studentid = s.stu_studentid 
RIGHT OUTER JOIN sql_exam AS x ON COUNT(m.stm_examid) = COUNT (x.stm_examid)
AND m.stm_examid IS NOT NULL
GROUP BY s.stu_student_id;

FOR #3 use the examples from above, simple joins will do:

SELECT s.stu_name, x.exa_name, m.stm_mark 
FROM sql_studentmarks AS m 
JOIN sql_students AS s ON m.stm_studentid = s.stu_studentid 
JOIN sql_exam x ON  m.stm_examid = x.exa_examid AND s.stu_studentid = x.exa_studentid;
2
  • Your first query misses the fact that the passing grade is stored in the exam table. Your second query doesn't use the variable you created...
    – Sparky
    Jan 18, 2014 at 14:12
  • My apologies @Sparky, it was a RPIA trying to edit the answer directly on my phone. Thanks for pointing my code's flaws out, I've edited the answer to address the issues.
    – safejrz
    Jan 18, 2014 at 16:39
1

The code below shows you the first two queries. Your joins are OK.. Try these in SQL Fiddle and then see if you can work the third query out

Query #1: (You were very close, just needed the distinct keyword)

select  stm_studentid,stu_name  
from sql_exam 
join sql_studentmarks on exa_examid=stm_examid 
                         and stm_mark>exa_minmarkreqdforpass 
inner join sql_students on stu_studentid=stm_studentid
group by stm_studentid,stu_name
having count(*) = (select count(*) from sql_exam)

Query #2: -

  • The group by lets you count the number of exams taken.
  • The having lets you compare the count to number of exams available
select stu_name,count(*) as NumExamsTaken
from sql_studentmarks
join sql_students on stu_studentid=stm_studentid
group by stu_name
having count(*) = (select count(*) from sql_exam)

Query #3:
This should get you started

 select  stu_studentid,stu_name,
            MG.stm_mark as MathGrade,
            EG.Stm_mark as EnglishGrade
    from sql_students
    join sql_exam MATH on MATH.exa_name='MATHS'
    join sql_exam ENG on ENG.exa_name='ENGLISH'
    join sql_studentmarks MG on MG.stm_studentid=stu_studentid
                            and MG.stm_examid=MATH.exa_examId
    join sql_studentmarks EG on EG.stm_studentid=stu_studentid
                            and EG.stm_examid=ENG.exa_examId
9
  • Hi Sparky, am getting the result properly with your answer for Query#1 on SQL fiddle, but when I try it on my MS SQL Server 2008, it returns all the students who passed any exam. Why so? Jan 18, 2014 at 13:58
  • OK, I missed the need to have passed all exams. I've modified the query for you
    – Sparky
    Jan 18, 2014 at 14:01
  • Now Query#1 seems perfect. Thanks. Let me analyse this one and try the second one. :) Jan 18, 2014 at 14:03
  • And ignore my first comment for this . I was confused. Jan 18, 2014 at 14:04
  • Hi Sparky, I tried your answer for Query#2, it is returning the number of exams each student attended. I am looking for student who attended all exams. Jan 18, 2014 at 14:14

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.