programming4us
programming4us
DATABASE

The SQL Programming Language : Complex Queries and Join Queries (part 3)

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
1/5/2011 9:39:07 AM

9 Joins with GROUP BY and HAVING

GROUP BY and HAVING can be combined with a join query. For example, suppose you wanted to find the titles of movies, along with the number of roles in them, grouped by title and having at least five roles. The titles come from the Movie table, whereas the number of roles depends on the count of the rows in the Role table with matching values for movieID. This query graphically illustrates the idea of a one-to-many relationship. Grouping is done on the one table, Movie, and counting is done on the many table, Role. HAVING restricts the results to those movies with at least five roles. Example Query 10 illustrates this idea.

Example Query 10

SELECT title, COUNT(roleID)
FROM Movie, Role
WHERE Movie.movieID = Role.movieID
GROUP BY title HAVING COUNT(roleID) >= 5;

The output of Example Query 10 would start as shown in the Example Query 10 Results.

Example Query 10 Results

titleExpr1001
Apollo 136
Batman5
Batman & Robin11
. . .. . .

Example Query 10 is the kind of query where it can be easy to forget the joining condition because you are so involved in arranging the grouping and conducting the count; however, you would immediately recognize that you had made a mistake when you looked at the results of the query without a joining condition.

10 Three-Way and Multi-Way Joins

In the example database, a many-to-many relationship exists between the Actor table and the Movie table. This relationship is captured by embedding the primary key of the Actor table as a foreign key in the Role table and by embedding the primary key of the Movie table as a foreign key in the Role table. The Role table is the table in the middle in this many-to-many relationship. It is quite possible that someone might want to retrieve information that comes from related rows in all three of these tables.

Suppose you wanted to find the actorID and lastName from the Actor table, the roleID and roleName from the Role table, and the movieID and title from the Movie table. The critical point is that you would want the actor to match the role played, and you would want the role to match the movie in which it appeared. This query would require joining the Actor and Role tables on the actorID column and simultaneously joining the Role and Movie tables on the movieID column. Thus, the query must include two joining conditions, as shown in Example Query 11.

Example Query 11

SELECT Actor.actorID, lastName, roleID, roleName,
Movie.movieID, title
FROM Actor, Role, Movie
WHERE Actor.actorID = Role.actorID
AND Role.movieID = Movie.movieID;

The output of Example Query 11 would start as shown in the Example Query 11 Results.

Example Query 11 Results

actorIDlastNameroleIDroleNamemovieIDtitle
00061Garland00001Dorothy Gale016Wizard of Oz, The
00062Morgan00002The Wizard of Oz016Wizard of Oz, The
00063Bolger00003The Scarecrow016Wizard of Oz, The
. . .. . .. . .. . .. . .. . .

The example database contains five tables overall. By definition, because they appear in the same database, they are all related to one another in some way. The relationships between the Actor, Role, and Movie tables were reviewed earlier. In addition, a many-to-many relationship exists between the Role table and the Quote table. This is captured by embedding the primary key of the Role table as a foreign key in the RoleQuote table and by embedding the primary key of the Quote table as a foreign key in the RoleQuote table. The previous example query—a three-way join—had three tables and two joining conditions in it. In general, a query with more than three tables and two joins in it is known as a multi-way join. An example can be formed by writing a query that depends on the relationships among all five tables of the example database.

Suppose you wanted to find the lastName of the actor, the roleName, the title of the movie, and the quoteText of the quote spoken for all actors, roles, movies, and quotes in the database. Writing this query involves all five tables, and it requires four joining conditions, one for each pairing of tables in a relationship. Example Query 12 shows this query.

Example Query 12

SELECT lastName, roleName, title, quoteText
FROM Actor, Role, Movie, RoleQuote, Quote
WHERE Actor.actorID = Role.actorID
AND Role.movieID = Movie.movieID
AND Role.roleID = RoleQuote.roleID
AND RoleQuote.quoteID = Quote.quoteID;

The output of Example Query 12 would start as shown in the Example Query 12 Results.

Example Query 12 Results

lastNameroleNametitlequoteText
GarlandDorothy GaleWizard of Oz, TheToto, I have a feeling we're not in Kansas anymore.
GarlandDorothy GaleWizard of Oz, TheLions and tigers and bears, oh my!
GarlandDorothy GaleWizard of Oz, TheThere's no place like home.
MorganThe Wizard of OzWizard of Oz, ThePay no attention to that man behind the curtain!
BolgerThe ScarecrowWizard of Oz, TheLions and tigers and bears, oh my!
. . .. . .. . .. . .

Three-way and multi-way join queries could also be made more complex by the inclusion of the other features illustrated earlier. Results could be counted, ordered, grouped, and otherwise manipulated.

Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us