Basic Select Statement


Rules and Guidelines on Writing SQL Statements

SQL statement is not case sensitive and can be entered on one or many lines. Clauses are placed on separate lines for readability and easy of editing. Oracle will recognize SQL statements even if it entered in mix format (lowercase and uppercase), but if we entered all the SQL statement in a standard form, we can construct a valid statements that easy to read and easy to edit.

A Select statement is used to retrieves Data from a table in the Database.

Syntax :

SELECT [DISTINCT] (*, column_name [alias],...)

FROM table_name;


The SELECT keyword choose which columns in a table that is returned by a query and FROM keyword identifies from which table the data should be picked up.



Selecting All Columns


we gonna get started with selecting all column in the SCOTT.dept table. to display all column in a table you must use an asterisk (*) character right after the SELECT keyword.



SELECT * FROM dept;

DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON


You can also mentioned all the column name separated by commas, after the SELECT keyword.



SELECT deptno, dname, loc
FROM dept;


DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON




Selecting Specific Columns


You can select specific column that you want to display in the query result by specifies the column name separated by commas, like the previous example



SELECT deptno, dname
FROM dept;


DEPTNO DNAME
---------- --------------
10 ACCOUNTING
20 RESEARCH
30 SALES
40 OPERATIONS



Changing Column Title in the Query Output


You can change a column title in the query output using an alias for a column name that less descriptive or if you are using calculation in the query that produce a long column name.



Without Using Column Alias :


SELECT Ename, sal, comm, sal+comm
FROM emp;


ENAME SAL COMM SAL+COMM
---------- ---------- ---------- ----------
SMITH 800
ALLEN 1600 300 1900
WARD 1250 500 1750
JONES 2975
MARTIN 1250 1400 2650
. . . . . . . . . . . .
. . . . . . . . . . . .

14 rows selected.



Using Column Alias :



SELECT Ename, sal, comm, sal+comm AS Total
FROM emp;


ENAME SAL COMM Total
---------- ---------- ---------- ----------
SMITH 800
ALLEN 1600 300 1900
WARD 1250 500 1750
JONES 2975
MARTIN 1250 1400 2650
. . . . . . . . . . . .
. . . . . . . . . . . .

14 rows selected.



If the alias column has more than one word that separated by space, you must quote the alias column. Example :



SELECT Ename, sal, comm, sal+comm AS "Paid Salary"
FROM emp;


ENAME SAL COMM Paid Salary
---------- ---------- ---------- -----------
SMITH 800
ALLEN 1600 300 1900
WARD 1250 500 1750
JONES 2975
MARTIN 1250 1400 2650
. . . . . . . . . . . .
. . . . . . . . . . . .

14 rows selected.




Displaying Unique Values with SQL Distinct Keywords


Distinct keyword will return the unique values from specified column and remove the duplicate values from the query result. First we are going to display all rows in the EMP table :



SELECT empno,ename,deptno
FROM emp;


EMPNO ENAME DEPTNO
------ ---------- ----------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7782 CLARK 10
7788 SCOTT 20
7839 KING 10
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20
7934 MILLER 10

14 rows selected.



Now we are going to display how many unique department in the EMP table using SQL Distinct keyword :



SELECT distinct(Deptno) as "Unique Dept"
FROM emp;


Unique Dept
-----------
10
20
30




Combining Column Using Concatenation Operator



Concatenation Operator is used to combine two or more column into a single column in the query output. Concatenation in oracle signed by two "||" (pipe) character. We can see how this operator work using a simple example bellow.



SELECT ename||' Work as a/an '||job
as "Employees Information"
FROM emp;

Employees Information
---------------------------------
SMITH Work as a/an CLERK
ALLEN Work as a/an SALESMAN
WARD Work as a/an SALESMAN
. . . .
. . . .

14 rows selected.



More Example :



SELECT ename||' Work as a/an '||job||
' with $'||sal||' monthly salary'
as "Employees Information"
FROM emp;

Employees Information
-----------------------------------------------------
SMITH Work as a/an CLERK with $800 monthly salary
ALLEN Work as a/an SALESMAN with $1600 monthly salary
WARD Work as a/an SALESMAN with $1250 monthly salary
. . . .
. . . .

14 rows selected.


Using SQL*Plus Console

SQL*Plus is an interactive command-line interface to execute SQL Statement, Creating PL/SQL Program script, creating SQL Result output to a file, and creating report. SQL*Plus installed automaticly when we install Oracle Database in our computer. Since 10g version of Oracle Database, oracle provide iSQL*Plus, which similar as SQL*Plus but the main different is iSQL*Plus is a web-based program and only accessible via web browser. SQL*Plus Commands can be divided into three categories :



  1. Execute Commands

  2. Editing Commands

  3. Formatting and Reporting Commands



Running SQL Plus from the Run menu in Windows OS


You can run SQL Plus console using Run dialog box instead of running it from start menu in the Microsoft Windows operating system.



  • Start | Run | in the run dialog type SQLPLUSW and then press enter



Logon to the Database


Before you can connect to a database you will be prompted a login dialog box. To logon to a database you can do with several way



Method 1 : type Username, Password, and Connect Identifier in each textbox.

Method 2 : type with the following format : Username/Password@connect_identifier in the username textbox.

example : scott/tiger@ocp


SQL*Plus Login Dialog

SQL*Plus Commands


Every SQL Statement which executed in SQL*Plus console will be stored in the SQL buffer by Oracle. SQL Buffer stored the last executed Query. Bellow is a list of SQL*Plus Commands which often used in the SQL*Plus Console :




  • / : Execute the last SQL Command in the SQL Buffer

  • [R]UN : Display and Execute the last SQL Command in the SQL Buffer

  • @ dan @@ : execute SQL command stored in a file.

  • HELP INDEX : Displaying the lists of SQL*Plus Help Index

  • SPOOL : Stores query results in an operating system file, or sends the file to a printer.
    SPO[OL] [file_name[.ext] | OFF | OUT]


  • EXECUTE : Executes a single PL/SQL statement. The EXECUTE command is often useful when you want to execute a PL/SQL statement that references a stored procedure. EXEC[UTE] statement

  • SAVE : Saves the contents of the SQL buffer in a host operating system command file. SAV[E] file_name[.ext] [CRE[ATE] | REP[LACE] | APP[END]]

  • CONNECT : Connects a given username to Oracle. CONN[ECT] [logon] [AS [SYSOPER|SYSDBA]]. where logon has the following syntax:
    username[/password] [@connect_identifier] | /

  • DISCONNECT : Commits pending changes to the database and logs the current username out of Oracle, but does not exit SQL*Plus. Use EXIT or QUIT to log out of Oracle and return control to your host computer's operating system. DISC[ONNECT]



For the complete help index and explanation about specific command, in the SQL*Plus Console type a command you want to get the explanation after HELP keyword. example : HELP SPOOL




Re-Login as a Different User


For example you already login as Scott, and want to logout and login again as user HR using the same SQL Plus console window. first you must disconnected user Scott from the Database by typing disconnect; in the SQL Plus console window and then type login parameter with the following format : Connect Username/Password@connect_identifier;
example : hr/hr@doe;
please note that you must add a ";" character at the end of a command to execute them.


reconnect



Setting Linesize and Pagesize


Pagesize : Display how many rows per page. Syntax : SET pagesize value.
bellow is an example using Pagesize to display all the rows from the Jobs table in the HR schema in one page


Pagesize using SQL*Plus default value :






Pagesize after SET by 50 rows per page :






Linesize : Display how many character per line. Syntax : SET linesize value.
bellow is an example using Linesize to adjust the line width.


Linesize using SQL*Plus default value :






Linesize after SET by 200 characters per line :