Oracle SQL FAQ: Topics
Oracle SQL FAQ: Topics
Oracle SQL FAQ: Topics
Topics
What is SQL and where does it come from? What are the difference between DDL, DML and DCL commands? How does one escape characters when building SQL queries? How does one eliminate duplicate rows from a table? How does one generate primary key values for a table? How does one get the time difference between two date columns? How does one add a day/hour/minute/second to a date value? How does one count different data values in a column? How does one count/sum RANGES of data values in a column? Can one retrieve only the Nth row from a table? Can one retrieve only rows X to Y from a table? How does one select EVERY Nth row from a table? How does one select the TOP N rows from a table? How does one code a tree-structured query? How does one code a matrix report in SQL? How does one implement IF-THEN-ELSE in a select statement? How can one dump/ examine the exact content of a database column? Can one drop a column from a table? Can one rename a column in a table? How can I change my Oracle password? How does one find the next value of a sequence? Workaround for snapshots on tables with LONG columns Where can one get more info about SQL? Back to Oracle FAQ Index
The development of SQL is governed by standards. A major revision to the SQL standard was completed in 1992, called SQL2. SQL3 support object extensions and are (partially?) implemented in Oracle8 and 9. Back to top of file
What are the difference between DDL, DML and DCL commands?
DDL is Data Definition Language statements. Some examples: CREATE - to create objects in the database ALTER - alters the structure of the database DROP - delete objects from the database TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed COMMENT - add comments to the data dictionary GRANT - gives user's access privileges to database REVOKE - withdraw access privileges given with the GRANT command
DML is Data Manipulation Language statements. Some examples: SELECT - retrieve data from the a database INSERT - insert data into a table UPDATE - updates existing data within a table DELETE - deletes all records from a table, the space for the records remain CALL - call a PL/SQL or Java subprogram EXPLAIN PLAN - explain access path to data LOCK TABLE - control concurrency
DCL is Data Control Language statements. Some examples: COMMIT - save work done SAVEPOINT - identify a point in a transaction to which you can later roll back ROLLBACK - restore database to original since the last COMMIT SET TRANSACTION - Change transaction options like what rollback segment to use
How does one escape special characters when building SQL queries?
The LIKE keyword allows for string searches. The '_' wild card character is used to match exactly one character, '%' is used to match zero or more occurrences of any characters. These characters can be escaped in SQL. Example: SELECT name FROM emp WHERE id LIKE '%\_%' ESCAPE '\'; Use two quotes for every one displayed. Example: SELECT 'Franks''s Oracle site' FROM DUAL; SELECT 'A ''quoted'' word.' FROM DUAL;
SELECT 'A ''''double quoted'''' word.' FROM DUAL; Back to top of file
How does one get the time difference between two date columns?
Look at this example query: select floor(((date1-date2)*24*60*60)/3600)
from
|| ' HOURS ' || floor((((date1-date2)*24*60*60) floor(((date1-date2)*24*60*60)/3600)*3600)/60) || ' MINUTES ' || round((((date1-date2)*24*60*60) floor(((date1-date2)*24*60*60)/3600)*3600 (floor((((date1-date2)*24*60*60) floor(((date1-date2)*24*60*60)/3600)*3600)/60)*60))) || ' SECS ' time_difference ...
If you don't want to go through the floor and ceiling math, try this method (contributed by Erik Wile): select to_char(to_date('00:00:00','HH24:MI:SS') + (date1 - date2), 'HH24:MI:SS') time_difference from ... Note that this query only uses the time portion of the date and ignores the date itself. It will thus never return a value bigger than 23:59:59. Back to top of file
Shaik Khaleel provided this solution to the problem: SELECT * FROM ( SELECT ENAME,ROWNUM RN FROM EMP WHERE ROWNUM < 101 ) WHERE RN between 91 and 100 ; Note: the 101 is just one greater than the maximum row of the required rows (means x= 90, y=100, so the inner values is y+1). Another solution is to use the MINUS operation. For example, to display rows 5 to 7, construct a query like this: SELECT * FROM tableX WHERE rowid in ( SELECT rowid FROM tableX WHERE rownum <= 7 MINUS SELECT rowid FROM tableX WHERE rownum < 5); Please note, there is no explicit row order in a relational database. However, this query is quite fun and may even help in the odd situation. Back to top of file
WHERE b.maxcol >= a.maxcol) ORDER BY maxcol DESC; Back to top of file
JOB DEPT10 DEPT20 DEPT30 DEPT40 --------- ---------- ---------- ---------- ---------ANALYST 6000 CLERK 1300 1900 950 MANAGER 2450 2975 2850
5000
5600
tableX;
select decode( GREATEST(A,B), A, 'A is greater OR EQUAL than B', 'B is greater than A')... select decode( GREATEST(A,B), A, decode(A, B, 'A NOT GREATER THAN B', 'A GREATER THAN B'), 'A NOT GREATER THAN B')... Note: The decode function is not ANSI SQL and is rarely implemented in other RDBMS offerings. It is one of the good things about Oracle, but use it sparingly if portability is required. From Oracle 8i one can also use CASE statements in SQL. Look at this example: SELECT ename, CASE WHEN sal>1000 THEN 'Over paid' ELSE 'Under paid' END FROM emp; Back to top of file
How can one dump/ examine the exact content of a database column?
SELECT DUMP(col1) FROM tab1 WHERE cond1 = val1; DUMP(COL1) ------------------------------------Typ=96 Len=4: 65,66,67,32 For this example the type is 96, indicating CHAR, and the last byte in the column is 32, which is the ASCII code for a space. This tells us that this column is blank-padded. Back to top of file
Other workarounds: 1. SQL> update t1 set column_to_drop = NULL; SQL> rename t1 to t1_base; SQL> create view t1 as select <specific columns> from t1_base; 2. SQL> create table t2 as select <specific columns> from t1; SQL> drop table t1; SQL> rename t2 to t1; Back to top of file