Return To Table of Contents
Return To Table of Contents
database systems. SQL was developed by IBM in the early 1970s for use in System R, and is "de facto" standard, as well as an ISO and ANSI standard. In common usage, SQL supports four types of SQL statements: Queries: This type of SQL statement implements the standard relational operations such as SELECTION, PROJECTION and JOIN. The SELECT statement forms the basis for many other SQL operations. You can use SELECT statements to query tables, create views, in subqueries, or when creating a table defined as the result set of query. Data Manipulation Language (DML): DML statements are used to allow data in the database to be added, amended or deleted. DML functionality is provided by the use of INSERT, UPDATE and DELETE statements. Data Definition Language (DDL): This is the language that allows for objects to be created or altered. Provided below are a few example Oracle DDL statements: CREATE TABLE DROP TABLE ALTER TABLE CREATE INDEX DROP INDEX ALTER INDEX CREATE VIEW CREATE TYPE
Data Control Language (DCL): This is the language that allows for transaction control. Provided below are a few example Oracle DCL statements: COMMIT SAVEPOINT ROLLBACK SET TRANSACTION
The development of SQL is governed by standards. A major revision of the SQL standard was completed in 1992, called SQL2. SQL3 is a newer revision that supports object extensions and will be partially implemented starting with Oracle8.
How can I transform a subquery involving the IN clause to a Join? [ Return to Table of Contents ] Original statement:
SELECT e.empno, e.ename FROM emp e WHERE e.deptno IN ( SELECT d.deptno FROM dept d WHERE d.loc = 'CHICAGO' );
New statement: SELECT e.empno, e.ename FROM emp e, dept d WHERE d.loc = 'CHICAGO' AND d.deptno = e.deptno;
How can I transform a statement involving an OR condition to a UNION ALL? [ Return to Table of Contents ] Original statement:
SELECT FROM WHERE OR dname, loc dept loc = 'CHICAGO' loc = 'NEW YORK';
New Statement:
SELECT dname, loc FROM dept WHERE loc = 'CHICAGO' UNION ALL SELECT dname, loc FROM dept WHERE loc = 'NEW YORK';
How do I eliminate duplicate values in a table? [ Return to Table of Contents ] Provided below are four methods for identifying or removing duplicate rows from a table: Method 1:
DELETE FROM emp a WHERE rowid > ( SELECT min(rowid) FROM emp b WHERE a.emp_id = b.emp_id );
Method 2:
CREATE TABLE emp2 AS SELECT distinct * FROM emp; DROP TABLE emp;
Method 3:
DELETE FROM emp WHERE rowid NOT IN ( SELECT MIN(rowid) FROM emp GROUP BY emp_id );
Method 4:
DELETE FROM emp a WHERE EXISTS ( SELECT 'true' FROM emp b WHERE b.emp_id = a.emp_id AND b.rowid < a.rowid );
NOTE: If you where to create an index on the joined fields in the inner loop, it may be possible to eliminate N^2 operations as there would be no need to loop through the entire table on each pass be a record.
How can I get a count of the different data values in a column? [ Return to Table of Contents ]
SELECT dname , sum(decode(job, 'CLERK', , sum(decode(job, 'SALESMAN', , sum(decode(job, 'MANAGER', , sum(decode(job, 'ANALYST', , sum(decode(job, 'PRESIDENT', FROM emp e , dept d WHERE e.deptno (+) = d.deptno GROUP BY dname;
1, 1, 1, 1, 1,
DNAME CLERK SALESMAN MANAGER ANALYST PRESIDENT -------------- ---------- ---------- ---------- ---------- ---------ACCOUNTING 1 0 1 0 1 OPERATIONS 0 0 0 0 0 RESEARCH 2 0 1 2 0 SALES 1 4 1 0 0
How can I get count/sum RANGES of data values in a column? [ Return to Table of Contents ]
A value "x" will be between values "y" and "z" if: GREATEST(x,y)=LEAST(x,z).
SELECT job , sum(decode(greatest(sal,2999), least(sal,6000), 1, 0)) "Range 30006000" , sum(decode(greatest(sal,1000), least(sal,2999), 1, 0)) "Range 10003000" , sum(decode(greatest(sal,0), least(sal,999), 1, 0)) "Range 0-1000" FROM emp GROUP BY job; JOB Range 3000-6000 Range 1000-3000 Range 0-1000 --------- --------------- --------------- -----------ANALYST 2 0 0 CLERK 0 2 2 MANAGER 0 3 0 PRESIDENT 1 0 0 SALESMAN 0 4 0
How can I get the time difference between two date columns? [ Return to Table of Contents ] Method 1: (Very long-winded. A more efficient version is indicated in Method 2.)
SELECT floor(((e1.hiredate-e2.hiredate)*24*60*60)/3600) || ' HOURS ' || floor((( (e1.hiredate-e2.hiredate)*24*60*60) floor(((e1.hiredatee2.hiredate)*24*60*60)/3600)*3600)/60) || ' MINUTES ' || round((( (e1.hiredate-e2.hiredate)*24*60*60) floor(( (e1.hiredate-e2.hiredate)*24*60*60)/3600)*3600 (floor((((e1.hiredate-e2.hiredate)*24*60*60) floor(((e1.hiredatee2.hiredate)*24*60*60)/3600)*3600)/60)*60)) ) || ' SECONDS ' time_difference FROM emp e1, emp e2 WHERE e1.ename = 'BLAKE' AND e2.ename = 'CLARK' TIME_DIFFERENCE ------------------------------162 HOURS 44 MINUTES 25 SECONDS
Method 2: (Thanks to Chris Hunt, http://www.extracon.com/, for providing me with this much easier and efficient SQL query to produce the same results!)
SELECT floor((e1.hiredate-e2.hiredate)*24) || ' HOURS ' || mod(floor((e1.hiredate-e2.hiredate)*24*60),60) || ' MINUTES ' || mod(floor((e1.hiredate-e2.hiredate)*24*60*60),60) || ' SECONDS ' time_difference
Can I retrieve only the Nth row from a table? [ Return to Table of Contents ] Method 1:
SELECT ename , job , hiredate FROM emp WHERE rowid = ( SELECT rowid WHERE rownum MINUS SELECT rowid WHERE rownum );
Method 2:
SELECT ename , job , hiredate FROM emp WHERE rownum = 1 AND rowid NOT IN ( SELECT rowid FROM emp WHERE rownum < 3 );
emp WHERE rowid in ( SELECT rowid WHERE rownum MINUS SELECT rowid WHERE rownum );
Can I retrieve EVERY Nth row from a table? [ Return to Table of Contents ] Method 1:
SELECT ename , job , hiredate FROM emp WHERE (rowid,0) in ( SELECT rowid, mod(rownum,4) FROM emp );
Retrieve the TOP N Rows from a table? [ Return to Table of Contents ] Method 1: Starting with Oracle8i, you can have an inner-query with an ORDER BY clause
SELECT ename , job , hiredate , sal FROM ( SELECT empno, ename, job, hiredate, sal FROM emp ORDER BY sal DESC ) WHERE rownum < 6;
SELECT ename , job , hiredate , sal FROM emp a WHERE 5 >= ( SELECT count(distinct b.sal) FROM emp b WHERE b.sal >= a.sal ) ORDER BY a.sal DESC
Is it possible to dump/examine the exact content of a database column? [ Return to Table of Contents ]
SELECT ename, job, dump(job) FROM emp WHERE ename = 'SMITH'; ENAME JOB ---------- --------SMITH CLERK DUMP(JOB) --------------------------Typ=1 Len=5: 67,76,69,82,75
NOTE: Using the "EMP" table from the SCOTT/TIGER schema makes a great test table since it has a "self-referencing" relation. (The MGR column contains the employee number of the "current" employee's boss. The LEVEL pseudo-column is an indication of hwo deep in the tree you are. Oracle can handle queries with a depth up to 255 levels. The "START WITH" clause is used to specify the start of the tree. More than one record can match the starting condition. One disadvantage of having a "CONNECT BY PRIOR" clause is that you cannot perform a JOIN to other tables.