SQL Detail
SQL Detail
OPERATOR = > < >= <= <>,^=,!= BETWEEN ..AND MEANING EQUAL TO GREATER THAN LESS THAN GREATER THAN EQUAL TO LESS THAN EQUAL TO NOT EQUAL TO BETWEEN TWO VALUE(INCLUSIVE) (This function can be used in both either in character or numeric and date type) MATCH THE ANY OF THE LIST OF VALUE MATCH THE CHARACTER PATTERN
IN(Set) For e.g.: IN(100,101) e.g.: IN(ROSHAN,RAMESH) LIKE For e.g. first_name LIKE S%;(shows name starting from S) S_ show all with SS,SA,SB etc IS NULL NOT IN ( , )
IS A NULL VALUE RETURNS OTHER VALUE THAN GIVEN IN NOT IN CONDITION. NOT can be used with BETWEEN, IN, LIKE function also. IMP: SELECT employee_id, first_name,last_name,job_id FROM employees WHERE job_id LIKE %SA\_% ESCAPE \;
The artists name is retrieved whenever the artist is found in the GUESTAPPEARANCE table: SELECT NAME FROM ARTIST WHERE EXISTS (SELECT GUESTARTIST_ID FROM GUESTAPPEARANCE); SELECT A.NAME FROM ARTIST A WHERE EXISTS (SELECT GUESTARTIST_ID FROM GUESTAPPEARANCE WHERE GUESTARTIST_ID = A.ARTIST_ID); SELECT NAME FROM ARTIST WHERE ARTIST_ID BETWEEN 1 AND 10; SELECT NAME FROM ARTIST WHERE ARTIST_ID = ANY (SELECT GUESTARTIST_ID FROM GUESTAPPEARANCE); SELECT RECORDING_DATE, PLAYING_TIME, TITLE FROM SONG WHERE TITLE BETWEEN 'A' and 'C' ORDER BY PLAYING_TIME NULLS FIRST;
Pseudocolumns
ROWID, ROWNUM, <sequence>.CURRVAL, <sequence>.NEXTVAL, LEVEL
SQLPLUS
CMDS[EP] =command separator
SET CMDSEP ~ COL NAME HEADING "Artist" ~ COL CITY HEADING "Location"
COLSEP - Sets the character used between columns in a report. ECHO - Tells SQL*Plus to either list the command before executing it (ON) ESC[APE] - An escape character allows a command-level character to be used without executing its inherent command. FEED[BACK] - Determines whether to display feedback (ON), suppress feedback (OFF), HEAD[ING] - Set to OFF for no column headings. Set to ON (the default) to display column headings. LINE[SIZE] - The number of characters on one line before SQL*Plus starts a new line. The default in SQL*Plus is 80 LONG - Set the default number of bytes retrieved. MARK[UP] HTML- Vaguely, this option can be used to generate HTML script from SQL*Plus output. NEWP[AGE] Sets the number of blank lines to print before printing the title (if any) and headings on a new page. NULL - Sets the string displayed when a null value is returned in a report. The default is a blank space. NUMF[ORMAT] { format } Apply a format to all output numbers.For instance, SET NUMFORMAT '999,999,999,990.00' NUMW[IDTH] - Reset the default of 10 for display width of numbers. PAGES[IZE] Sets the number of lines per report page. This is most important when you are printing out a report. The LINESIZE and PAGESIZE must be set correctly to match the printable area. SET PAGES 0 to suppress formatting. PAU[SE] - SET PAUSE ON will wait for the user to press Enter before issuing a page break and displaying the next screen. RECSEP - Can be set to WRAPPED to output a record separator when a line wraps. Set to OFF to disable and EACH to include a record separator for every line. RECSEPCHAR - This option allows resetting of the record separator. SQLP[ROMPT] - This option changes the SQL prompt. TERM[OUT] - Turns on or off the screen display TIMI[NG] - Turns on or off the display of elapsed time. WRAP - Word or line wrapping implies that text overflowing SET FEEDBACK OFF such that the text rows selected. UNDEFINE CDNUM or DEFINE CDNUM=2 set variable in sql query. SAVE CDREPORT Use the SAVE command to write the SQL buffer contents to a file
EDIT CDREPORT
@@. This tells SQL*Plus to run the script and, in addition, to look for any scripts called within this script in the same directory.
FUNCTION CONCAT(HELLO,WORLD) or HELLO||WORLD SUBSTR(HELLOWORLD,1,5) LENGTH(HELLOWORLD) INSTR(HELLOWORLD,W) LPAD(SALARY,10,*) RPAD(SALARY,10,*) REPLACE(JACK and JUE,J,BL) TRIM(H from HELLOWORLD) LOWER(expression) UPPER(expression) INITCAP(expression) INSTR(expression, substring [, position [, occurrence]]) TRIM([[LEADING|TRAILING|BOTH] character FROM] expression),
SQL FUNCTION: 1. SINGLE ROW FUNCTION (Return one result per row) 2. MULTI-ROW FUNCTION (Return one result per set of row) NUMBER FUNCTION FUNCTION ROUND (49.926,2) TRUNC(49.926,2) MOD(1600, 300) ABS(n), CEIL(n) and FLOOR(n). POWER(m, n). SIGN(n). SQRT(n) TO_BINARY_DOUBLE(expression, format) and TO_BINARY_ FLOAT(expression, format) NANVL(value, replace). OUTPUT 45.93 (Round the value to specified decimal) 45.92 (Truncates the value to specified decimal) 100 (Return Reminder of the Division)
REMAINDER(n, m). For example: SELECT ROUND(45.923,2), ROUND(45.923,0), ROUND(45.923,-1) FROM DUAL; OUTPUT: 45.92 46 50 For example: SELECT TRUNC(45.923,2), TRUNC(45.923,0), TRUNC(45.923,-1) FROM DUAL; OUTPUT: 45.92 45 40
ARITHMETIC WITH DATE OPERATION Date+Number Date-Number Date-Date Date+Number/24 RESULT Date Date Number of Days Date DESCRIPTION Add a number of days to date Substract a number of days from date Substract one date from another Add a number of hours to date
DATE FUNCTION FUNCTION MONTHS _BETWEEN(date1,date2) OUTPUT NO. OF MONTHS BETWEEN TWO DATES
ADD MONTH TO DATE NEXT DAYS AS SPECIFIED BY US LAST DAY OF MONTH ROUND DATE TRUNC DATE
Format Characters Rounding and Truncating CC The first year in a century. YYYY, YEAR, YY The nearest year, rounds up on July 1st. Q The nearest quarter, rounds up on the 16th of month two. MONTH, MON, MM The nearest month, rounds up on the 16th. WW The same day of the week as the first day of the year. W The same day of the week as the first day of the month. DDD, DD The day. DAY, D The first day of the week. HH, HH12, HH24 The hour (HH24 is a 24-hour clock). MI The minute.
ASSUME SYSTEM DATE: 25-JUL-03 FUNCTION ROUND(SYSDATE,MONTH) ROUND(SYSDATE,YEAR) TRUNC(SYSDATE,MONTH) TRUNC(SYSDATE,YEAR) USING TO_CHAR function with DATE TO_CHAR(date,date format ) For example: TO_CHAR(hire_date,dd-mm-yyyy) USING TO_CHAR function with DATE TO_CHAR(salary,$99,999999.00) salary USING TO_DATE and TO_NUMBER function. RESULT 01-AUG-03 01-JAN-04 01-JUL0-03 01-JAN-04
COALESCE(E1,E2En, SUBSTITUTION)
eturns a null value when both expressions are equal The number of bytes in an expression
CASE: C=Condition R=Result CASE WHEN C1 THEN R1 WHEN C2 THEN R2 ... WHEN CN THEN RN ELSE RD END Example: SELECT last_name, job_id, salary, CASE job_id WHEN it_prog THAN 1.10*salary WHEN st_clark THAN 1.20*salary WHEN president THAN 4.10*salary Else salary END Revised Salary FROM employees; Note: DECODE is used only in oracle while CASE is ANSI CODE function can be used in both.
^ $ [ ] ( ) { i }, and { i, j
Start of line. End of line. List of elements allowing match of any expression contained within. Expression parentheses.
{ i, }, }
COLUMN TITLE FORMAT A32 SELECT TITLE, VAL FROM(SELECT TITLE ,REGEXP_INSTR(title, 'A(/*)') AS VAL FROM SONG ) WHERE VAL > 0;