Senthilkumar Gopal

thoughts, code, ramblings and notes

12 February 2009

I am studying for the Oracle Certified Professional certification and following are notes which are specific to the certification, from the prep book. These notes are also helpful for a refresher on SQL syntax and usability.

Chapter 1

  1. The client tier consists of two components: the users and the user processes. The server tier has three components: the server processes that execute the SQL, the instance, and the database itself.

  2. User Processes - SQL*Plus and SQL Developer

  3. Oracle Net - Proprietary client server software used by Oracle DB for communication

  4. Table ⇐⇒ relation or an entity.

  5. Rows ⇐⇒ records or tuples, and columns ⇐⇒ attributes or fields.

  6. No. of rows ⇐⇒ cardinality of the tuples.

  7. Hierarchical paradigm ⇒ Storing employees of each department seperately

    • Rows are delimited by comma

    • Data retrieval is faster, updation is difficult

  8. Relation paradigm is useful in OLTP, DSS

  9. Normalization - BOOKS Table - ISBN, Title, Author, Publisher&Address

    • The first normal form is to remove the repeating groups - Make one primary key per table

    • 1st normal form - BOOKS - ISBN, Title, Publisher&Address AUTHORS - Name, ISBN

    • The second normal form removes columns from the table that are not dependent on the primary key

    • 2nd Normal form - BOOKS - ISBN, Title, Publisher, AUTHORS - -do- , PUBLISHER - PUBLISHER,street, city, state

    • Third normal form removes all columns that are interdependent

    • 3rd Normal form - PUBLISHERS - PUBLISHER, Address Code ADDRESSES - Address Code, Street, City, State

  10. Every table should have a primary key defined.This is a requirement of the relational paradigm. Note that the Oracle database deviates from this standard: it is possible to define tables without a primary key

  11. Standards

    1. Primary key columns identified with a hash (#)

    2. Foreign key columns identified with a back slash (\)

    3. Mandatory columns (those that cannot be left empty) with an asterisk (*)

    4. Optional columns with a lowercase o

  12. "crow’s feet" to indicate which sides of the relationship are the many and the one.

  13. SQL is managed by ISO and ANSI.

    1. ISO - Organisation Internationale de Normalisation, based in Geneva

    2. ANSI - American National Standards Institute, based in Washington, DC.

  14. SQL Commands

    1. Data Manipulation Language (DML) commands: SELECT, INSERT, UPDATE, DELETE, MERGE

    2. Data Definition Language (DDL) commands: CREATE, ALTER, DROP, RENAME, TRUNCATE, COMMENT

    3. Data Control Language (DCL) commands: GRANT, REVOKE

    4. Transaction Control Language (TCL) commands: COMMIT, ROLLBACK, SAVEPOINT

  15. SQL Tools

    1. SQL*Plus is a user process written in C.

    2. It establishes a session against an instance and a database over the Oracle Net protocol.

    3. The platforms for the client and the server can be different /u01/app/oracle/product/db_1/bin/sqlplus - typical location

    4. Env Variables required are:

      1. The ORACLE_HOME - the set of files and directories containing the executable code and some of the configuration files.

      2. PATH must include ORACLE_HOME/bin

      3. LD_LIBRARY_PATH ORACLE_HOME/lib. [but in practice you may get away without setting this]

      4. database username followed by a forward slash character as a delimiter, then a password followed by an @ symbol as a delimiter, and finally an Oracle Net connect identifier.

      5. Ex: sqlplus system/oracle@orc1

      6. executable file sqlplus.exe, and the graphical version was sqlplusw.exe

      7. Windows: D:\oracle\app\product\11.1.0\db_2\BIN\sqlplus.exe

      8. Logon String: system/oracle@orcl

      9. sqlplus /nolog - to prevent it from immediately presenting a login prompt HKEY_LOCAL_MACHINE/SOFTWARE/ORACLE/KEY_OraDb11g_home1 - Registry Key for Env Variables

      10. sqlplus scott/tiger@orcl - resolves the server orc1 using tnsnames.ora or using LDAP [TNS - Transparent Network Substrate ]

      11. sqlplus scott/tiger@linsrv1.bplc.co.za:1521/orcl.bplc.com - Complete details of server IP and port and the database service to connect to.

      12. SQL Developer - JDK1.5 is the prerequisite

  16. Definitions

    1. A database user is a person who can log on to the database.

    2. A database schema is all the objects in the database owned by one user.

    3. CREATE SCHEMA command does not create a schema, it is used for creating objects in a schema.

    4. A schema is initially created empty, when a user is created with the CREATE USER command. 17) Notes

    5. SQL, PL/SQL, and Java can all run in the database

    6. Third normal form is the usual form aimed for by systems analysts when they normalize data into relational structures.

    7. SQL Developer needs a graphics terminal to display windows and JRE

    8. A schema and a user are inseparable.

Chapter 2

  1. DESC[RIBE] <SCHEMA>.tablename

  2. access to a special table called DUAL, which belongs to the SYS schema

  3. NUMBER(p,s) → precision and scale → max number of digits given in precision

  4. CHAR data type utilizes storage inefficiently, padding any unused components with spaces.

  5. TIMESTAMP data type - introduction in Oracle 9i

  6. Three concepts from relational theory encompass the capability of the SELECT statement: projection, selection, and joining

    1. Projection refers to the restriction of attributes (columns) selected from a relation or table

    2. Selection refers to the restriction of the tuples or rows selected from a relation (table)

    3. Joining, as a relational concept, refers to the interaction of tables with each other in a query

  7. SELECT *|{[DISTINCT] column|expression [alias],…​} FROM table;

  8. DISTINCT performs a distinct for the combination of columns. Ex: select distinct job_id, department_id from job_history;

  9. SQL*Plus always requires a statement terminator, and usually a semicolon is used.

  10. Individual statements in SQL scripts are commonly terminated by a line break (or carriage return) and a forward slash on the next line, instead of a semicolon.

  11. SELECT TABLE_NAME from USER_TABLES

  12. Hierarchy

	( ) - Brackets or parentheses
	/ - Division
	* - Multiplication
	- - Subtraction
	+ - Addition
Note
Operations with the same level of precedence are evaluated from left to right. If more than one operator with the same level of precedence is present in an expression, then these will be evaluated from left to right.

Chapter 2 Continued

  1. select col1 [AS] <alias> → For column aliasing we can use [as] or just a space - >using AS → good SQL coding habit

  2. Most Common Errors

    1. ORA-00923: FROM keyword not found where expected

    2. ORA-00942: table or view does not exist

  3. Case preservation of an alias is only possible if the alias is double quoted and double quotes are needed if the alias is more than one word

  4. "ORA-00923: FROM keyword not found where expected" - multi word alias is not double quoted

  5. || represent the character concatenation operator

  6. select 'literal'||'processing using the REGIONS table' from regions;

  7. 'Plural’s with the literal 'Plural''s

  8. double quotes cannot be used

  9. alternative quote (q) operator → wrapping symbols → (round brackets), {curly braces}, [square brackets], or <angle brackets>

  10. Using the q operator, the character delimiter can effectively be changed from a single quotation mark to any other character

  11. format→ select q’X → q' is the notation and X is the delimiter character Ex: select q’X’Test this String for Plural’s X' "qX" from dual; 'Test this String for Plural’s X' → is the test string

  12. If we use one of the wrapping symbols, then q'<'sdfgdfgd>' can be given where '>' is taken as the delimiter

  13. Any arithmetic calculation with a NULL value always returns NULL. even division by a null value results in null, unlike division by zero, which results in an error

  14. The character concatenation operators ignore null, whilst the arithmetic operations involving null values always result in null

  15. All arithmetic operations with null will give null as the answer, while concatenation will just ignore the null value and give the rest as the answer

CHAPTER 3

  1. SELECT *|{[DISTINCT] column|expression [alias],…​} FROM table [WHERE condition(s)];

  2. where salary = 10000; where salary = '10000';Both formats are acceptable to Oracle since an implicit data type conversion is performed when necessary.

  3. String concatenations and arithmetic operations can also be used in the WHERE clause

  4. The literals are automatically converted into DATE values based on the default date format, which is DD-MON-RR. [RR means 50-99 will be 1950 to 1999 and 0-50 will be 2000 and 2050]

  5. That DATE values are only equal to each other if there is an exact match between all their components including day, month, year, hours, minutes, and seconds.

  6. The entire four-digit year component (YYYY) can been specified

  7. START_DATE + 30 returns a DATE 30 days later than the start_date

  8. END_DATE - START_DATE gives a NUMBEr

  9. Not Equal - != (or) <>

  10. Testing character inequality : the strings being compared on either side of the inequality operator are converted to a numeric representation of its character [same for < or >]

  11. The Oracle server stores dates in an internal numeric format, and these values are compared within the conditions.

  12. BETWEEN ⇐⇒ >= and ⇐

  13. IN operator - equivalent to multiple ORs

  14. wildcards - % [0 or more characters] and _ [1 character]

  15. like '%' - all rows with the values NOT NULL

  16. % and _ can be escaped using '\' [backslash] and denoted as like 'a\%' ESACPE '\'

  17. we can changed the Escape character as well

  18. For Null checks always use IS NULL

  19. FOR AND operator - If the row contains a NULL value that causes one of the conditions to evaluate to NULL, then that row is excluded

  20. SELECT * FROM EMPLOYEES WHERE SALARY LIKE '%80%'; - Oracle temporarily changes the NUMBER to CHAR data for the comparison

  21. Usage of NOT: where NOT (last_name='King'), where first_name NOT LIKE 'R%',where department_id NOT IN (10,20,30),where salary NOT BETWEEN 1 and 3000,where commission_pct IS NOT NULL

  22. WHERE A and B or C or D and E, then a row will be returned if both conditions A and B are fulfilled, or only condition C is met, or only condition D is met, or both conditions D and E are fulfilled. Conditions seperated by AND needs both to be satisfied while OR needs only one of them to be satisfied

  23. Precedence

	NOT] LIKE, IS [NOT] NULL, [NOT] IN
	[NOT] BETWEEN
	!=,<>
	NOT
	AND
	OR
  1. SELECT *|{[DISTINCT] column|expression [alias],…​} FROM table [WHERE condition(s)] [ORDER BY {col(s)|expr|numeric_pos} [ASC|DESC] [NULLS FIRST|LAST]];

  2. The default sort order is assumed to be NULLS LAST for ascending sorts and NULLS FIRST for descending sorts.

  3. If no ORDER BY clause is specified, the same query executed at different times may return the same set of results in different row order, so no assumptions should be made regarding the default row order.

  4. Positional sorting applies only to columns in the SELECT list that have a numeric position associated with them

  5. Composite Sorting: order by job_id desc, last_name, 3 desc;

  6. The ampersand character (&) is the symbol chosen to designate a substitution variable in a statement and precedes the variable name with no spaces between them

  7. When the statement is executed, the Oracle server processes the statement, notices a substitution variable, and attempts to resolve this variable’s value in one of two ways.

    1. First, it checks whether the variable is defined in the user session. (The DEFINE command).

    2. If the variable is not defined, the user process prompts for a value that will be substituted in place of the variable.

    3. Once a value is submitted, the statement is complete and is executed by the Oracle server.

    4. The ampersand substitution variable is resolved at execution time and is sometimes known as runtime binding or runtime substitution.

  8. SUBSTITUTION: any alphanumeric name and invalid data type gives ORA-00904: invalid identifier error is returned.

  9. Date and Character literals need to be enclosed within quotes. Best Practice is to define the substitution with quotes so that the date and character will be quoted without the user need

  10. When the Oracle server encounters a double ampersand substitution variable, a session value is defined for that variable and you are not prompted to enter a value to be substituted for this variable in subsequent references.

  11. To undefine the SEARCH variable, you need to use the UNDEFINE command

  12. any element of a SQL statement is a candidate for substitution

  13. column name references do not require single quotes both when explicitly specified and when substituted via ampersand substitution

  14. DEFINE command can be used to retrieve a list of all the variables currently defined in your SQL session

  15. It can also be used to explicitly define a value for a variable referenced as a substitution variable by one or more statements during the lifetime of that session.

  16. SET DEFINE OFF → Makes & as an ordinary character

  17. The VERIFY command controls whether the substitution variable submitted is displayed onscreen so you can verify that the correct substitution has occurred

  18. SET VERIFY ON|OFF

  19. VERIFY is switched ON, the query is executed, and you are prompted to input a value. Once the value is input and before the statement commences execution, Oracle displays the clause containing the reference to the substitution variable as the old clause with its line number and, immediately below this, the new clause displays the statement containing the substituted value.

  20. NULLS LAST can be applied in the ORDER BY clause for every column - ORDER BY 3 DESC NULLS LAST, 2 ASC;

CHAPTER 4

  1. case conversion - LOWER, UPPER, and INITCAP

  2. character manipulation - LENGTH, CONCAT, SUBSTR, INSTR, LPAD, RPAD, TRIM, and REPLACE

  3. CONCAT takes only two strings are parameters

  4. SUBSTR(string, start position, number of characters) - 1 indexed

    1. if the position is not found → gives no result

    2. if the length is larger, returns only the available characters

  5. INSTR → similar to indexOf → INSTR(source string, search item, [start position],[nth occurrence of search item]) → returns 0 if not found

  6. LPAD(string, length after padding, padding string) and RPAD(string, length after padding, padding string)

  7. TRIM - > by default trims spaces TRIM('' from '#PASS#WORD#') → 'PASS#WORD'

  8. REPLACE(string, search item, replacement item) - All the instances

  9. ROUND, TRUNC, MOD - Numeric functions

  10. ROUND(number, decimal precision) - round(42.39,1) = 42.4 → >=5 will be rounded to its ceiling

  11. TRUNC → just drops the additional numerals - trunc(42.39,1) = 42.3

  12. MOD(dividend, divisor) → mod(42,10) = 2

  13. MONTHS_BETWEEN, ADD_MONTHS, LAST_DAY, NEXT_DAY, SYSDATE, ROUND, and TRUNC → date functions

  14. MONTHS_BETWEEN(greater_date, smaller_date) → MONTHS_BETWEEN('2-JAN-2008','01-JAN-2008') → .0322 → stored as a decimal value. Need to TRUNC to get only the MONTHS value

  15. LAST_DAY(date 1) function returns the last day of the month that the specified date falls into,

  16. NEXT_DAY(date 1, day of the week) returns the date on which the next specified day of the week falls after the given date (if day of the week is not valid, throws error)

  17. SYSDATE function takes no parameters and returns a date value that represents the current server date and time

  18. ROUND(date, date precision format) and TRUNC(date, date precision format) round and truncate a given date value to the nearest date precision format like day, month, or year:

sysdate = 17-DEC-2007
round(sysdate,'month') = 01-JAN-2008
trunc(sysdate,'month') = 01-DEC-2007
[Works only in 11g]
  1. Oracle’s implementation of SQL is compliant with the ANSI:1999 (American National Standards Institute) standard for SQL.

    1. More recently, it claimed partial compliance to the SQL:2003 standard endorsed by both ISO (International Organization for Standardization) and ANSI.

    2. SQL functions have been standardized, and Oracle has documented those that are fully or partially compliant to the SQL:2003 standard.

  2. Character Case Conversion Functions - If parameters are numeric or date value, it is implicitly converted into a string.

  3. select lower('The SUM '||'100+100'||' = '||(100+100)) from dual → the sum 100+100 = 200. Calculations are done if inside bracket

  4. (SYSDATE+2) → Adds two days

  5. select initcap('init cap or init_cap or init%cap') from dual → Init Cap Or Init_Cap Or Init%Cap

    1. space, _,%,!, $ are all used as word seperators. Punctuation or special characters are regarded as valid word separators.

  6. concat(1+2.14,' approximates pi') → 3.14 approximates pi

  7. LPAD(s, n, p) and RPAD(s, n, p),

    1. if the parameter n is smaller than or equal to the length of the source string s, then no padding occurs and only the first n characters of s are returned

  8. TRIM([trailing|leading|both] trimstring from s).

    1. TRIM(trailing trimstring from s) removes all occurrences of trimstring from the end of the string s if it is present

    2. TRIM(leading trimstring from s) removes all occurrences of trimstring from the beginning of the string s if it is present.

    3. TRIM(both trimstring from s) removes all occurrences of trimstring from the beginning and end of the string s if it is present

    4. [both appears to be optional]

  9. INSTR(source string, search string, [search start position], [nth occurrence])

    1. A negative number for the start position gives the occurence from the End instead of the beginning

    2. However, if the Occurence is ignored, then it just does a absolute of the position

  10. SUBSTR(source string, start position, [number of characters to extract]) → negative number in start position begins the search from that position from the End of the String

  11. REPLACE(source string, search item, [replacement term]) If the replacement term parameter is omitted, each occurrence of the search item is removed from the source string

  12. If the specified decimal precision is n, the digit significant to the rounding is found (n + 1) places to the RIGHT of the decimal point.

    1. If it is negative, the digit significant to the rounding is found n places to the LEFT of the decimal point.

    2. If the numeric value of the significant digit is greater than or equal to 5, a "round up" occurs, else a "round down" occurs.

    3. round(1301.916718,-3) → 1000

    4. round(1601.916718,-3) → 2000

    5. round(1601.916718) → 1602

  13. A numeric truncation is different from rounding because the resulting value drops the numbers at the decimal precision specified and does not attempt to round up or down if the decimal precision is positive.

    1. However, if the decimal precision specified (n) is negative, the input value is zeroed down from the nth decimal position.

    2. trunc(1301.916718,-3) → 1000

    3. trunc(1601.916718,-3) → 1000

    4. trunc(1601.916718) → 1601

  14. The MOD function returns the numeric remainder of a division operation

    1. If the divisor is zero, no division by zero error is returned and the MOD function returns a zero instead

    2. If the divisor is larger than the dividend, then the MOD function returns the dividend as its result

    3. MOD(5.2,3) → 2.2

    4. MOD(7,0) → 0 [result is 7 in 10g]

    5. MOD(0,7) → 0

    6. MOD(7,35) → 7

  15. The default format of the results comprises two digits that represent the day, a three-letter abbreviation of the month, and two digits representing the year component.

    1. By default, these components are separated with hyphens in SQL*Plus and forward slashes in SQL Developer 34.. Date Format Mask

	 DD 	Day of the month
	 MON 	Month of the year
	 YY 	Two-digit year
	 YYYY	Four-digit year including century
	 RR	Two-digit year (Year 2000–compliant)
	 CC 	Two-digit century
	 HH	Hours with AM and PM
	 HH24 	Twenty-four-hour time
	 MI 	Minutes
	 SS	Seconds
  1. The SYSDATE function returns the DD-MON-RR components of the current system date

  2. Date1 - Date2 = Num1; Date1 - Num1 = Date2; Date1 = Date2 + Num1;

  3. use 6/24 to add hours

  4. MONTHS_BETWEEN(start date, end date)

  5. ADD_MONTHS (start date, number of months)

    1. The number of months may be negative, resulting in a target date earlier than the start date being returned.

    2. The number of months may be fractional, but the fractional component is ignored and the integer component is used

  6. NEXT_DAY (start date, day of the week)

    1. The acceptable values are determined by the NLS_DATE_LANGUAGE database parameter but the default values are at least the first three characters of the day name or integer values, where 1 represents Sunday, 2 represents Monday, and so on.

    2. NEXT_DAY('02-JAN-2009','WEDNE') → Works using the First three chars 'WED'

  7. LAST_DAY(start date)

  8. ROUND(source date, [date precision format]) → [No implicit conversion for DATE]

    1. The date precision format parameter specifies the degree of rounding and is optional. If it is absent, the default degree of rounding is day.

    2. The date precision formats include century (CC), year (YYYY or YEAR), quarter (Q), month (MM or MONTH), week (W), day (DD), hour (HH), and minute (MI)

  9. Rounding up to century is equivalent to adding one to the current century.

    1. Rounding up to the next month occurs if the day component is greater than 15(> 15) , else rounding down to the beginning of the current month occurs.

    2. If the month falls between one and six, then rounding to year returns the date at the beginning of the current year, else it returns the date at the beginning of the following year

  10. TRUNC(source date, [date precision format])

    1. The date precision format parameter specifies the degree of truncation and is optional. .. If it is absent, the default degree of truncation is day

    2. Any time component of the source date is set to midnight or 00:00:00

    3. TRUNC is similar to ROUND, except it is always the FLOOR and never the CEILING

    4. TRUNC(TO_DATE('31-JAN-2009'),'MM') → 01-JAN-2009

  11. Functions dont need parameters

Chapter 5

  1. length(1234) → implicit conversion for numbers and dates to char → 4 is the result

  2. mod('11',2) → implicit conversion -

    1. mod('$11',2) → ORA-1722: invalid number

  3. Implicit date conversion should not have Time parameters [Check this]. Implicit conversion for dates can occur if the pattern follows [D|DD] separator1 [MON|MONTH] separator2 [R|RR|YY|YYYY] separator1 and separator2 elements may be most punctuation marks, spaces, and tabs

add_months('1\january/8',1) -> 01/FEB/08
months_between('13*jan*8', '13/mar/2008') -> -2
add_months('01$jan/08',1) -> 01/FEB/08
add_months('13!jana08',1) -> ORA-1841: (full) year must be between –4713 and +9999 and not be 0
jana is not a valid month -> only 3 characters or full month is allowed
add_months('24-JAN-09 18:45',1) -> ORA-1830: date format picture ends before converting entire input string
  1. Optional national language support parameters (nls_parameters) are useful for specifying the language and format in which the names of date and numeric elements are returned

  2. Publicly available view called NLS_SESSION_PARAMETERS that contains the NLS parameters for your current session. The default NLS_CURRENCY value is the dollar symbol, but this can be changed at the user session level ALTER SESSION set NLS_CURRENCY='GBP';

  3. TO_CHAR(number1, [format], [nls_parameter])

Formats
   9  Numeric width
   0  Displays leading zeros 		Ex: 09999 0012 00012
   .  Position of decimal point
   D  Decimal separator position (period is default)
   ,  Position of comma symbol
   G  Group separator position (comma is default)
   $  Dollar sign
   L  Local currency
  MI  Position of minus sign for negatives
  PR  Wrap negatives in parentheses
EEEE  Scientific notation [Must be only 4 E's and always gives in 1.xxxxxE+10 format]
   U  nls_dual_currency [if nls_dual_currency value is set]
   V  Multiplies by 10n times (n is the number of nines after V) Ex: 9999V99 3040 304000
   S + or - sign is prefixed
  1. When a format mask is smaller than the number being converted, a string of hash symbols is returned instead.

  2. When a format mask contains fewer fractional components than the number, it is first rounded to the number of decimal places in the format mask before being converted.

    1. TO_CHAR(date1, [format], [nls_parameter]) → Default is DD/MON/RR

      'Month' -> January
      'MOnth' -> JANUARY
      'month' -> january
      'Month' -> padded with spaces
      'fmMonth' -> Not padded with spaces
      Y-YYYY -> Year digits
      RR -> 2 digit year
      YEAR -> Case Sensitive Full Year spelling
      MM, MON, MONTH -> 2 digit, Three char, full month -> case sensitive
      D, DD, DDD -> day of week, month, year
      DY -> 3 letter abbreviation
      DAY -> Case Sensitive day
      only DAY, MONTH, YEAR are case sensitive AND PADDED NOT the shorter forms
      W, WW -> week of month, year
      Q -> Quarter
      CC -> Century
      S before CC, YYYY, YEAR -> sign (-) for BC
      I-IYYY -> ISO year dates for Y-YYYY
      BC, AD, B.C. and A.D. -> to display BC or AD
      J -> Julian Day -> days since 31 December 4713 BC
      IW -> ISO standard week (1 to 53)
      RM -> Roman numeral month
      AM, PM, A.M. and P.M.-> Meridian Indicators
      HH, HH12 and HH24 -> Hour of day, 1-12 hours, and 0-23 hours
      MI, SS, SSSSS -> Minutes. Seconds, Seconds past midnight
  - / . , ? # ! -> Punctuation marks for seperators
  "any literal" -> simply displays the character literal
  TH -> positional or ordinal text -> 12th
  SP -> spelled out number
  THSP or SPTH -> Spelled out and Ordinal number -> twelfth
  1. TO_DATE(string1, [format], [nls_parameter])

to_date('25-DEC') -> ORA-01840: input value not long enough for date format
to_date('25-DEC', 'DD-MON') -> 25-DEC-2009
to_date('25-DEC-10', 'fxDD-MON-YYYY') -> ORA-01862: the numeric value does not match the length of the format item
  fx -> Makes strict checking
to_date('25-DEC-10', 'DD-MON-YYYY') -> 25-DEC-10 -> takes as 0010

Formats similar to Point 7 used for TO_CHAR
  1. TO_NUMBER(string1, [format], [nls_parameter])

to_number('$1,000.55')  -> ORA-1722: invalid number.
to_number('$1,000.55','$999,999.99') -> 1000.55

If you convert a number using a shorter format mask, an error is returned -> RA-1722: invalid number.

TO_NUMBER(123.56,'999.9') returns an error, while TO_CHAR(123.56,'999.9') returns 123.6.
  1. NVL(original, ifnull) → both columns are mandatory : ORA-00909: invalid number of arguments.

    1. nvl(substr('abc',4),'No substring exists')

    2. since there is no 4th character, it returns null and hence the 'No substring exists' is returned

  2. NVL2(original, ifnotnull, ifnull)

    1. The data types of the ifnotnull and ifnull parameters must be compatible, and they cannot be of type LONG.

    2. They must either be of the same type, or it must be possible to convert ifnull to the type of the ifnotnull parameter. → ORA-01722: invalid number

    3. The data type returned by the NVL2 function is the same as that of the ifnotnull parameter

  3. NULLIF(ifunequal, comparison_term) → returns NULL if both terms are equal else the first term

    1. NO IMPLICIT conmversion → nullif('24-JUL-2009','24-JUL-09') returns the first term as these are not equal

  4. COALESCE(expr1, expr2,…​,exprn), where expr1 is returned if it is not null, else expr2 if it is not null, and so on

    1. COALESCE(expr1,expr2) = NVL(expr1,expr2)

    2. COALESCE(expr1,expr2,expr3) = NVL(expr1,NVL(expr2,expr3))

    3. The data type COALESCE returns if a not null value is found is the same as that of the first not null parameter.

    4. To avoid an "ORA-00932: inconsistent data types" error, all not null parameters must have data types compatible with the first not null parameter

  5. The DECODE function is specific to Oracle, while the CASE expression is ANSI SQL compliant

  6. DECODE(expr1,comp1, iftrue1, [comp2,iftrue2…​[ compN,iftrueN]], [iffalse])

  7. CASE search_expr

CASE search_expr
      WHEN comparison_expr1 THEN iftrue1
      [WHEN comparison_expr2 THEN iftrue2
      ..
      WHEN comparison_exprN THEN iftrueN
      ELSE iffalse]
    END

    CASE
      WHEN condition1 THEN iftrue1
      [WHEN condition2 THEN iftrue2
      ...
      WHEN conditionN THEN iftrueN
      ELSE iffalse]
    END

Chapter 6

  1. COUNT({*|[DISTINCT|ALL] expr}) ;

    1. The ALL keyword is part of the default syntax, so COUNT(ALL expr) and COUNT(expr) are equivalent

    2. These count the number of nonnull occurrences of expr in each group

    3. Data Type allowed: NUMBER, DATE, CHAR, or VARCHAR2

  2. AVG([DISTINCT|ALL] expr) → AVG(ALL expr) and AVG(expr) add the nonnull values of expr for each row and divide the sum by the number of nonnull rows in the group.

    1. Data Type allowed: NUMBER

  3. SUM([DISTINCT|ALL] expr) → Data Type allowed: NUMBER

  4. MAX([DISTINCT|ALL] expr); MIN([DISTINCT|ALL] expr)

    1. Data Type allowed: NUMBER, DATE, CHAR, or VARCHAR2

  5. VARIANCE([DISTINCT|ALL] expr); STDDEV([DISTINCT|ALL] expr);

    1. Statistical variance refers to the variability of scores in a sample or set of data.

    2. VARIANCE(DISTINCT expr) returns the variability of unique nonnull data in a group.

    3. STDDEV calculates statistical standard deviation, which is the degree of deviation from the mean value in a group. It is derived by finding the square root of the variance.

    4. Data Type allowed: NUMBER

  6. Group functions may only be nested two levels deep → ORA-00935: group function is nested too deeply.

  7. Select Statement

SELECT column|expression|group_function(column|expression [alias]),...}
   FROM table
   [WHERE condition(s)]
   [GROUP BY {col(s)|expr}]
   [ORDER BY {col(s)|expr|numeric_pos} [ASC|DESC] [NULLS FIRST|LAST]];
  1. If an item, which is not a group function, appears in the SELECT list and there is no GROUP BY clause, → ORA-00937: not a single-group group function

    1. If a GROUP BY clause is present but that item is not a grouping attribute, → ORA-00979: not a GROUP BY expression

    2. If a group function is placed in a WHERE clause → ORA-00934: group function is not allowed here

  2. Select Statement

SELECT column|expression|group_function(column|expression [alias]),…}
   FROM table
   [WHERE condition(s)]
   [GROUP BY {col(s)|expr}]
   [HAVING group_condition(s)]
   [ORDER BY {col(s)|expr|numeric_pos} [ASC|DESC] [NULLS FIRST|LAST]];
  1. The HAVING clause can occur before the GROUP BY clause in the SELECT statement.

    1. However, it is more common to place the HAVING clause after the GROUP BY clause.

    2. All grouping is performed and group functions are executed prior to evaluating the HAVING clause

  2. NVL in Select clause only useful for display and NVL in WHERE or HAVING is useful for modifying the values being verified

    1. NVL(x,0) → 0

    2. LENGTH(NVL(x,0)) → 1

Continued in Part 2