SQL Statements
 

Consider following database with Student, Subject and Marks Tables.

Data

Question : What are the Names of Students in School ?
Answer : Nimal, Kamal, Sunil and Mala

in SQL ( * means all records )
SELECT * FROM STUDENT 
will return all students in STUDENT table
Question : What are the Subjects taught in School ?
Answer : ICT, PHYSICS, MATH, MEDIA, DRAMA, MUSIC

SELECT * FROM
 SUBJECT
This will return all Subjects from SUBJECT table
Question : What are the marks of Nimal ?
Answer : ICT=77, PHYSICS=75, MATH=65

To obtain this data we have to JOIN STUDENT, SUBJECT and MARKS Tables

SELECT stNo, subNo, Marks FROM MARKS WHERE  stNO=1
This will return 3 records from MARKS Table

However we will not have Student Name and Subject Name in the result. They have to be obtained from STUDENT and SUBJECT tables.

WE Need to write a Join Query for this purpose.

select STUDENT.Name, MARKS.Marks, SUBJECTS.subject from Marks
inner join STUDENT on MARKS.stNo = STUDENT.stNo
inner join SUBJECT on MARKS.subNo = SUBJECTS.subNo;

(c) Shilpa Sayura Foundation 2006-2017