<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0" xmlns:blogChannel="http://backend.userland.com/blogChannelModule">

<channel>
<title>Topic &#x27;mysql practice question&#x27; in forum &#x27;Webmasters and Developers&#x27; - dslreports.com</title>
<link>http://www.dslreports.com/forum/mysql-practice-question-27808653</link>
<description></description>
<language>en</language>
<pubDate>Thu, 20 Jun 2013 01:57:56 EDT</pubDate>
<lastBuildDate>Thu, 20 Jun 2013 01:57:56 EDT</lastBuildDate>

<item>
<title>Re: mysql practice question</title>
<link>http://www.dslreports.com/forum/Re-mysql-practice-question-27876081</link>
<description><![CDATA[watice posted : wow, I was struggling with JOINS myself & had asked elsewhere recently for help. Thank you kindly. Will be reading this in detail once I am not so sleep deprived from all night coding binges. ]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-mysql-practice-question-27876081</guid>
<pubDate>Thu, 03 Jan 2013 08:44:25 EDT</pubDate>
</item>

<item>
<title>Re: mysql practice question</title>
<link>http://www.dslreports.com/forum/Re-mysql-practice-question-27810813</link>
<description><![CDATA[loli posted : Damn. I see myself toying around with this all month long. This is fun. Thanks alot again for the quick and easy to understand examples. I figured out subqueries too :)]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-mysql-practice-question-27810813</guid>
<pubDate>Tue, 11 Dec 2012 16:32:15 EDT</pubDate>
</item>

<item>
<title>Re: mysql practice question</title>
<link>http://www.dslreports.com/forum/Re-mysql-practice-question-27810552</link>
<description><![CDATA[loli posted : <div class="bquote"><said>said by <a href="/profile/811675" onClick="this.blur(); return popup(event,'/uidpop?ajh=1&uid=811675');">cdru</a>:</said><p><div class="bquote"><said>said by <a href="/profile/679039" onClick="this.blur(); return popup(event,'/uidpop?ajh=1&uid=679039');">loli</a>:</said><p>I think I also figured out how to deal with my updating question.</p></div>Are you updating a table and saving the sum?  <br> </p></div>Yeah. Just for practice purpose I'm trying to come up with simple problems to deal with updating and nested queries. I've seen selects inside selects or something of that nature. <br><br>Normalizing I get a sense of it's usage.  &raquo;<A HREF="http://www.sqlcourse2.com/joins.html" >www.sqlcourse2.com/joins.html</A><br><small>--<br>Verizon FiOS 35/35 &raquo;<A HREF="http://bit.ly/RVzC75" >bit.ly/RVzC75</A></small>]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-mysql-practice-question-27810552</guid>
<pubDate>Tue, 11 Dec 2012 15:29:04 EDT</pubDate>
</item>

<item>
<title>Re: mysql practice question</title>
<link>http://www.dslreports.com/forum/Re-mysql-practice-question-27810137</link>
<description><![CDATA[cdru posted : <div class="bquote"><said>said by <a href="/profile/679039" onClick="this.blur(); return popup(event,'/uidpop?ajh=1&uid=679039');">loli</a>:</said><p>I think I also figured out how to deal with my updating question.</p></div>Are you updating a table and saving the sum?  <br><br>Good database practice is normalizing data so that you minimize redundancies and dependencies.  Having a computed field such as your sum generally would not go along as a best practice since it's a computed value.  There can always be exceptions to this, particularly if a given computation is complex or to gain performance advantage.]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-mysql-practice-question-27810137</guid>
<pubDate>Tue, 11 Dec 2012 13:58:22 EDT</pubDate>
</item>

<item>
<title>Re: mysql practice question</title>
<link>http://www.dslreports.com/forum/Re-mysql-practice-question-27810089</link>
<description><![CDATA[loli posted : wow... Didn't expect all that :o Give me a moment while I absorb this. Thanks alot! <br><br>I think I also figured out how to deal with my updating question.<br><small>--<br>Verizon FiOS 35/35 &raquo;<A HREF="http://bit.ly/RVzC75" >bit.ly/RVzC75</A></small>]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-mysql-practice-question-27810089</guid>
<pubDate>Tue, 11 Dec 2012 13:49:44 EDT</pubDate>
</item>

<item>
<title>Re: mysql practice question</title>
<link>http://www.dslreports.com/forum/Re-mysql-practice-question-27808946</link>
<description><![CDATA[cdru posted : There are 3 main types of JOINs in SQL: INNER, OUTER, and CROSS.  In addition, outer can be split up into LEFT OUTER, RIGHT OUTER, and FULL OUTER. Below are examples of 6 types of joins.<br><br>Here is the data that we'll be using for the examples, just so we're on the same page:<br><small>sql code</small>:<div class="syntax"><pre><span class="codetext"><font color="#FFFFFF"><font color="#ffff00">CREATE</font> <font color="#ff40ff">TABLE</font> [dbo].[table1](<br>  [name] [nvarchar](<font color="#ff6060">1</font>) <font color="#ff40ff">NULL</font>,<br>  [x] [int] <font color="#ff40ff">NULL</font>,<br>  [y] [int] <font color="#ff40ff">NULL</font><br>) <font color="#ff40ff">ON</font> [PRIMARY]<br>GO<br><font color="#ffff00">INSERT</font> [dbo].[table1] ([name], [x], [y]) <font color="#ff40ff">VALUES</font> (N<font color="#ff6060">'a'</font>, <font color="#ff6060">100</font>, <font color="#ff6060">200</font>)<br><font color="#ffff00">INSERT</font> [dbo].[table1] ([name], [x], [y]) <font color="#ff40ff">VALUES</font> (N<font color="#ff6060">'b'</font>, <font color="#ff6060">200</font>, <font color="#ff6060">400</font>)<br><font color="#ffff00">INSERT</font> [dbo].[table1] ([name], [x], [y]) <font color="#ff40ff">VALUES</font> (N<font color="#ff6060">'e'</font>, <font color="#ff6060">400</font>, <font color="#ff6060">800</font>)<br> <br><font color="#ffff00">CREATE</font> <font color="#ff40ff">TABLE</font> [dbo].[table2](<br>  [name] [nvarchar](<font color="#ff6060">1</font>) <font color="#ff40ff">NULL</font>,<br>  [x] [int] <font color="#ff40ff">NULL</font>,<br>  [y] [int] <font color="#ff40ff">NULL</font><br>) <font color="#ff40ff">ON</font> [PRIMARY]<br>GO<br><font color="#ffff00">INSERT</font> [dbo].[table2] ([name], [x], [y]) <font color="#ff40ff">VALUES</font> (N<font color="#ff6060">'a'</font>, <font color="#ff6060">10</font>, <font color="#ff6060">20</font>)<br><font color="#ffff00">INSERT</font> [dbo].[table2] ([name], [x], [y]) <font color="#ff40ff">VALUES</font> (N<font color="#ff6060">'b'</font>, <font color="#ff6060">30</font>, <font color="#ff6060">40</font>)<br><font color="#ffff00">INSERT</font> [dbo].[table2] ([name], [x], [y]) <font color="#ff40ff">VALUES</font> (N<font color="#ff6060">'c'</font>, <font color="#ff6060">50</font>, <font color="#ff6060">60</font>)<br><font color="#ffff00">INSERT</font> [dbo].[table2] ([name], [x], [y]) <font color="#ff40ff">VALUES</font> (N<font color="#ff6060">'d'</font>, <font color="#ff6060">70</font>, <font color="#ff6060">80</font>)<br></font></span></pre></div><br><b>INNER:</b> An inner join will take values from both the left and right tables from the JOIN based on some criteria.  Typically this is some type of a key field although that is not a requirement that it be a true key.<br><br><small>sql code</small>:<div class="syntax"><pre><span class="codetext"><font color="#FFFFFF"><font color="#ffff00">SELECT</font> table1.name <font color="#ff40ff">as</font> name1,<br>table1.x <font color="#ff40ff">as</font> table1x,<br>table1.y <font color="#ff40ff">as</font> table1y,<br>table2.name <font color="#ff40ff">as</font> name2,<br>table2.x <font color="#ff40ff">as</font> table2x,<br>table2.y <font color="#ff40ff">as</font> table2y<br><font color="#ff40ff">FROM</font> table1<br>INNER JOIN table2 <font color="#ff40ff">on</font> table1.name = table2.name<br> <br>name1 table1x       table1y      name2      table2x      table2y<br>a     <font color="#ff6060">100</font>           <font color="#ff6060">200</font>          a          <font color="#ff6060">10</font>           <font color="#ff6060">20</font><br>b     <font color="#ff6060">200</font>           <font color="#ff6060">400</font>          b          <font color="#ff6060">30</font>           <font color="#ff6060">40</font><br></font></span></pre></div><br>In the above example, the INNER JOIN is performed by matching rows where table1.name = table2.name. Only rows A and B from each table are returned as those are the only two names that are common between both tables.<br><br><b>LEFT OUTER:</b> A left outer join will take all rows from the left table and only the rows from the right table that match based on the criteria.  <br><br><small>sql code</small>:<div class="syntax"><pre><span class="codetext"><font color="#FFFFFF"><font color="#ffff00">SELECT</font> table1.name <font color="#ff40ff">as</font> name1,<br>table1.x <font color="#ff40ff">as</font> table1x,<br>table1.y <font color="#ff40ff">as</font> table1y,<br>table2.name <font color="#ff40ff">as</font> name2,<br>table2.x <font color="#ff40ff">as</font> table2x,<br>table2.y <font color="#ff40ff">as</font> table2y<br><font color="#ff40ff">FROM</font> table1<br>LEFT OUTER JOIN table2 <font color="#ff40ff">on</font> table1.name = table2.name<br> <br>name1 table1x       table1y      name2 table2x      table2y<br>a     <font color="#ff6060">100</font>           <font color="#ff6060">200</font>          a     <font color="#ff6060">10</font>           <font color="#ff6060">20</font><br>b     <font color="#ff6060">200</font>           <font color="#ff6060">400</font>          b     <font color="#ff6060">30</font>           <font color="#ff6060">40</font><br>e     <font color="#ff6060">400</font>           <font color="#ff6060">800</font>          <font color="#ff40ff">NULL</font>  <font color="#ff40ff">NULL</font>         <font color="#ff40ff">NULL</font><br></font></span></pre></div><br>For the e row, we get NULL values for the table2 columns from table2 as there was not a corresponding row in that table, but the LEFT OUTER requires that all the rows from the left table be included.<br><br><b>RIGHT OUTER:</b> A RIGHT OUTER is just like a LEFT OUTER, except all the rows from the right table are included even if there are corresponding rows in the left table.<br><br><small>sql code</small>:<div class="syntax"><pre><span class="codetext"><font color="#FFFFFF"><font color="#ffff00">SELECT</font> table1.name <font color="#ff40ff">as</font> name1,<br>table1.x <font color="#ff40ff">as</font> table1x,<br>table1.y <font color="#ff40ff">as</font> table1y,<br>table2.name <font color="#ff40ff">as</font> name2,<br>table2.x <font color="#ff40ff">as</font> table2x,<br>table2.y <font color="#ff40ff">as</font> table2y<br><font color="#ff40ff">FROM</font> table1<br>RIGHT OUTER JOIN table2 <font color="#ff40ff">on</font> table1.name = table2.name<br> <br>name table1x       table1y      table2x      table2y<br>a    <font color="#ff6060">100</font>           <font color="#ff6060">200</font>          <font color="#ff6060">10</font>           <font color="#ff6060">20</font><br>b    <font color="#ff6060">200</font>           <font color="#ff6060">400</font>          <font color="#ff6060">30</font>           <font color="#ff6060">40</font><br><font color="#ff40ff">NULL</font> <font color="#ff40ff">NULL</font>          <font color="#ff40ff">NULL</font>         <font color="#ff6060">50</font>           <font color="#ff6060">60</font><br><font color="#ff40ff">NULL</font> <font color="#ff40ff">NULL</font>          <font color="#ff40ff">NULL</font>         <font color="#ff6060">70</font>           <font color="#ff6060">80</font><br></font></span></pre></div><br>We have two new rows that correspond to c and d in table2.  NULL shows up as the name as we specify that we want to use table1.name as the name.<br><br><b>FULL OUTER:</b> A FULL OUTER join combines both the LEFT and RIGHT OUTER JOINs, meaning it includes all rows from both tables regardless if a corresponding row is found in the other.<br><br><small>sql code</small>:<div class="syntax"><pre><span class="codetext"><font color="#FFFFFF"><font color="#ffff00">SELECT</font> table1.name <font color="#ff40ff">as</font> name1,<br>table1.x <font color="#ff40ff">as</font> table1x,<br>table1.y <font color="#ff40ff">as</font> table1y,<br>table2.name <font color="#ff40ff">as</font> name2,<br>table2.x <font color="#ff40ff">as</font> table2x,<br>table2.y <font color="#ff40ff">as</font> table2y<br><font color="#ff40ff">FROM</font> table1<br>FULL OUTER JOIN table2 <font color="#ff40ff">on</font> table1.name = table2.name<br> <br>name1 table1x       table1y      name2 table2x      table2y<br>a     <font color="#ff6060">100</font>           <font color="#ff6060">200</font>          a     <font color="#ff6060">10</font>           <font color="#ff6060">20</font><br>b     <font color="#ff6060">200</font>           <font color="#ff6060">400</font>          b     <font color="#ff6060">30</font>           <font color="#ff6060">40</font><br>e     <font color="#ff6060">400</font>           <font color="#ff6060">800</font>          <font color="#ff40ff">NULL</font>  <font color="#ff40ff">NULL</font>         <font color="#ff40ff">NULL</font><br><font color="#ff40ff">NULL</font>  <font color="#ff40ff">NULL</font>          <font color="#ff40ff">NULL</font>         c     <font color="#ff6060">50</font>           <font color="#ff6060">60</font><br><font color="#ff40ff">NULL</font>  <font color="#ff40ff">NULL</font>          <font color="#ff40ff">NULL</font>         d     <font color="#ff6060">70</font>           <font color="#ff6060">80</font><br></font></span></pre></div><br><b>CROSS JOIN:</b> A CROSS JOIN does not use a ON clause for joining the two tables.  With a CROSS JOIN, every row in the left table is joined to every row in the right table.<br><br><small>sql code</small>:<div class="syntax"><pre><span class="codetext"><font color="#FFFFFF"><font color="#ffff00">SELECT</font> table1.name <font color="#ff40ff">as</font> name1,<br>table1.x <font color="#ff40ff">as</font> table1x,<br>table1.y <font color="#ff40ff">as</font> table1y,<br>table2.name <font color="#ff40ff">as</font> name2,<br>table2.x <font color="#ff40ff">as</font> table2x,<br>table2.y <font color="#ff40ff">as</font> table2y<br><font color="#ff40ff">FROM</font> table1<br>CROSS JOIN table2<br> <br>name1 table1x table1y name2 table2x table2y<br>a     <font color="#ff6060">100</font>     <font color="#ff6060">200</font>     a     <font color="#ff6060">10</font>      <font color="#ff6060">20</font><br>a     <font color="#ff6060">100</font>     <font color="#ff6060">200</font>     b     <font color="#ff6060">30</font>      <font color="#ff6060">40</font><br>a     <font color="#ff6060">100</font>     <font color="#ff6060">200</font>     c     <font color="#ff6060">50</font>      <font color="#ff6060">60</font><br>a     <font color="#ff6060">100</font>     <font color="#ff6060">200</font>     d     <font color="#ff6060">70</font>      <font color="#ff6060">80</font><br>b     <font color="#ff6060">200</font>     <font color="#ff6060">400</font>     a     <font color="#ff6060">10</font>      <font color="#ff6060">20</font><br>b     <font color="#ff6060">200</font>     <font color="#ff6060">400</font>     b     <font color="#ff6060">30</font>      <font color="#ff6060">40</font><br>b     <font color="#ff6060">200</font>     <font color="#ff6060">400</font>     c     <font color="#ff6060">50</font>      <font color="#ff6060">60</font><br>b     <font color="#ff6060">200</font>     <font color="#ff6060">400</font>     d     <font color="#ff6060">70</font>      <font color="#ff6060">80</font><br>e     <font color="#ff6060">400</font>     <font color="#ff6060">800</font>     a     <font color="#ff6060">10</font>      <font color="#ff6060">20</font><br>e     <font color="#ff6060">400</font>     <font color="#ff6060">800</font>     b     <font color="#ff6060">30</font>      <font color="#ff6060">40</font><br>e     <font color="#ff6060">400</font>     <font color="#ff6060">800</font>     c     <font color="#ff6060">50</font>      <font color="#ff6060">60</font><br>e     <font color="#ff6060">400</font>     <font color="#ff6060">800</font>     d     <font color="#ff6060">70</font>      <font color="#ff6060">80</font><br></font></span></pre></div>The number of resulting rows is the product of the number of rows from each table.  A CROSS JOIN can produce gigantic result sets inadvertently so be careful. <br><br>With your original example where you just had "FROM table1, table2", that is shorthand for a CROSS JOIN. You happened to get the expected result because table1 only had 1 row.  When it was CROSS JOINed to table 2, there was only 1 row that had table2.name = 'a'.  With my example data, you would have 3 rows, one for table1.a, table1.b, and table1.e and you'd get a very different result (2190 vs 330).<br><br>What you would want to do is a INNER JOIN or RIGHT INNER JOIN.<br><small>sql code</small>:<div class="syntax"><pre><span class="codetext"><font color="#FFFFFF"><font color="#ffff00">SELECT</font> SUM( table1.x + table2.x + table1.y + table2.y ) <font color="#ff40ff">AS</font> sum<br><font color="#ff40ff">FROM</font> table1<br>INNER JOIN table2 <font color="#ff40ff">on</font> table1.name = table2.name<br><font color="#ff40ff">WHERE</font> table2.name =  <font color="#ff6060">'a'</font><br> <br>sum<br><font color="#ff6060">330</font><br> <br><font color="#ffff00">SELECT</font> SUM( table1.x + table2.x + table1.y + table2.y ) <font color="#ff40ff">AS</font> sum<br><font color="#ff40ff">FROM</font> table1<br>LEFT JOIN table2 <font color="#ff40ff">on</font> table1.name = table2.name<br><font color="#ff40ff">WHERE</font> table2.name =  <font color="#ff6060">'c'</font><br> <br>sum<br><font color="#ff40ff">NULL</font><br><font color="#ffff00">SELECT</font> SUM( ISNULL(table1.x, <font color="#ff6060">0</font>) + table2.x + ISNULL(table1.y, <font color="#ff6060">0</font>) + table2.y ) <font color="#ff40ff">AS</font> sum<br><font color="#ff40ff">FROM</font> table1<br>RIGHT JOIN table2 <font color="#ff40ff">on</font> table1.name = table2.name<br><font color="#ff40ff">WHERE</font> table2.name =  <font color="#ff6060">'c'</font><br> <br>sum<br><font color="#ff6060">110</font><br></font></span></pre></div>The first example is the proper way to get 330 for record 'a' presuming name = a would appear only once in table2.  If a appeared multiple times, all 'a' rows would be added together.<br><br>The second example is looking at row c, which does not have a corresponding row in table 1.  That's why we get a NULL value.<br><br>The third example fixes the second example by wrapping the table1 values in an ISNULL() function.  Thsi function will use the 2nd parameter as the value if the first parameter is NULL.<br>]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/Re-mysql-practice-question-27808946</guid>
<pubDate>Tue, 11 Dec 2012 08:48:29 EDT</pubDate>
</item>

<item>
<title>mysql practice question</title>
<link>http://www.dslreports.com/forum/mysql-practice-question-27808653</link>
<description><![CDATA[loli posted : Hello. My semester is over and I'm studying sql for next semester ahead of time. This is not homework! I promise. I've reached this point that's driving me crazy.<br><br>I have 2 tables and I want to update the sum value as pictured below. Is the query okay btw? I understand I can just do <br><br><pre class="brush: text">SELECT SUM( table1.x + table2.x + table1.y + table2.y ) AS sum&#012;FROM table1, table2&#012;WHERE table2.name =  'a'&#012; &#012;</pre><!--end code block--><br>But I want to understand the different kinds of JOINS I guess.<br><br>So yeah. I want to update sum so that for name = a, it is 330. name = b, it is 370 and so on. I'm supposed to use some form of nested query here? <br><br>[att=1]<br><br>Sites I use for practice<br>&raquo;<A HREF="http://www.sqlcourse.com/index.html" >www.sqlcourse.com/index.html</A><br>&raquo;<A HREF="http://www.sqlcourse2.com/index.html" >www.sqlcourse2.com/index.html</A><br>&raquo;<A HREF="http://sqlzoo.net/" >sqlzoo.net/</A><br><br>Any recommendations would be good too :)<div class="borderless"><TABLE WIDTH=95% align=center border=0 CELLPADDING=4"><TR><TD ALIGN=CENTER VALIGN=CENTER BGCOLOR=#FFFFFF nwrap COLSPAN=3 WIDTH=100%><A HREF="/speak/slideshow/27808653?c=2058326&ret=L2ZvcnVtL3IyNzgwODY1My54bWw%3D"><IMG class="apic" BORDER=0 TITLE="89855 bytes" WIDTH=600 HEIGHT=466 SRC="/r0/download/2058326.thumb600~a5770da425db0bd4d1c397badb944075/asd.jpg/thumb.jpg" ALT="Click for full size"></A></TD></TABLE></div>]]></description>
<guid isPermaLink="true">http://www.dslreports.com/forum/mysql-practice-question-27808653</guid>
<pubDate>Tue, 11 Dec 2012 04:07:09 EDT</pubDate>
</item>

</channel>
</rss>
