|
|
|
Custom Search
| |
|
| |
|
Using Outer Joins to Combine Data from Two Tables
In last section about inner joins, we have seen that inner join can return data from two or more tables based on one or more join columns of common values. With outer join, we are able to retrieve data that have NO common values in the join columns. There two types of outer joins - left join and right join.
A practical illustration To better show you how out joins work, I created this scenario. You live in a healthy life style. You have a list of 10 veggies you'd like to eat and want to track what veggies you eat on each weekday. You want to know (1) what veggies you ate or didn't eat last week. (2) which weekday you didn't eat any veggies. Here are three tables I created based on scenario described above - wkday, veggie, eating. Table #1: wkday Table wkday contains 7 rows of data - Monday to Sunday.
Use script below to create wkday table: CREATE TABLE `wkday` ( Use script below to populate wkday table with data: insert into `wkday`(`id`,`name`) Table #2: veggie Table veggie contains 10 vegetable names.
Use script below to create veggie table: CREATE TABLE `veggie` ( Use script below to populate veggie table with data: insert into `veggie`(`id`,`name`) Table #3: eating Table eating contains data about what veggies you ate last week. In this table, column wkday_id is foreign key column which points back to the id column in wkday table. Column veggie_id is foreign key column which points back to the id column in veggie table. Column id is primary key of eating table.
Use script below to create wkday table: CREATE TABLE `eating` ( Use script below to populate wkday table with data: insert into `eating`(`id`,`wkday_id`,`veggie_id`)
Note that, from this tutorial onwards, all practice queries will be written in lower-case. The purpose is to show you the different styles of writing SQL scripts. Personally I prefer lower-case as I can type faster without having to worry about changing to upper-case for SQL keywords. SQLyog displays the keywords in colors anyway. There are two useful short-cut keys in SQLyog to change a selection to uppercase or lowercase:
Make selection uppercase: Crlt+Shift+U Practice #1: Return all the weekdays on which you either ate or didn't eat any veggies. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /* In the result displayed below, if the weekday id is found in eating table, it's displayed in third column. If it's not found, NULL is returned. Wednesday (id 3) and Sunday (id 7) have no matching data in eating table so NULL values are returned from eating table in third column. The fourth column is veggie_id column in eating table where NULL values are displayed. This is because each row in eating table has a corresponding wkday_id and veggie_id. If the weekday is not found, no veggies were eaten on that day.
Query result set - 11 rows returned: Practice #2: Return the weekdays on which you didn't eat any veggies. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 2 rows returned: Practice #3: Use right join to get the same result as Practice #2. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 2 rows returned: Practice #4: Return the weekdays on which you ate some veggies. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 9 rows returned: Practice #5: Return all the veggies you either ate or didn't eat. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 12 rows returned: Practice #6: Return the veggies you didn't eat. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 3 rows returned: Practice #7: Use right join to get the same result as Practice #6. In Firefox (not IE), copy and paste the following SQL to your SQLyog query window. Note that the SQL needs to end with semi-colon if you have multiple queries in the query window. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. /*
Query result set - 3 rows returned:
|
|
Copyright © 2012 GeeksEngine.com. All Rights Reserved. This website is hosted by LunarPages. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
| Home | Kung Fu Timer | Feedback | Terms of Use | Privacy Policy |