Difference between revisions of "Introduction to Databases with Stanford University : Relational Algebra Exercises"

From Ittichai Chammavanijakul's Wiki
Jump to navigation Jump to search
Line 1: Line 1:
Source: http://db-class.org
+
* Introduction to Databases by Standford University http://db-class.org
 +
* Additional materials: http://openclassroom.stanford.edu/MainFolder/CoursePage.php?course=IntroToDatabases
  
 
1. Find all pizzas eaten by at least one female over the age of 20.
 
1. Find all pizzas eaten by at least one female over the age of 20.
Line 7: Line 8:
 
  \select_{gender='female' and age > 20} Person  
 
  \select_{gender='female' and age > 20} Person  
 
   \join Eats)  
 
   \join Eats)  
 +
</pre>
 +
 +
2. Find the names of all females who eat at least one pizza served by Straw Hat. (Note: The pizza need not be eaten at Straw Hat.)
 +
 +
<pre>
 +
\project_{name} (
 +
  \select_{gender='female'} (
 +
      ( \select_{pizzeria='Straw Hat'} Serves
 +
        \join Eats
 +
      ) \join Person
 +
  )
 +
)
 +
</pre>
 +
 +
3. Find all pizzerias that serve at least one pizza for less than $10 that either Amy or Fay (or both) eat.
 +
 +
<pre>
 +
\project_{pizzeria} (
 +
  \select_{name='Amy' or name='Fay'} (
 +
      \select_{price < 10} Serves
 +
      \join Eats
 +
  )
 +
)
 
</pre>
 
</pre>

Revision as of 17:49, 16 November 2011

1. Find all pizzas eaten by at least one female over the age of 20.

\project_{pizza} ( 
 \select_{gender='female' and age > 20} Person 
  \join Eats) 

2. Find the names of all females who eat at least one pizza served by Straw Hat. (Note: The pizza need not be eaten at Straw Hat.)

\project_{name} ( 
  \select_{gender='female'} ( 
      ( \select_{pizzeria='Straw Hat'} Serves 
        \join Eats 
       ) \join Person 
   )
)

3. Find all pizzerias that serve at least one pizza for less than $10 that either Amy or Fay (or both) eat.

\project_{pizzeria} ( 
   \select_{name='Amy' or name='Fay'} ( 
      \select_{price < 10} Serves 
      \join Eats 
   ) 
)