Oracle Interview Question
Oracle Interview Question
Q5) What is the difference between a Primary Key & Unique Key?
Primary key is used to identify each table row uniquely, while a Unique key
prevents duplicate values in a table column.
Q6) How can we find out the current date and time in Oracle?
We can find the current date & time using SYSDATE command in Oracle.
Syntax: SELECT SYSDATE into CURRENT_DATE from dual;
Q7) How will you write a query to get 5th RANK students from the table
STUDENT_REPORT?
SELECT TOP 1 RANK
FROM (SELECT TOP 5 RANK
FROM STUDENT_REPORT
ORDER BY RANK DESC) AS STUDENT
ORDER BY RANK ASC;
Q8) When do we use the GROUP BY clause in SQL query?
GROUP BY clause is used to identify and group the data by one or more
columns in the query results. This clause is often used with aggregate
functions like COUNT, MAX, MIN, SUM, AVG, etc.
Syntax:
SELECT COLUMN_1, COLUMN_2
FROM TABLENAME
WHERE [condition]
GROUP BY COLUM_1, COLUMN_2
Ex:
DECODE function
SELECT ORDERNUM
DECODE (STATUS, ‘0’, ‘ORDERED’, ‘P’, ‘PACKED’, ‘S’, ‘SHIPPED’, ‘A’,
‘ARRIVED’)
FROM ORDERS;
CASE function
SELECT ORDERNUM
CASE (WHEN STATUS = ‘0’ THEN ‘ORDERED’
WHEN STATUS = ‘P’ THEN PACKED
WHEN STATUS = ‘S’ THEN ‘SHIPPED’
ELSE ‘ARRIVED’) END
FROM ORDERS;
=> Both the commands will display order numbers with their respective status
as.
IF,
Status 0 = Ordered
Status P = Packed
Status S = Shipped
Status A = Arrived
Various integrity constraints are available which include Primary Key, Foreign
Key, UNIQUE Key, NOT NULL & CHECK.
Q11) What do you mean by MERGE in Oracle and how can we merge two
tables?
The MERGE statement is used to merge the data from two tables. It selects the
data from the source table and inserts/updates.
Syntax:
MERGE INTO TARGET_TABLE_1
USING SOURCE_TABLE_1
ON SEARCH_CONDITION
WHEN MATCHED THEN
INSERT (COL_1, COL_2…)
VALUES (VAL_1, VAL_2…)
WHERE < CONDITION>
WHEN NOT MATCHED THEN
UPDATE SET COL_1=VAL_1, COL_2=VAL_2…
WHEN <CONDITION>
Q12) What are the set operators UNION, UNION ALL, MINUS & INTERSECT
meant to do?
The set operator facilitates the user to fetch the data from two or more than
two tables at once if the columns and relative data types are the same in the
source tables.
UNION operator returns all the rows from both the tables expect the
duplicate rows.
UNION ALL returns all the rows from both the tables along with the
duplicate rows.
MINUS returns rows from the first table, which does not exist in the
second table.
INTERSECT returns only the common rows in both the tables.
Q13) Can we convert a date to char in Oracle and if so, what would be the
syntax?
We can use the TO_CHAR function to do the above conversion.
SELECT to_char (to_date (’30-01-2018’, ‘DD-MM-YYYY’), ‘YYYY-MM-DD’)
FROM dual;
Q14) What do you understand by a database object? Can you list a few of
them?
Object used to store the data or references of the data in a database is known
as a database object. The database consists of various types of DB objects such
as tables, views, indexes, constraints, stored procedures, triggers, etc.
Q16) What do you understand by database schema and what does it hold?
Schema is a collection of database objects owned by a database user who can
create or manipulate new objects within this schema. The schema can contain
any DB objects like table, view, indexes, clusters, stored procs, functions, etc.