Showing posts with label Interview Questions. Show all posts
Showing posts with label Interview Questions. Show all posts

Tuesday, July 16, 2019

Real Time Scenarios in SQL Queries

Scenario 1 :  What is Query to find Second highest salary for employee?

This is most asked Real Time Scenarios in SQL in many industries. There are lot of real time situation where user needs to deal with this kind of situation.User will try multiple queries to find out the same result.
Query 1 :
Select distinct Salary from Employee e1 where 2=Select count(distinct Salary) from Employee e2 where e1.salary<=e2.salary;
Query 2:
select min(salary)from(select distinct salary from emp order by salary desc)where rownum<=2;
Query 3:
select * from(Select S.*,DENSE_RANK() OVER (PARTITION BY DNO ORDER BY SALARY DESC) DR from Source) S Where S.DR=2;

Scenario 2 : Fetching Nth Record from the table.

There are some situations where user needs to find out the Nth records from the table. I will divide this scenario in to 3 parts for better understanding of people.
Query 1 :  Query to find First Record from the table.
 Select * from Employee where Rownum =1;
Query 2: Query to find last record from the table.
Select * from Employee where Rowid= select max(Rowid) from Employee;
Query 3 : Query to find Nth Record from the table.
select * from ( select a.*, rownum rnum from ( YOUR_QUERY_GOES_HERE — including the order by ) a where rownum <= N_ROWS ) where rnum >= N_ROWS;

Scenario 3 : Find and delete duplicate rows

There are real world situations where user needs to find and delete duplicate rows from the table. These are most used SQL queries in real world to find the duplicate rows and delete it. When there is a situation where user needs to add unique constraint to column,user needs to delete duplicate rows.
Query 1 :  Query to find duplicate rows.
 select a.* from Employee a where rowid != 
         (select max(rowid) from Employee b where  a.Employee_num =b.Employee_num;
Query 2: Query to delete duplicate rows
Delete from Employee a where rowid !=  (select max(rowid) from Employee b where  a.Employee_num =b.Employee_num;

Scenario 4 : Find a table specific information

There are times where user needs to find out the table specific information.There are so many system tables which will find a table specific information.
Query 1: How to Find table name and its owner?
Make sure that the database user have logged in with SYS user.
Select table_name,Owner from All_tables order by table_name,owner;
Query2:How to find Selected Tables from a User?
SELECT Table_Name FROM User_Tables WHERE Table_Name LIKE ‘STU%’;
Scenario 5 : Find the constraint information
There are so many scenarios in real world that user needs to find out the constraint information.There are so  many constraints used to make the database normalized.The following are some important queries which will gives us the information about the oracle constraints.
Query 1 : How to find all details about Constraints?
SELECT * From User_Constraints;
SELECT * FROM User_Cons_Columns;
Query 2: How to find Constraint Name?
SELECT Table_Name, Constraint_Name FROM User_Constraints;
Query 3: How to find Constraint Name with Column_Name?
SELECT Column_Name, Table_Name, Constraint_Name FROM User_Cons_Columns;
Query 4: How to find Selected Tables which have Constraint?
SELECT Table_Name FROM User_Cons_Columns WHERE Table_Name LIKE ‘STU%’;
Query 5: How to find Constraint_Name, Constraint_Type, Table_Name?
SELECT Table_Name, Constraint_Type, Constraint_Name FROM User_Constraints;
SELECT Table_Name, Constraint_Type, Constraint_Name, Generated FROM User_Constraints;

Scenario 6: How to create a table which has same structure  or how to create duplicate table.

There are so many situations where user needs to create duplicate tables for testing purpose. There are some needs where user needs to create the structure of the table. The following are 2 most important queries which are used in 90% of Real Time Scenarios in SQL.
Query 1: Create the duplicate table with data
Create table Employee_1 as Select * from Employee;
Query 2: Create the table structure duplicate to another table.
Create table Employee_1 as Select * from Employee where 1=2;

Scenario 7 : Finding the procedures information

There are situations in Real Time Scenarios of SQL where user needs to find out the procedures information.
Query 1 :How to check Procedures?
 SELECT * FROM User_Source
WHERE Type=’PROCEDURE’
AND NAME IN (‘SP_CONNECTED_AGG’,’SP_UNCONNECTED_AGG’);
Query 2:How to find procedure columns information?
select OWNER, OBJECT_NAME, ARGUMENT_NAME, DATA_TYPE, IN_OUT from ALL_ARGUMENTS order by OWNER, OBJECT_NAME, SEQUENCE;

Scenario 8: Scenario of Self Join

We need to check the table which are joined with itself.There are the situations where user needs to join the table with itself. I will try to give one query which explains the scenario of self join.
Query : The query to find out the manager of employee
Select e.employee_name,m.employee name from Employee e,Employee m where e.Employee_id=m.Manager_id;

Scenario 9 : Email validation of SQL

There is need to add the email validation using SQL queries.These are also most common Real Time Scenarios in SQL.
Query : How to add the email validation using only one query?
User needs to use REGEXP_LIKE function for email validation.
 SELECT
Email
FROM
Employee
where NOT REGEXP_LIKE(Email, ‘[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}’, ‘i’);

How to Generate Permutations in SQL 



with mydata as (
    select 'A' mycol from dual
    union all
    select 'B' mycol from dual
    union all
    select 'C' mycol from dual
    union all
    select 'D' mycol from dual
    union all
    select 'E' mycol from dual
    union all
    select 'F' mycol from dual
    union all
    select 'G' mycol from dual
    union all
    select 'H' mycol from dual
)
select c1.mycol , c2.mycol
from mydata c1, mydata c2
where c1.mycol <> c2.mycol

Friday, December 16, 2011

Triggers in Oracle Forms


Triggers in Oracle Forms

Triggers are blocks of PL/SQL code that are written to perform tasks when a specific event occurs within an application. In effect, an Oracle Forms trigger is an event-handler written in PL/SQL to augment (or occasionally replace) the default processing behavior. Every trigger has a name, and contains one or more PL/SQL statements. A trigger encapsulates PL/SQL code so that it can be associated with an event and executed and maintained as a distinct object.

Block Processing Triggers:

Block processing triggers fire in response to events related to record management in a block.
·                        When-Create-Record Perform an action whenever Oracle Forms attempts to create a new record in a block.
·                        When-Clear-Block Perform an action whenever Oracle Forms flushes the current block; that is, removes all records from the block.
·                        When-Database-Record Perform an action whenever Oracle Forms changes a record’s status to Insert or Update, thus indicating that the record should be processed by the next COMMIT_FORM operation.

Interface Event Triggers:

Interface event triggers fire in response to events that occur in the form interface. Some of these triggers, such as When-Button-Pressed, fire only in response to operator input or manipulation. Others, like When-Window-Activated, can fire in response to both operator input and programmatic control.
·                        When-Button-Pressed Initiate an action when an operator selects a button, either with the mouse or through keyboard selection.
·                        When-Checkbox-Changed Initiate an action when the operator toggles the state of a check box, either with the mouse or through keyboard selection.
·                        When-Image-Activated Initiate an action whenever the operator double-clicks an image item.
·                        When-Image-Pressed Initiate an action whenever an operator clicks on an image item.
·                        When-Radio-Changed Initiate an action when an operator changes the current radio button selected in a radio group item.
·                        When-Window-Activated Initiate an action whenever an operator or the application activates a window.
·                        When-Window-Closed Initiate an action whenever an operator closes a window with the window manager’s Close command.
·                        When-Window-Deactivated Initiate an action whenever a window is deactivated as a result of another window becoming the active window.

Master/Detail Triggers:

Oracle Forms generates master/detail triggers automatically when a master/detail relation is defined between blocks. The default master/detail triggers enforce coordination between records in a detail block and the master record in a master block. Unless developing custom block-coordination schemes, you do not need to define these triggers.
·                        On-Check-Delete-Master Fires when Oracle Forms attempts to delete a record in a block that is a master block in a master/detail relation.
·                        On-Clear-Details Fires when Oracle Forms needs to clear records in a block that is a detail block in a master/detail relation because those records no longer correspond to the current record in the master block.
·                        On-Populate-Details Fires when Oracle Forms needs to fetch records into a block that is the detail block in a master/detail relation so that detail records are synchronized with the current record in the master block.

Message-Handling Triggers:

Oracle Forms automatically issues appropriate error and informational messages in response to runtime events. Message handling triggers fire in response to these default messaging events.
·                        On-Error Replace a default error message with a custom error message, or to trap and recover from an error.
·                        On-Message To trap and respond to a message; for example, to replace a default message issued by Oracle Forms with a custom message.

Validation Triggers:

Validation triggers fire when Oracle Forms validates data in an item or record. Oracle Forms performs validation checks during navigation that occurs in response to operator input, programmatic control, or default processing, such as a Commit operation.
·                        When-Validate-Item
·                        When-Validate-Record

Navigational Triggers:

Navigational triggers fire in response to navigational events. Navigational triggers can be further sub-divided into two categories: Pre- and Post- triggers, and When-New-Instance triggers. Pre- and Post- Triggers fire as Oracle Forms navigates internally through different levels of the object hierarchy. When-New-Instance-Triggers fire at the end of a navigational sequence that places the input focus on a different item.
·                        Pre-Form Perform an action just before Oracle Forms navigates to the form from “outside” the form, such as at form startup.
·                        Pre-Block Perform an action before Oracle Forms navigates to the block level from the form level.
·                        Pre-Record Perform an action before Oracle Forms navigates to the record level from the block level.
·                        Pre-Text-Item Perform an action before Oracle Forms navigates to a text item from the record level.
·                        Post-Text-Item Manipulate an item when Oracle Forms leaves a text item and navigates to the record level.
·                        Post-Record Manipulate a record when Oracle Forms leaves a record and navigates to the block level.
·                        Post-Block Manipulate the current record when Oracle Forms leaves a block and navigates to the form level.
·                        Post-Form Perform an action before Oracle Forms navigates to “outside” the form, such as when exiting the form.
·                        When-New-Form-Instance Perform an action at form start-up. (Occurs after the Pre-Form trigger fires).
·                        When-New-Block-Instance Perform an action immediately after the input focus moves to an item in a block other than the block that previously had input focus.
·                        When-New-Record-Instance Perform an action immediately after the input focus moves to an item in a different record.
·                        When-New-Item-Instance Perform an action immediately after the input focus moves to a different item. 

Transactional Triggers:

Transactional triggers fire in response to a wide variety of events that occur as a form interacts with the data source.
·                        On-Delete
·                        On-Insert
·                        On-Update
·                        On-Logon
·                        On-Logout
·                        Post-Database-Commit
·                        Post-Delete
·                        Post-Insert
·                        Post-Update
·                        Pre-Commit
·                        Pre-Delete
·                        Pre-Insert
·                        Pre-Update

Query-Time Triggers:

Query-time triggers fire just before and just after the operator or the application executes a query in a block.
·                        Pre-Query Validate the current query criteria or provide additional query criteria programmatically, just before sending the SELECT statement to the database.
·                        Post-Query Perform an action after fetching a record, such as looking up values in other tables based on a value in the current record. Fires once for each record fetched into the block.