<?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>Oracle Database Tips blog &#187; sql select</title>
	<atom:link href="http://oracle-database-tips.com/wp/tag/sql-select/feed" rel="self" type="application/rss+xml" />
	<link>http://oracle-database-tips.com/wp</link>
	<description>News for Oracle professionals</description>
	<lastBuildDate>Fri, 11 May 2012 12:12:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Oracle SQL Select</title>
		<link>http://oracle-database-tips.com/wp/sql/oracle-sql-select</link>
		<comments>http://oracle-database-tips.com/wp/sql/oracle-sql-select#comments</comments>
		<pubDate>Fri, 23 Oct 2009 10:55:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[boston time]]></category>
		<category><![CDATA[chicago operations]]></category>
		<category><![CDATA[correct syntax]]></category>
		<category><![CDATA[oracle select]]></category>
		<category><![CDATA[select syntax]]></category>
		<category><![CDATA[sql select]]></category>
		<category><![CDATA[synonym]]></category>

		<guid isPermaLink="false">http://oracle-database-tips.com/wp/?p=64</guid>
		<description><![CDATA[This tutorial will show you how to use the correct syntax to query data in the Oracle database.]]></description>
			<content:encoded><![CDATA[<h1></h1>
<h3>How to query the Oracle database</h3>
<p>Always test these &#8216;select&#8217; commands on a test server<br />
before implementing them on a production server.</p>
<p>This tutorial will show you how to use the correct syntax to query data          in the Oracle database.</p>
<p>When you query an object, you could be querying a table, a synonym or          a view,<br />
but this will make no difference to the syntax you need to use or to the          result you will obtain.</p>
<p>A table consists of a number of columns and rows and your query          will be built so that you decide which rows and which columns to return.</p>
<p>The simplified syntax for the queries we&#8217;ll be using in this          tutorial is as follows:</p>
<p>select &lt;column list&gt; from &lt;table&gt; where &lt;row criteria&gt;          ;</p>
<p>For example: (log in as scott/tiger)</p>
<p>Table DEPT looks like this :</p>
<table border="1" cellspacing="0" cellpadding="0" width="400">
<tbody>
<tr>
<td width="73"><strong>DEPTNO </strong></td>
<td width="62"><strong>DNAME </strong></td>
<td width="57"><strong>LOC</strong></td>
</tr>
<tr>
<td>10</td>
<td>ACCOUNTING</td>
<td>NEW YORK</td>
</tr>
<tr>
<td>20</td>
<td>RESEARCH</td>
<td>DALLAS</td>
</tr>
<tr>
<td>30</td>
<td>SALES</td>
<td>CHICAGO</td>
</tr>
<tr>
<td>40</td>
<td>OPERATIONS</td>
<td>BOSTON</td>
</tr>
</tbody>
</table>
<p>There are 3 columns : DEPTNO, DNAME and LOC<br />
There are 4 rows.</p>
<p><strong>Column criteria</strong></p>
<p>Let&#8217;s query all columns and all rows:<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select deptno, dname, loc from dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --> Time saving tip: use * instead of the whole column list if you plan to          return each column<br />
in the table:</p>
<p>Let&#8217;s only query the Location column in the table.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select loc from dept;

LOC
-------------
NEW YORK
DALLAS
CHICAGO
BOSTON</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   -->Now let&#8217;s query both the DNAME and LOC columns from this table:<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select dname, loc from dept;

DNAME          LOC
-------------- -------------
ACCOUNTING     NEW YORK
RESEARCH       DALLAS
SALES          CHICAGO
OPERATIONS     BOSTON</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --> <strong>Row criteria</strong></p>
<p>To specify only a subset of rows to be returned,<br />
you will use the WHERE clause.</p>
<p>Let&#8217;s query only the rows in the dept table belonging to the SALES department.<br />
We will query all columns.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select * from dept where dname = 'SALES';

    DEPTNO DNAME          LOC
---------- -------------- -------------
        30 SALES          CHICAGO</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   -->We could also decide to make the DEPTNO the criteria to deliver the same        result.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select * from dept where deptno = 30;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        30 SALES          CHICAGO</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   -->What if we want to query all rows related to departments that have department        numbers larger than 20 ?<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select * from dept where deptno &gt; 20;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --> What if you want to make your <em>where</em> clause more complex ?</p>
<p>Let&#8217;s look for any rows that have &#8216;SALES&#8217; as a department name but also          any rows that have &#8216;BOSTON&#8217; as the location.</p>
<p>This means that<br />
any row contains the value &#8216;SALES&#8217; in the DNAME column,<br />
as well as<br />
any row containing the value &#8216;BOSTON&#8217; in the LOC column<br />
will be included in the result set.</p>
<p>You can do so by adding another section to the <em>where</em> clause and          using the &#8216;OR&#8217; term to glue them together.<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select * from dept
     where dname = 'SALES' or loc = 'BOSTON';

    DEPTNO DNAME          LOC
---------- -------------- -------------
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --><br />
If you want your result set to return only rows<br />
where the location is &#8216;BOSTON&#8217;<br />
and<br />
the department name is &#8216;SALES&#8217;,<br />
you will see 0 rows returned because no row has both those values for the        relevant columns :<br />
<!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; select * from dept
     where dname = 'SALES' and loc = 'BOSTON';

no rows</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --> <strong>Note on datatypes<br />
</strong> Your <em>where</em> clause will have to be aware of which type          of data you are using as query criteria.<br />
In other words, you must be sure to use the correct datatype in your <em>where</em> clause.</p>
<p>For example, DNAME is a varchar column, so you must put single quotes          around your query criteria.<br />
(Only number datatypes do not need single quotes)</p>
<p>If you try to specify SALES in your <em>where</em> clause like this          :</p>
<p><em>where dname = SALES;</em></p>
<p>you will receive an error:</p>
<p><em> ORA-00904: &#8220;SALES&#8221;: invalid identifier</em></p>
<p>because you are expected to supply single quotes around a varchar datatype          like this:</p>
<p><em> where dname = &#8216;SALES&#8217;;</em></p>
<p><strong>How do you know the datatype of a column in a table ?</strong></p>
<p>Use DESCRIBE to see which columns are in a table and what their datatypes          are:</p>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --></p>
<div id="textbox">
<div>
<pre class="large">SQL&gt; desc dept;
 Name                Null?    Type
 ------------------- -------- -----------------

 DEPTNO                       NUMBER(2)
 DNAME                        VARCHAR2(14)
 LOC                          VARCHAR2(13)</pre>
</div>
</div>
<p><!-- ccccccccccccccccccccccccccccccccccccccccccccccccc   --> <strong>Varchar tip </strong><br />
Oracle is case sensitive to data inside the tables,<br />
unlike column and table names which are usually upper case unless you          explicitly create them otherwise.</p>
<p>So, there is a difference between &#8216;SALES&#8217;, &#8216;sales&#8217; and even &#8216;Sales&#8217;.</p>
<p>If you are unsure of the case that is used in your data, you can force          the result to upper (or lower)<br />
case by calling the <em>upper</em> or <em>lower </em><a title="opens new window" href="http://www.oracle-database-tips.com/oracle_functions.html" target="_blank">function</a> to convert the column data, like this:</p>
<p>&#8230; where lower(dname) = &#8216;sales&#8217;;.</p>
<p><!-- ============================================================================================================================================================ --></p>
<h3>Recommended reading:</h3>
<p>Spend some time reading Oracle&#8217;s documentation to get comfortable with Oracle&#8217;s        syntax.</p>
<p>It will be worth the effort.</p>
<div id="content">
<li><a title="opens new window" onclick="window.open('http://www.oracle-database-tips.com/cgi-bin/counter.pl?url=http%3A%2F%2Fdownload.oracle.com%2Fdocs%2Fcd%2FB19306_01%2Fserver.102%2Fb14200%2Fstatements_10002.htm%23i2065646&amp;referrer=http%3A%2F%2Fwww.oracle-database-tips.com%2Fselect.html'); return false;" href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_10002.htm#i2065646" target="_blank">Oracle          Online Documentation: SQL Reference</a></li>
<li><a title="opens new window" onclick="window.open('http://www.oracle-database-tips.com/cgi-bin/counter.pl?url=http%3A%2F%2Fdownload.oracle.com%2Fdocs%2Fcd%2FB19306_01%2Fserver.102%2Fb14200%2Fqueries.htm%23i2068094&amp;referrer=http%3A%2F%2Fwww.oracle-database-tips.com%2Fselect.html'); return false;" href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries.htm#i2068094" target="_blank">Oracle Online Documentation : SQL Reference, SQL Queries and Subqueries</a></li>
</div>
]]></content:encoded>
			<wfw:commentRss>http://oracle-database-tips.com/wp/sql/oracle-sql-select/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

