Sunteți pe pagina 1din 2

Retrieve Data from Single Table.

/*Functions*/
--SUBSTR
desc vendors
Name Null? Type
----------------------------------------- -------- ------------
VENDOR_ID NOT NULL NUMBER
VENDOR_NAME NOT NULL VARCHAR2(50)
VENDOR_ADDRESS1 VARCHAR2(50)
VENDOR_ADDRESS2 VARCHAR2(50)
VENDOR_CITY NOT NULL VARCHAR2(50)
VENDOR_STATE NOT NULL CHAR(2)
VENDOR_ZIP_CODE NOT NULL VARCHAR2(20)
VENDOR_PHONE VARCHAR2(50)
VENDOR_CONTACT_LAST_NAME VARCHAR2(50)
VENDOR_CONTACT_FIRST_NAME VARCHAR2(50)
DEFAULT_TERMS_ID NOT NULL NUMBER
DEFAULT_ACCOUNT_NUMBER NOT NULL NUMBER

SQL> select vendor_contact_first_name,vendor_contact_last_name,


SUBSTR(vendor_contact_first_name,1,1)||SUBSTR(vendor_contact_last_name,1,1) as
intials
from vendors;
resultset
VENDOR_CONTACT_FIRST_NAME VENDOR_CONTACT_LAST_NAME
IN
-------------------------------------------------- -----------------------------
--------------------
Val Pinsippi
VP
Robert Aranovitch
RA
Fyodor Finklestein
FF
Sam Smith
SS
Mercedez Articunia
MA
Harold Spivak
HS

Dual Table
This table is useful for testing expressions that use literal values,arthematic
calculations and functions.
Distinct
--The distinct Keyword prevents duplicate(identical) rows from being included in
the reslut set.
--The ALL keyword causes all rows matching the search condition to be included i
n the result set.
Rownum Pseudo column to limit number of rows.
--You can use rownumR column to limit the number of rows included in the result
set.
--If you want to sort the result set before you use the ROWNUM pseudo column to
limit
the number of rows included in the result set, go for subquery.
sel
Extract first five rows
SQL>select * from invoices where rownum<=5;
Extract first five rows then sorts the result
SQL>select vendor_id,invoice_total from invoices where rownum<=5 order by invoic
e_total desc
Sort the result set then extract the rows
SQL>select vendor_id,invoice_total from (select * from invoices order by invoice
_total desc)
where rownum<=5

Order By Clause
Used to sort data in asec or desc order.
You can sort by any column in the base table, regard less of whether it's includ
ed in the select clause
IF qUery includes the DISTINCT keyword. Then, you can sort by columns included i
n the select clause.
You can also sort data in result set using the result set.
TOP N Queries
In order to select employees with the highest five salaries, you have to force s
orting and then apply ROWNUM condition. Here is the syntax for a top-N query whe
re N = 5 (this syntax with the subquery works only in Oracle 8i):
SELECT Empno, Ename, Job, Mgr, Hiredate, Sal
FROM
(SELECT Empno, Ename, Job, Mgr, Hiredate, Sal
FROM Emp
ORDER BY NVL(Sal, 0) DESC)
WHERE ROWNUM < 6;

S-ar putea să vă placă și