Cheatsheet for Exam 1Z0-051 Oracle Database 11g: SQL Fundamentals
- Data must be enclosed in single quotes and is case sensitive
- Columns name, table names are not case sensitive unless enclosed in double quotes
- Null means unknown and is not equal to zeros or blanks
- Null values are compared using IS NULL operator or IS NOT NULL operator
- DISTINCT must be the 1st clause after SELECT (SELECT DISTINCT)
- The ORDER BY clause must be the last clause
- By default Nulls are sorted last
- You can change the sort order of nulls by specifying NULLS FIRS T or NULLS LAST
- The default sort order is in ascending sequence
- Sorts are case sensitive in character sort.
- RIGHT OUTER JOIN returns both matching and non-matching data from the right table in the join.
- LEFT OUTER JOIN returns both matching and non-matching data from the left table in the join.
- FULL OUTER JOIN returns both matching and non-matching data from both tables in join.
- The ON clause for joins can be used for joining table columns with different name but compatible data types
- Additional join conditions can be specified with WHERE clause when tables are joined with ON or USING clause or Natural joins
- NATURAL JOINS cannot have qualifier (table alias or table name). Example: SELECT table1.col1, col2, col3 is wrong.
- Number of data values in the VALUES clause of INSERT command should match the number of columns. Example INSERT INTO table1(col1, col2) values (col1,col2);
- VALUES clause cannot be specified for INSERT command if insert is based on output of SELECT. Example: INSERT INTO table1 SELECT * FROM table2
- You cannot mix single row results and group functions in SELECT statement unless single rows are grouped with group function
- WHERE Clause is use to eliminate rows; HAVING clause is used eliminate groups. Both WHERE and HAVING can be part of same select.
- Transaction ends with COMMIT, ROLLBACK or DDL or DCL or normal exit
- INTERSECT : Returns common values from 2 or more s SELECTS
- UNION: Returns all Distinct values from 2 or more SELECTS
- UNION ALL: Returns all values including duplicates from 2 or more SELECTS
- MINUS: Returns rows from 1st SELECT that are not in 2nd SELECT
- The number of columns and data types must be identical for INTERSECT, UNION , UNION ALL and MINUS
- By default, All set operators expect for UNION ALL (for INTERSECT, UNION and MINUS) return data in sorted order of 1st column of the 1st SELECT statement.
- Oracle internally stores date in numeric format.
- Date1-Date2 returns numeric value in days
- Date1+ n returns date (where n is numeric value)
- Only one LONG or LONG RAW data type supported per table
- LONG or LONG RAW cannot be used in ORDER BY clause.
- Starting value of sequence cannot be altered. The only option is to drop and recreate the sequence if you want to change the starting value of sequence.
- All savepoints defined are lost when transaction ends. Using rollback to savepoint after transaction results in error.
- <ANY is less than maximum ; >ANY is more than minimum; =ANY is same as IN
- <ALL is less than minimum; >ALL is more than maximum;
- COALESCE returns 1st non-null value in expression and take multiple values as argument. All arguments must be of same data type
- Indexes are automatically created on unique and primary keys only
- Primary key cannot contain null values whereas unique key can contain null values
- A table can have only one primary key.
- A table can have more than one unique key
- Any constraint can be added to table with existing data provided the existing data satisfies the constraint to be added. Example: Primary key can be added to table with existing data if there are no duplicates and NULL values.
- Group function COUNT(*) returns the count without ignoring duplicates and null values.
- Group function COUNT(DISTINCT col1) returns the count ignoring duplicates and null values.
- DELETE statement can be rolled back whereas TRUNCATE cannot be rolled back. TRUNCATE is DDL command and therefore implicitly committed
- DELETE statement deletes all the rows in the table ONLY if the WHERE condition is NOT specified. Otherwise rows are deleted based on WHERE clause.
- TRUNCATE always deletes all the rows in the table keeping the structure of the table intact.
- Multicolumn or expressions can be compared between main query and subquery. Example: SELECT col1, col2, col3, col4 FROM tab1 WHERE (col3,col4) IN (SELECT col3, col4 FROM tab2)
- Sub queries can contain GROUP BY clause , GROUP functions and ORDER BY clause
- SELECT NVL2(NULLIF(sal, comm), 0, 10000) as TOTAL_SALARY from emp will return total salary of 10000 if sal =comm or if sal is null.
- INTERVAL YEAR TO MONTH stores period of time in years and months. Example : 5-1 is 5 years and 1 month
- INTERVAL DAY TO SECONDS stores period of time in days, hours, minutes and seconds. Example: 6:02:30:00 is 6 days 2 hours 30 minutes and 00 seconds
- Data stored in BFILE data type is external (OS file) to database whereas data stored in BLOB is internal or stored within the database.
- Null values can be retrieved with IS NULL or IS NOT NULL operator. You cannot use =NULL in WHERE clause. Example: SELECT * FROM tab1 WHERE col1=NULL is wrong.
- Col1=NULL can be used as target. Example: update employees set commission_pct=NULL
- NULL values are ignored when check is made for duplicates.
- Arithmetic expressions containing NULL value evaluate to NULL. This can be mitigated with appropriate functions like NVL, NVL2 etc.
- CHECK constraint cannot refer to SYSDATE, UID, USERENV & USER functions
- CHECK constraint cannot refer to CURRVAL, NEXTVAL, LEVEL &ROWNUM pseudo columns
- You cannot delete a row from view if it contains DISTINCT clause, GROUP BY clause, GROUP functions and ROWNUM.
- View cannot be modified if it is defined with DISTINCT clause, GROUP BY clause, GROUP functions , ROWNUM & columns defined by expressions
- Data cannot be inserted into view if it is defined with DISTINCT clause, GROUP BY clause, GROUP functions , ROWNUM , columns defined by expressions & NOT NULL columns are not part of view
- CREATE TABLE table1 AS SELECT * from table2 will create table1 which will have same table structure and data as table2. None of constraints other than NOT NULL are copied to new table1. Data will be copied to be new table based on condition specified in WHERE clause.
- Public and private synonyms can have same name for same table.
- Sub queries can be used in WHERE clause , HAVING clause, FROM clause, SELECT list,
- When a table is dropped, all data, indexes, constraints on the table are also dropped. Views and synonyms dependent on the table are not dropped, instead they become invalid.
- When a table is dropped with pending transaction, all data is committed before dropping the table.
- Sort is done with ORDER BY clause and must be the last clause of given SELECT statement.
- For SELECTs with Sub queries, each subquery can have its own ORDER BY clause.
- Only one ORDER BY clause is supported for SELECT statements with UNION, UNION ALL MINUS &INTERSECT clause.
- ORDER BY supports ordering of column by column name, column position, column expression & coumn alias. When used with set operators (UNION, UNION ALL MINUS &INTERSECT), Oracle only recognizes column name or column alias or column expression of the 1st SELECT statement. Sorting using column names from 2nd or later SELECTS will return in error.
- NVL2 evaluates expression1. Expression2 is returned if expression1 is NOT NULL. Expression3 is returned expression1 is NULL.SYNTAX: NVL2(expression1 ,expression2, expression3)
- NULLIF compares expression1 and expression2. NULL is returned if expression1 and expression2 are equal. Expression1 is returned if expression1 and expression 2 are not equal. SYNTAX: NULLIF(expression1, expression2)
- COALESCE returns 1st NOT NULL expression in the list.SYNTAX: COALESCE (expression1, expression2, expression3, …..)
- All group functions expect COUNT(*) ignore NULL values. Examples of group functions are AVG, MAX , MIN, STDDEV, SUM, VARIANCE .COUNT(expr) ignores NULL values whereas COUNT(*) does not. Use NVL to force group functions to include NULL values. Example: SUM(NVL(sal,0))
- By default all group functions does not ignore duplicate values. You can ignore duplicate values with DISTINCT clause. Example: AVG(SALARY) vs. AVG(DISTINCT sal)
- With Natural Join , tables are joined by matching values of one or more columns with same column names. An error is returned if columns names are same but data types are different
- Qualifiers or table alias cannot be used with Natural Joins. Example: table1.col1 is not valid
- Additional restrictions for natural joins are implemented with WHERE clause.
- USING clause cannot be used with Natural Join
- Qualifiers or table alias cannot be used in the SELECT list with USING clause for joins. Example: SELECT table1.col1 is not valid. This restriction is also applicable for condition used in USING clause. Example: USING (table1.col1) is incorrect
- ON clause for joins is used to specify join condition. Additional condition can be specified as part of ON clause or with WHERE clause.
- RR date format calculate the century format of the date
- Single row functions return a single result for every row in the table that satisfies the given condition.
- AVG(date1) will fail where as AVG(date1-date2) will succeed because 2nd date expression returns numeric value unlike 1st date expression.
- DBA permissions are required to create PUBLIC synonym.
- NULL values can be inserted into table by omitting the column from column list or specify NULL for column value in VALUES clause.
- NOT NULL constraints must be defined at column level. All other constraints can be defined at table level or column level.
- A foreign key must match existing value in parent table or can be NULL
- ON DELETE CASCADE deletes dependent child data when parent is deleted.
- ON DELETE SET NULL sets dependent child data to N ULL when parent is deleted.
- Views create with “WITH CHECK OPTION” specifies that INSERTS or UPDATES performed via the view cannot create rows that view cannot select.DELETES via view are not impacted as DELETES do not add rows.
- A sequence can be shared by multiple tables
- Sequence should not be created with CYCLE option if they are used for primary key values
- Sequence number is lost if the transaction is rolled back.
- All sequences stored in the cache are lost if the database /system crashes.
- A public and private synonym with same name can exist for the same table
- Use format specifier spth to display date as spelled-out ordinal numbers.
- A parent table having children dependent on it cannot be truncated w/o disabling referential integrity constraints.In other words , TRUNCATE command will fail if referential integrity constraints are not disabled
- A transaction is logical collection of DML statements. If one the DML statements fails, only that statement is rolled back and not the entire transaction.
- Oracle DB issues implicit commit before and after any DDL even if the DDL statement fails.
- “FOR UPDATE” clause is used to hold lock on rows returned by SELECT statement for future update. This lock is released only when the transaction is committed or rolled back.
- No constraints can be defined on LONG or LONG RAW column
- A Cartesian join is formed because of missing join condition or invalid join condition.
- Minimum of n-1 join valid join conditions are required to avoid Cartesian join where n is number of tables to be joined.
- When <, > or = operator is used for comparing the results of subquery, the query will succeed as long as the subquery returns one and only row.Generally subquery with group functions returns single row but this is not always the case
- Example: SELECT MAX(sal) FROM EMP returns one row but SELECT MAX(sal) FROM EMP GROUP BY deptno returns multiple rows.
- If multiple rows are returned by subquery, then use ANY, ALL, SOME or IN operator
Recent Comments