1. What does ending your query with “limit 100” do and when should you use this statement?

  1. What data will this query return? {{{select * from biograph, members

where matgrp = grp and biograph.sname = members.sname and sex = 'M' and status = 0 and dcause = 0 and statdate = date order by matgrp, pid; }}}

  1. What data will this query return? {{{select * from biograph, members

where biograph.matgrp = members.grp and biograph.sname = members.sname and biograph.sex = 'M' and biograph.status = 0 and biograph.dcause = 0 and biograph.statdate = members.date order by biograph.matgrp, biograph.pid; }}}

  1. What data will this query return? {{{select *
    • from biograph
      • join members on (biograph.matgrp = members.grp and
        • biograph.sname = members.sname)
      • where biograph.sex = 'M' and
        • biograph.status = 0 and biograph.dcause = 0 and biograph.statdate = members.date
        order by biograph.matgrp, biograph.pid;

}}}

  1. What data will this query return? {{{-- This is an illustrative query used in the Babase Quiz

-- It is fiendishly tricky so pay close attention! SELECT *

}}}

  1. What are the pros and cons of the various queries above. Which do you prefer?

  1. Write a query that shows each date that each male offspring spent in his maternal group -- the one codified in the GROUPS table. {{{SELECT biograph.matgrp, biograph.sname, members.date
    • FROM biograph
      • JOIN members ON (members.grp = biograph.matgrp AND
        • members.sname = biograph.sname)
      WHERE biograph.sex = 'M' ORDER BY biograph.matgrp, biograph.sname, members.date;

}}}

  1. Write a query that returns the number of days that each male offspring spends in his maternal group -- the one codified in the GROUPS table. {{{SELECT biograph.matgrp, biograph.sname, count(*) AS daysingrp
    • FROM biograph
      • JOIN members ON (members.grp = biograph.matgrp AND
        • members.sname = biograph.sname)
      WHERE biograph.sex = 'M' GROUP BY biograph.matgrp, biograph.sname ORDER BY biograph.matgrp, biograph.sname;

}}}

-- table we create will go away automatically when the transaction -- rolls back. BEGIN;

-- Create an intermediate table in my personal schema to get the real results from. SELECT biograph.matgrp, biograph.sname, count(*) AS daysingrp

SELECT foobar.matgrp, avg(foobar.daysingrp)

-- Undo all the changes we made to the database, i.e. forget -- about the intermediate table we created. ROLLBACK; }}}

-- sql session is done executing. SELECT biograph.matgrp, biograph.sname, count(*) AS daysingrp

SELECT foobar.matgrp, avg(foobar.daysingrp)

}}}

-- as if they were a real table. SELECT foobar.matgrp, avg(foobar.daysingrp)

}}}

Wiki content based upon work supported by the National Science Foundation under Grant Nos. 0323553 and 0323596. Any opinions, findings, conclusions or recommendations expressed in this material are those of the wiki contributor(s) and do not necessarily reflect the views of the National Science Foundation.