You are on page 1of 2

Question 1. A “GROUP BY”, which may require WHERE and HAVING conditions.

List the names (astroname) and number of missions flown of all astronauts who have flown 7
or more Shuttle missions. Order by astroname. Hint: the query requires
“NASA2_Astronaut NATURAL JOIN NASA2_Assigned”. With the given data, the
result of the (correct) query will be:

astroname | missions  name of second column optional


----------------------+---------
Chang-Diaz, Franklin | 7
Helms, Susan | 7
Ross, Jerry | 7
Voss, James | 7
(4 rows)

Question 2. A simple sub-query (i.e. a sub-query that is not correlated).


Select the Shuttles mission (missionno) and duration (days) of the mission(s) that have flown for
the most number of days. With the given data, the result of the (correct) query will be:
missionno | launchyear | days
-----------+------------+------
STS-80 | 1996 | 17
(1 row)

Question 3. A join requiring the cross product notation.


That is, the join cannot be done with “NATURAL JOIN”. It may be a self-join, or involve 2 or
more tables.

List the Commander and Pilot (astrono) of all Shuttle missions with mission numbers …

LIKE 'STS-2%'

With the given data, the result of the (correct) query will be:

commander | pilot | missionno  rename of first and


--------+-------+----------- second column optional
138 | 408 | STS-2
192 | 116 | STS-26
166 | 161 | STS-27
372 | 341 | STS-28
112 | 77 | STS-29
Answer 1:
A “GROUP BY”, which may require WHERE and HAVING conditions.

SELECT astroname, count(missionno) as Missions  see note below


FROM NASA2_Astronaut NATURAL JOIN NASA2_Assigned
WHERE projectname = 'Shuttle'
GROUP BY astroname
HAVING count(missionno) > 6
ORDER BY astroname;

Notes: (1) Instead of count(missionno) students may also use count(*).


(2) The third column need not be renamed using “as Missions”. The defaults of
count(missionno) or “count(*)” are acceptable.

Answer 2:
A simple sub-query (i.e. a sub-query that is not correlated).

SELECT missionno, launchyear, days


FROM NASA2_Mission
WHERE projectname = 'Shuttle'
AND days = (SELECT max(days)
FROM NASA2_Mission
WHERE projectname = 'Shuttle');

Note: Instead of :
… days = (SELECT max(days)
students may use: … days >= ALL (select days

Answer 3:
A join requiring the cross product notation (i.e. cannot be done with “NATURAL JOIN”). It
may be a self-join, or involve 2 or more tables.

select ass1.astroNo as Commander, ass2.astroNo as Pilot,


ass1.MissionNo
from NASA2_Assigned ass1, NASA2_Assigned ass2
where ass1.projectName = 'Shuttle'
AND ass1.role = 'Commander' AND ass2.role = 'Pilot'
AND ass1.MissionNo = ass2.MissionNo
AND ass1.MissionNo LIKE 'STS-2%'
order by ass1.missionNo;

You might also like