|
Custom Search
| |
|
Another Example for Outer Joins (three tables) In this tutorial, we continue left and right join study with an example of three tables joined. The three tables have been properly normalized in relational format with IDs acting as surrogate key and foreign key, so it looks more like real world database tables. 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. 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: Simplified result The query above shows only IDs which to us are not very user friendly. Below is a query for simplified result which only shows the weekdays and veggies eaten on that day. If no veggie is eaten, NULL is displayed. select wd.name as wkday_name,
Query result set - 11 rows returned: Practice #2: Return the weekdays on which you didn't eat any veggies. 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: Simplified result select wd.name as wkday_name,
Query result set - 2 rows returned: Practice #3: Use right join to get the same result as Practice #2. 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. 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: Simplified result The simplified result shows the weekdays and veggies eaten on that day, no IDs in the result. select wd.name as wkday_name,
Query result set - 9 rows returned: Practice #5: Return all the veggies you either ate or didn't eat. 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: Simplified result The query above shows only IDs which to us are not very user friendly. Below is a query for simplified result which only shows the weekdays and veggies eaten on that day. If no veggie is eaten, NULL is displayed. select v.name as veggie_name,
Query result set - 12 rows returned: Practice #6: Return the veggies you didn't eat. 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. 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:
Happy Coding! Other tutorials in this category 1. Using Inner Joins to Combine Data from Two Tables |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. 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 | Feedback | Terms of Use | Privacy Policy |