<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indiana University: IT Training Tips Blog &#187; SQL Programming</title>
	<atom:link href="http://ittrainingtips.iu.edu/category/sql-programming/feed" rel="self" type="application/rss+xml" />
	<link>http://ittrainingtips.iu.edu</link>
	<description>Tips and tricks to help you make the most of technology</description>
	<lastBuildDate>Tue, 21 May 2013 20:48:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Workshops: Install and Use PHP Locally</title>
		<link>http://ittrainingtips.iu.edu/sql-programming/php-workshops-install-and-use-php-locally/03/2013</link>
		<comments>http://ittrainingtips.iu.edu/sql-programming/php-workshops-install-and-use-php-locally/03/2013#comments</comments>
		<pubDate>Tue, 05 Mar 2013 20:51:57 +0000</pubDate>
		<dc:creator>Tom Mason</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Macintosh Skills]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://ittrainingtips.iu.edu/?p=5592</guid>
		<description><![CDATA[To develop database-driven PHP applications, we need three things, the Apache web server, the PHP processor, and MySQL. This tutorial will walk you through all of the installation and setup that you need to do on your own machine to have a local instance of a Apache/MySQL/PHP (AMP) environment. The instructions below will walk you [...]]]></description>
				<content:encoded><![CDATA[<p>To develop database-driven PHP applications, we need three things, the Apache web server, the PHP processor, and MySQL. This tutorial will walk you through all of the installation and setup that you need to do on your own machine to have a local instance of a Apache/MySQL/PHP (AMP) environment.</p>
<p>The instructions below will walk you through the setup you need for each of our PHP workshops, starting with PHP: The Basics.</p>
<p>At the time of writing, PHP: The Basics is the only PHP workshop offered by IT Training.</p>
<h3>PHP: The Basics</h3>
<p>The following links contain step-by-step instructions on configuring XAMP for the PHP: The Basics workshop.</p>
<ul>
<li><a href="http://ittrainingtips.iu.edu/?p=5801">Macintosh OS: Install and Configure MAMP for PHP Development</a>.</li>
<li><a href="http://ittrainingtips.iu.edu/?p=5799">Windows: Install and Configure WAMP for PHP Development</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://ittrainingtips.iu.edu/sql-programming/php-workshops-install-and-use-php-locally/03/2013/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How To Retrieve Top (n) Rows from a Database</title>
		<link>http://ittrainingtips.iu.edu/sql-programming/how-to-retrieve-top-n-rows-from-a-database/06/2009</link>
		<comments>http://ittrainingtips.iu.edu/sql-programming/how-to-retrieve-top-n-rows-from-a-database/06/2009#comments</comments>
		<pubDate>Fri, 19 Jun 2009 18:48:12 +0000</pubDate>
		<dc:creator>Amy Neymeyr</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[General News]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL Programming]]></category>

		<guid isPermaLink="false">http://ittrainingtips.iu.edu/sql-programming/how-to-retrieve-top-n-rows-from-a-database/06/2009</guid>
		<description><![CDATA[In SQL:Data Retrieval, one of the topics we cover is how to write a database query to answer questions like, &#8220;How many students were enrolled each term in Biology 101 from 1997-2008&#8243; or &#8220;What is the average salary for the employees who work in the Marketing department?&#8221; These types of questions can be answered by [...]]]></description>
				<content:encoded><![CDATA[<p>In <a target="_blank" href="http://ittraining.iu.edu/workshops/workshop_detail.aspx?workshop=87">SQL:Data Retrieval</a>, one of the topics we cover is how to write a database query to answer questions like, &#8220;How many students were enrolled each term in Biology 101 from 1997-2008&#8243; or &#8220;What is the average salary for the employees who work in the Marketing department?&#8221; These types of questions can be answered by using SQL <em>functions</em>. A function is a small bit of code that can accept a value, do something with it (like a calculation), and then return a new result.  In SQL, there are functions like AVG (to average) and COUNT (for, um, counting.  I bet you guessed that, though, right? <img src='http://ittrainingtips.iu.edu/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Some functions, like AVG and COUNT, are available in almost every database product. Other functions are database-specific, made available by the database vendor to make your life easier (and make the competitors&#8217; products less enticing as well). An example of one of these functions is the TOP function in Microsoft SQL Server.  Here&#8217;s a faux query, written for <strong>Microsoft SQL Server</strong>, to find the top 5 students in a class by their course grade:</p>
<p><code>SELECT TOP 5 (studentID), grade<br />
FROM students<br />
ORDER BY grade DESC</code></p>
<p>Theoretically, this query would return a list of 5 student IDs and their grades from the table named Students. The<code> ORDER BY</code> clause at the end of the statement ensures that, before the database picks the 5 at the top of the list, the list is sorted according to grade, with the best grades on top. (In other words, the <code>DESC</code> keyword tells the database to sort records in descending order.)</p>
<p>Unfortunately for people working in other databases, like Oracle or MySQL, the TOP function is specific to Microsoft SQL Server.  So, how can we find the top 5 students in other databases?</p>
<p>In <strong>MySQL</strong>, an equivalent statement would be:</p>
<p><code>SELECT studentID, grade<br />
FROM students<br />
ORDER BY grade DESC<br />
LIMIT 0,5</code></p>
<p>Here, the <code>LIMIT</code> command tells the database to display 5 records starting from  the 1st record (0), after sorting the records in the table according to the grades.  To display 15 records starting with the 5th record, the last line of the previous statement would be <code>LIMIT 4,15</code>.</p>
<p>Unfortunately, the solution for <strong>Oracle</strong> is not nearly as simple:</p>
<p><code>SELECT studentID, grade<br />
FROM (<br />
SELECT studentID, grade, RANK() OVER (ORDER BY grade DESC) rankByGrade<br />
FROM students<br />
)<br />
WHERE </code><code>rankByGrade</code><code> <= 5</code></p>
<p>Whoa!  What's going on here?  This is a demonstration of a <em>subquery</em>. A subquery is simply a SELECT statement nested inside another SQL statement.  In the above example, the interior SELECT statement (the one that starts on the 3rd line) will be executed first. Any results from the subquery will essentially be treated as a temporary table, existing for the duration of the statement's execution only. This temporary table is used by the exterior query (the <code>SELECT studentID, grade</code> part) as its source of data.</p>
<p>This particular subquery  is using an Oracle function called <code>RANK</code> in association with <code>(ORDER BY grade DESC) </code>to create a column that provides a ranking based on the grade value, from highest to lowest. The highest grade gets a rank of 1, the next highest grade gets a rank of 2, and so on.  In other words, if we were to run just the subquery by itself, the results might look something like:</p>
<p><code>studentID  grade    rankByGrade<br />
---------  -----    ------<br />
373478     100       1<br />
</code><code>938937     98.6      2<br />
</code><code>671037     98.2      </code><code>3<br />
</code><code>565422     96.8      </code><code>4</code><code><br />
</code></p>
<p>These results, including the derived <code>rankByGrade</code> number, are then used by the exterior query, which essentially is:<br />
<code>SELECT studentID, grade<br />
FROM [the results of the subquery]<br />
WHERE rankByGrade <= 5</code></p>
<p>Thus, the exterior query returns only 5 records, which correspond to the records that have a <code>rankByGrade</code> of 5 or less, or, in our case, the top 5 students.</p>
<p><strong>Thanks to Mike Halla, database guru, for providing a sample Oracle query to address this issue</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ittrainingtips.iu.edu/sql-programming/how-to-retrieve-top-n-rows-from-a-database/06/2009/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
