0% found this document useful (0 votes)
103 views

Indexes and View and Other Important Interview Questions

Indexes are used to access data in database tables faster. The main types of indexes in Oracle are B-tree indexes and bitmap indexes. B-tree indexes are the default index type and are good for indexing key columns. Bitmap indexes are useful when certain values are repeated continuously. Other index types include function-based indexes, unique indexes, reverse key indexes, and compressed indexes. Views are virtual tables that are not physically stored and do not store data. They are created using queries that join tables and can hide complexity and provide security by restricting access to columns. Inline views are subqueries in the FROM clause that can replace tables in queries and are also called derived tables or subselects.

Uploaded by

Bathula Sudhakar
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Indexes and View and Other Important Interview Questions

Indexes are used to access data in database tables faster. The main types of indexes in Oracle are B-tree indexes and bitmap indexes. B-tree indexes are the default index type and are good for indexing key columns. Bitmap indexes are useful when certain values are repeated continuously. Other index types include function-based indexes, unique indexes, reverse key indexes, and compressed indexes. Views are virtual tables that are not physically stored and do not store data. They are created using queries that join tables and can hide complexity and provide security by restricting access to columns. Inline views are subqueries in the FROM clause that can replace tables in queries and are also called derived tables or subselects.

Uploaded by

Bathula Sudhakar
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 11

INDEXES in oracle

What is Index and Why Should We Use Index?

Indexes are used to access data in the table faster.    A book is like a table of contents.
When searching for a topic in the book, we can quickly find out the page number of the
topic we’re looking for from the table of contents.
If the book doesn’t have a table of contents, we’ll cycle through the pages until we find
the topic we’re looking for.    The indexes in our database are the same.    Stores a row
in our table and the ROWID information that corresponds to that row.
When a data is searched in a table, Oracle first checks the presence of the index.    If
there is no index, it reads all the records in the table until you find the desired record.   
This is known as FULL TABLE SCAN

When we encounter a low-performing SQL, the first thing we usually check is that the
index we’ve used in the WHERE condition of the query is indexed?

By default, Oracle creates B-tree indexes.


Two Types of Indexes.
1. B Tree (Balanced Tree Index) - Default Index in Oracle
2. Bitmap

B-tree Index
The default index type in the Oracle database is B-tree (Balanced Tree) index.    –
always use for key columns
CREATE INDEX JOB_INDX ON OT.EMPLOYEES(JOB_ID);

CREATE INDEX supplier_idx      ON supplier (supplier_name, city);

Bitmap Index
This index type is the most commonly used index type after B-Tree. This type of index is
used when certain values repeat continuously.
CREATE BITMAP INDEX EXP_1 ON HR.EMPLOYEES(GENDER);

Function Based Index


In this type of index, When you use the function in the Where condition of your queries,
it will not use the index unless you have created the index with this function. In this case
you need to create Function based index
Eg :
Create index MAD_IX on HR.EMPLOYEES(lower(name));

Unique Index
This type of index is the advanced version of the B-Tree index structure. Normally, the
B-Tree index structure is Non-Unique.
When we use the Unique keyword to create this index type, Oracle will create a Unique
index.
This type of index is especially used on columns with unique records. It is created as
follows.
Create unique index MTC_IX on customer(SSID);

Reverse key Index


This index is an improved type of B-Tree index. In this index type, the value of the index
key is always reversed.
Ex; Suppose that our index values are 123, 456, 789, in this case Reversed key index
values will be 321, 654, 987.
This index is created as follows.
create Index MID_IX on customer(customer_id) reverse;
Compressed Index
The use of index compression in indexes with multiple columns can be very useful,
especially if the column contains data repetition.
Less disk space and less I / O operation to achieve compressed index.
CREATE INDEX INDX_COMP ON HR.EMPLOYEES (FIRST_NAME, LAST_NAME) COMPRESS 2;

Employees may have the same name or surname. We’ve done compression on the
concatenated index.

select * from user_indexes where table_name='CUSTOMERS';

DROP INDEX index_name;


VIEWS in oracle

Introduction to Oracle Views


View is actually a virtual table that is not physically stored in the database    and
does not store any sort of data.
It is created generally by using join query with two or more tables which enables
the view to hide the data complexity.
It will also provide security, since It is not storing any data in the view physically so
it actually restricts the access of various columns of a table to the user of the
database.
Views are created by a query joining one or more tables.

Simple views
Complex views

CREATE VIEW in Oracle


The syntax to create the view –
CREATE VIEW view name AS
SELECT column [ , column ] FROM table
WHERE condition;
Eg 1 :
CREATE VIEW empview AS
SELECT *
FROM employees
WHERE employees.manager_id = 100;

Eg 2:
CREATE VIEW employeeview1 AS
SELECT
employee_id,
first_name || ' ' || last_name full_name,
phone_NUMBER
FROM
employees;

We can now check the just created above Oracle VIEW by using this query.
Select * from employeeview;

Eg 3 :    Creating oracle view with columns alias

CREATE VIEW employeeview1 AS


SELECT employee_id, first_name || '    ' || last_name "full name",
FLOOR( months_between ( CURRENT_DATE, hire_date )/ 12 ) as years
FROM employees;
Select * from employeeview1;

Eg: 4 : Access view with where clause and order by

SELECT *
FROM employeeview
WHERE years > 10
ORDER BY full name;

Eg 5 : Creating oracle view with multiple tables

CREATE OR REPLACE VIEW dept_manger AS


SELECT
employee_id,first_name || '    ' || last_name as name, department_name
FROM EMPLOYEES
INNER JOIN DEPARTMENTS
on EMPLOYEES. EMPLOYEE_ID = DEPARTMENTS. MANAGER_ID;

Eg 6 : Delete view by using the Drop statement


Drop view employee_view;
Advantages of Views in an oracle
The view can be created on selected data or column of the table, which restrict
the view of a table and can hide some of the data or column in the tables.
The view creates to view the data without storing the data into the view.
View an be created to Join two or more tables data and store it as one view or
object.
A view is created to achieve security.
The view gives good performance to run complex queries or join queries.
View will not occupies memory space

Disadvantages of Views in an oracle


DML operations cannot perform in view.
The view becomes inactive if the table is dropped on which it is created.
.

             
      INLINE VIEW    ( IMPORTANT INTERVEW QUESTION)
An inline view is not a real view but a subquery in the FROM clause of a SELECT
statement. Consider the following SELECT statement:

SELECT
        column_list
FROM
    (
                SELECT
            *
                FROM
                        table_name
        ) t;

The subquery specified in the FROM clause of a query is called an inline view.
Because an inline view can replace a table in a query, it is also called a derived
table. It is also called as subselect.
Eg    1 :
The following query retrieves the top 10 most expensive products from the
products table:
SELECT
    *
FROM
    (
                SELECT
                        product_id,
                        product_name,
                        list_price
                FROM
                        products
                ORDER BY
                        list_price DESC
    )
WHERE
        ROWNUM <= 10;
In this example, first, the inline view returns all products sorted by list prices in
descending order. And then the outer query retrieves the first 10 rows from the
inline view.

B) Inline view joins with a table example


It returns the product categories and the highest list price of products in each
category:
SELECT
        category_name,
        max_list_price
FROM
        product_categories a,
    (
                SELECT
                        category_id,
                        MAX( list_price ) max_list_price
                FROM
                        products
                GROUP BY
                        category_id
    )b
WHERE
        a.category_id = b.category_id
ORDER BY
        category_name;

Few Important oracle intervew questions as in informatia interview below


1. Different between DECODE and Case
https://tipsfororacle.blogspot.com/2016/09/decode-vs-case.html
2.Difference between Delete vs    Truncate vs Drop
https://tipsfororacle.blogspot.com/2016/09/delete-vs-truncate-vs-drop.html
3. find nth highest salary from a table
https://tipsfororacle.blogspot.com/2016/09/find-nth-highestminimum-salary-
from.html
4.How to Identify and Remove Duplicates records    from Table.
https://tipsfororacle.blogspot.com/2016/09/reason-we-made-mistake-and-
allowed-to.html
5. Analytical and Aggregate Functions
https://tipsfororacle.blogspot.com/2016/09/analytic-functions-vs-aggregate.html
6. Difference Between Rank and DenseRank
https://learnsql.com/cookbook/whats-the-difference-between-rank-and-
dense_rank-in-sql/
7. difference rownum and rowid
8. sub queries co- related sub queries

You might also like