You are on page 1of 6

12/1/2016 SQL Interview Questions

Express To Learn (Home/) Health Tips

Data Warehouse (Data-Warehouse-Introduction/) Informatica (Transformations/) ODI (ODI-Interview-Questions/)

<< Previous UNIX (UNIX/) Contact Us (Contact-Us/) Blog (
What are materialized views?
A materialized view provides indirect access to table data by storing the results of a query in a separate
schema object. It is unlike an ordinary view which does not take up any storage space or contain any data
Materialized views are schema objects that can be used to summarize, precompute, replicate, and
distribute data. E.g. to construct a data warehouse.
If you update or insert a record in a view the corresponding table will be affected. But if you update the
same in materialized view the corresponding table will not be affected.
What is data abstraction?
Data abstraction lets you extract the essential properties of data while ignoring unnecessary details. Once
you design a data structure, you can forget the details and focus on designing algorithms that manipulate
the data structure.
What is meant by base type & subtype (Data Types)? Give some examples?
A base type is the data type from which a subtype is derived. A subtype associates a base type with a
constraint and so defines a subset of values.
1.   Number Types:
You use the BINARY_INTEGER data type to store signed integers. Its magnitude range is -2147483647..
2147483647. Like PLS_INTEGER values, BINARY_INTEGER values require less storage than NUMBER values.
However, most BINARY_INTEGER operations are slower than PLS_INTEGER operations.
·         NATURAL 
·         POSITIVE ---- these 2 let you restrict an integer variable to non-negative or positive        
·         NATURALN­1/ 1/6
12/1/2016 SQL Interview Questions

·         POSITIVEN --- these 2 prevent the assigning of nulls to an integer variable.
·         SIGNTYPE --- lets you restrict an integer variable to the values -1, 0, and 1, which is 
                                          useful in programming tri-state logic.
NUMBER subtypes:
You use the NUMBER data type to store fixed-point or floating-point numbers of virtually any size. Its
magnitude range is 1E-130.. 10E125. If the value of an expression falls outside this range, you get a
numeric overflow or underflow error. You can specify precision, which is the total number of digits, and
scale, which is the number of digits to the right of the decimal point
·         DEC
·         DECIMAL
·         NUMERIC --- these 3 are used to declare fixed-point numbers with a maximum
                             precision of 38 decimal digits.
·         FLOAT --- these 2 are used to declare floating-point numbers with a maximum 
                       precision of 126 binary digits, which is roughly equivalent to 38 decimal     
·         REAL --- used to declare floating-point numbers with a maximum precision of 63
                     binary digits, which is roughly equivalent to 18 decimal digits.
·         INTEGER
·         INT
·         SMALLINT --- these 3 are used to declare integers with a maximum precision of 38
                             decimal digits.
You use the PLS_INTEGER data type to store signed integers. Its magnitude range is -2147483647..
2147483647. PLS_INTEGER values require less storage than NUMBER values. Also, PLS_INTEGER
operations use machine arithmetic, so they are faster than NUMBER and BINARY_INTEGER operations,
which use library arithmetic. For better performance, use PLS_INTEGER for all calculations that fall within
its magnitude range.
2.  Character Types:
You use the CHAR data type to store fixed-length character data. The CHAR data type takes an optional
parameter that lets you specify a maximum length up to 32767 bytes. If you do not specify a maximum
length, it defaults to 1. Remember, you specify the maximum length in bytes, not characters. So, if a CHAR­1/ 2/6
12/1/2016 SQL Interview Questions

(n) variable stores multi-byte characters, its maximum length is less than n characters. The maximum
width of a CHAR database column is 2000 bytes. So, you cannot insert CHAR values longer than 2000 bytes
into a CHAR column.
You use the LONG data type to store variable-length character strings. The LONG data type is like the
VARCHAR2 data type, except that the maximum length of a LONG value is 32760 bytes.
You use the LONG RAW data type to store binary data or byte strings. LONG RAW data is like LONG data,
except that LONG RAW data is not interpreted by PL/SQL. The maximum length of a LONG RAW value is
32760 bytes.
LONG columns can store text, arrays of characters, or even short documents. You can reference LONG
columns in UPDATE, INSERT, and (most) SELECT statements, but not in expressions, SQL function calls, or
certain SQL clauses such as WHERE, GROUP BY, and CONNECT BY.
You use the RAW data type to store binary data or byte strings. For example, a RAW variable might store a
sequence of graphics characters or a digitized picture. Raw data is like VARCHAR2 data, except that PL/SQL
does not interpret raw data. The RAW data type takes a required parameter that lets you specify a
maximum length up to 32767 bytes.
The maximum width of a RAW database column is 2000 bytes. So, you cannot insert RAW values longer
than 2000 bytes into a RAW column.
Internally, every database table has a ROWID pseudo column, which stores binary values called rowids.
Each rowid represents the storage address of a row. A physical rowid identifies a row in an ordinary table.
A logical rowid identifies a row in an index-organized table. The ROWID data type can store only physical
rowids. However, the UROWID (universal rowid) data type can store physical, logical, or foreign (non-
Oracle) rowids.
When you select or fetch a rowid into a UROWID variable, you can use the built-in function
ROWIDTOCHAR, which converts the binary value into an 18-byte character string. Conversely, the function
CHARTOROWID converts a UROWID character string into a rowid. If the conversion fails because the
character string does not represent a valid rowid, PL/SQL raises the predefined exception
SYS_INVALID_ROWID. This also applies to implicit conversions.
Physical Rowids Physical rowids provide fast access to particular rows. As long as the row exists, its
physical rowid does not change. Efficient and stable, physical rowids are useful for selecting a set of rows,
operating on the whole set, and then updating a subset.­1/ 3/6
12/1/2016 SQL Interview Questions

A physical rowid can have either of two formats. The 10-byte extended rowid format supports tablespace-
relative block addresses and can identify rows in partitioned and non-partitioned tables. The 6-byte
restricted rowid format is provided for backward compatibility.
The format, OOOOOOFFFBBBBBBRRR, has four parts:
OOOOOO: The data object number (AAAAqc in the example above) identifies the database segment.
Schema objects in the same segment, such as a cluster of tables, have the same data object number.
FFF: The file number (AAB in the example) identifies the data file that contains the row. File numbers are
unique within a database.
BBBBBB: The block number (AAADFN in the example) identifies the data block that contains the row. Block
numbers are relative to their data file, not their tablespace. So, two rows in the same tablespace but in
different data files can have the same block number.
RRR: The row number (AAH in the example) identifies the row in the block.
Logical Rowids Logical rowids provide the fastest access to particular rows. Oracle uses them to construct
secondary indexes on index-organized tables. Having no permanent physical address, a logical rowid can
move across data blocks when new rows are inserted. However, if the physical location of a row changes,
its logical rowid remains valid.
A logical rowid can include a guess, which identifies the block location of a row at the time the guess is
made. Bypassing a full key search, Oracle uses the guess to search the block directly. However, as new
rows are inserted, guesses can become stale and slow down access to rows. To obtain fresh guesses, you
can rebuild the secondary index.
You can use the ROWID pseudo column to select logical rowids (which are opaque values) from an index-
organized table. Also, you can insert logical rowids into a column of type UROWID, which has a maximum
size of 4000 bytes.
The ANALYZE statement helps you track the staleness of guesses. This is useful for applications that store
rowids with guesses in a UROWID column, and then use the rowids to fetch rows. However, when fetching
from highly volatile tables, it’s a good idea to use rowids without guesses.
You use the VARCHAR2 datatype to store variable-length character data. The VARCHAR2 datatype takes a
required parameter that specifies a maximum length up to 32767 bytes. You cannot use a constant or
variable to specify the maximum length; you must use an integer literal in the range 1.. 32767.­1/ 4/6
12/1/2016 SQL Interview Questions

The VARCHAR2 datatype involves a trade-off between memory use and efficiency. For a VARCHAR2 (>=
2000) variable, PL/SQL dynamically allocates only enough memory to hold the actual value. However, for a
VARCHAR2 (< 2000) variable, PL/SQL preallocates enough memory to hold a maximum-size value. So, for
example, if you assign the same 500-byte value to a VARCHAR2 (2000) variable and to a VARCHAR2(1999)
variable, the latter uses 1499 bytes more memory. The maximum width of a VARCHAR2 database column
is 4000 bytes. Therefore, you cannot insert VARCHAR2 values longer than 4000 bytes into a VARCHAR2
What are LOB types?
The LOB (large object) datatypes BFILE, BLOB, CLOB, and NCLOB let you store blocks of unstructured data
(such as text, graphic images, video clips, and sound waveforms) up to four gigabytes in size. And, they
allow efficient, random, piece-wise access to the data.
LOB types store values, called lob locators, that specify the location of large objects stored in an external
file, in-line (inside the row) or out-of-line (outside the row). Database columns of type BLOB, CLOB, NCLOB,
or BFILE store the locators. BLOB, CLOB, and NCLOB data is stored in the database, in or outside the row.
BFILE data is stored in operating system files outside the database.
PL/SQL operates on LOBs through the locators. For example, when you retrieve a BLOB column value,
only a locator is returned. Locators cannot span transactions or sessions. So, you cannot save a locator in
a PL/SQL variable during one transaction or session, then use it in another transaction or session. To
manipulate LOBs, use the supplied package DBMS_LOB.
What are the differences between LOB types & LONG/LONGRAW data types?
The LOB types differ from the LONG and LONG RAW types in several ways. For example, LOBs (except
NCLOB) can be attributes of an object type, but LONGs cannot. The maximum size of a LOB is four
gigabytes, but the maximum size of a LONG is two gigabytes. Also, LOBs support random access to data,
but LONGs support only sequential access.
What is the syntax for user defined subtypes? Give some examples? What is its type compatibility?
You can define your own subtypes in the declarative part of any PL/SQL block, subprogram, or package
using the syntax:
SUBTYPE subtype_name IS base_type [NOT NULL];
Where subtype_name is a type specifier used in subsequent declarations and base_type is any scalar or
user-defined PL/SQL type. To specify base_type, you can use %TYPE, which provides the datatype of a
variable or database column. Also, you can use %ROWTYPE, which provides the rowtype of a cursor,
cursor variable, or database table. Some examples follow:
SUBTYPE BirthDate IS DATE NOT NULL; -- based on DATE type­1/ 5/6
12/1/2016 SQL Interview Questions

SUBTYPE Counter IS NATURAL; -- based on NATURAL subtype
SUBTYPE DutyRoster IS NameList; -- based on TABLE type
TYPE TimeRec IS RECORD (minutes INTEGER, hours INTEGER);
SUBTYPE FinishTime IS TimeRec; -- based on RECORD type
SUBTYPE ID_Num IS emp.empno%TYPE; -- based on column type
SUBTYPE DeptFile IS c1%ROWTYPE; -- based on cursor rowtype
But you cannot define size-constrained subtypes directly; you can use a simple workaround to define
them indirectly. Just declare a size-constrained variable, then use %TYPE to provide its datatype, as shown
in the following example:
temp VARCHAR2(15);
SUBTYPE Word IS temp%TYPE; -- maximum size of Word is 15
Type Compatibility:
An unconstrained subtype is interchangeable with its base type.
Different subtypes are interchangeable if they have the same base type.
Different subtypes are also interchangeable if their base types are in the same datatype family.
When do we use DEFAULT declaration & assignment operator for variables to assign a value?
<< Previous
© 2016 DEFAULT for variables that have a typical value. Use the assignment operator for variables (such as
( accumulators) that have no typical value. A couple of examples follow:
  Source of the content is gathered from multiple sources on the web. Owner is not responsible for any false or incorrect information,if  any correction needed  to the content report it in the contact us (Contact­Us/)
hours_worked INTEGER DEFAULT 40; page or contact

employee_count INTEGER := 0;­1/ 6/6