UNION ALL

If you run the same query from the last section on UNION with UNION ALL instead, you will get the same results because there weren't any duplicates to filter out.

You can use the following new query to see how UNION ALL works by removing duplicates: 

SELECT playerid, yearid, teamid, G AS gamesbatted FROM lahmansbaseballdb.batting
WHERE yearid = 2005
UNION ALL
SELECT playerid, yearid, teamid, g_batting FROM lahmansbaseballdb.appearances
WHERE yearid = 2005
ORDER BY yearid, playerid, gamesbatted;

The previous query will give us the results shown in the following screenshot:

From the preceding screenshot, we can make the following observations:

  • In the query results, you see two rows for each playerid based on the yearid, teamid, and gamesbatted fields, regardless of whether the gamesbatted field has the same value in the batting and appearances tables. 
  • If you execute this same query as UNION, you would only see one row for each playerid, yearid, teamid, and gamesbatted combination. 
  • The results with UNION ALL bring up an interesting question, though. If the appearances table has the number of games batted in any given year and the batting table also has this value, you would think they would match one another. UNION ALL becomes a good way of seeing what discrepancies may lie in different tables in your database. 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.94.249