Worked SQL Examples


Mark Mahoney

NEW! Explore Our New Platform, the Playback Press

Learn from all of your favorite code playbacks (including the ones in this book) on our brand-new site, the Playback Press.

At the Playback Press, every playback is equipped with an AI assistant that's ready to answer your questions and to test your knowledge of the material you are learning. Watch this video, "How to View a Code Playback", to see these exciting new features in action. It's like having a personal tutor right at your fingertips!

All of our books remain free of charge, check them out now!

This is a collection of 30 'worked examples' where I show the viewer how I go about writing SQL queries. SQL queries are rarely written left-to-right, top-to-bottom. Instead, a database developer typically builds their queries up from small and simple to more complex. These playbacks show how my queries grow as I get closer and closer to a correct solution. They also emphasize how to use an ER Diagram and schema when writing queries.

The database I am using describes courses taken by students in a university. It is a SQLite database called studentGrades.sqlite. Here are the entities and relationships:

Entites:

  • Students
  • Sections
  • Courses
  • Professors
  • Departments

Relationships:

  • Any one student takes zero or more sections (every student receives a grade).
  • Any one section has zero or more students taking it.
  • Any one section is an instance of one course.
  • Any one course has zero or more instances of sections.
  • Any one sections is taught by a single professor.
  • Any one professor teaches zero or more sections.
  • Any one professor belongs to zero or more departments.
  • Any one department has zero or more professors.
  • Any one department has zero or one professor who is the chairperson of the department.
  • Any one professor chairs zero or one department.
  • Any one course is offered by one department.
  • Any one department offers zero or more courses.

Here is an ER Diagram and schema describing the database:

ER Diagram
Schema

If you are brand new to SQL you might want to start with another one of my 'books' of code playbacks:
Database Design and SQL for Beginners

If you would like to learn how to write programs that use a database you can read:
Programming with SQLite.

If you'd like to stay connected and get updates when I add new playbacks you can follow me on twitter:@markm208.

Q1: List every course name, section number, and professor name in chronological order for every section that has ever been offered.
Q2: List every course name and section number for every course offered by the computer science department.
Q3: Find the name of every professor who has ever taught CSCI111.
Q4: List all of the professor names and their departments.
Q5: List the names of the professors who have taught both CSCI111 and CSCI112.

Q6: List the names of all of the students of professor Mark Mahoney who are greater than or equal to 21 years old.
Q7: List the names of all of the students who are taught by a department chair.
Q8: List all of the course names and section numbers of every course ever taught by a department chair.
Q9: List all of the courses with the oldest student.
Q10: List all of the courses and section numbers with the youngest average student age.

Q11: List all of the course names and section numbers of courses with less than four credits.
Q12: List all of the course names and section numbers with the smallest enrollment.
Q13: List all of the student names who have taken more than one course with Mark Mahoney.
Q14: List all of the student names who have taken a course with both Mark Mahoney and Eric Whendon.
Q15: List all the course names and section numbers that had two or more students earn A's.

Q16: Find the names of all the students who have taken CSCI111.
Q17: Find the names of all professors in the computer science department who are not chairs of a department.
Q18: Find the names of all professors who are the chair of a department.
Q19: Find the ssn, first and last name, course name, and grade earned for all courses taken in spring 2007.
Q20: Find the course name and section number of all the courses that have ever been offered in the fall.

Q21: Find the names of all of the professors teaching in spring 2007.
Q22: Find the names of all of the students who have received an A and a B in any course.
Q23: Find out how many students have ever taken CSCI111.
Q24: Find the average age of all students who ever had a course with Mark Mahoney.
Q25: Find the names of all of the professors who have never taught a course.

Q26: Find the names of all of the professors of who have taught May Jones.
Q27: Find the names of the students who have had a course in Fall 2006 or Spring 2007.
Q28: Find the names of the students who have taken a course from a professor who has more than one appointment to a department.
Q29: Find the average age of students who took courses in Spring 2007.
Q30: Find the sum of all of the credit hours offered by the computer science department in 2007