Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Assignment 1 Basic SQL - Solutions

solns of assignment 1 (Basic SQL)

Uploaded by

nisha charaya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 1 Basic SQL - Solutions

solns of assignment 1 (Basic SQL)

Uploaded by

nisha charaya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment 1: Basic SQL - Solutions

Solutions: University Schema

1. select name
from student
where tot_cred>100;

2. select c.course_id, c.id, c.grade


from takes as c, student as s
where c.id=s.id and s.name='Tanaka';

3. select distinct instructor.ID,name


from teaches, instructor, course
where instructor.ID=teaches.ID and course.course_id=teaches.course_id
and course.dept_name='Comp. Sci.' ;

As in the previous query, the join using clause can be used instead. Beware
of using natural join, since that would force the instructor's department to
match the course's department. Your test data should have included a case
where the instructor is from a different department, but has taught a Comp.
Sci. course.

4. (select course_id from section where semester='Fall' )


intersect
(select course_id from section where semester='Spring');
Optional

1. select name from instructor where dept_name = 'Comp. Sci.';

2. select course.course_id, title


from teaches, instructor, course
where instructor.ID=teaches.ID and course.course_id=teaches.course_id
and instructor.name='Srinivasan';

Beware of using natural join for this query, since instructor and course both
have a common attribute dept_name, and natural join makes these equal; as
a result, courses taught by Srinivasan outside his department are excluded.

You can write the query using the using clause though, as

select course_id, title


from (teaches join instructor using (ID)) join course using (course_id)
where name='Srinivasan'

and can even use the clause


on (teaches.course_id = instructor.course_id)
in place of the using clause above.
3. select distinct name
from instructor, teaches
where instructor.ID = teaches.ID and teaches.semester =
'Spring' and teaches.year = '2009'

Solutions: Railway Schema

1. select stcode1, stcode2


from track
where distance < 20;

2. select id
from trainhalts
where timein <> timeout and

stcode in (select station.stcode from station where station.name='THANE')


;

Note that instead of in, = can be used above, provided that station names
are guaranteed to be unique. This query can also be written using a join, for
example using

select distinct id
from trainhalts, station
where trainhalts.stcode = station.stcode and station.name = 'THANE'
and timein <> timeout

3. select t.name from trainhalts th, train t,station st where th.id=t.id


and st.stcode=th.stcode and th.timein is null and st.name='MUMBAI';

4. select st.name from trainhalts th, train t,station st where th.id=t.id and
st.stcode=th.stcode and t.name='CST-AMR_LOCAL' order by th.seqno;

5. select distinct name


from train, trainhalts
where train.id=trainhalts.id and trainhalts.seqno< 6 and

trainhalts.stcode in (select stcode from station where station.name='THAN


E');

You might also like