Oracle
Oracle
SUBJECT: ORACLE
Q1. Solve
1. Consider the following table to solve any five the following SQL
queries:
Employee (Empno, Ename, Salary, Joining_date, Birth_date, Job_id) Job
(Job_id, Job_desc, Job Title, Max_salary, Min_salary)
SELECT *
FROM Employee
WHERE Ename LIKE 'P%';
SELECT Employee.*
FROM Employee
JOIN Job ON Employee.Job_id = Job.Job_id
WHERE Employee.Salary > Job.Min_salary;
e) Display list of employees who have joined after ‘1st January, 2010’
Ans:
SELECT *
FROM Employee
WHERE Joining_date > '2010-01-01';
SELECT *
FROM Employee
JOIN Job ON Employee.Job_id = Job.Job_id
WHERE Job_title = 'MANAGER';
1. VIEWS
Ans: In Oracle, views are virtual tables that provide a customized and logical
representation of data stored in one or more underlying tables. They are
created by combining data from different tables or selecting specific columns
and rows from existing tables. Views are a powerful tool in database
management as they allow users to retrieve data without directly accessing or
modifying the underlying tables.
Here are some key points about CLOB data type in Oracle:
3. Storage: CLOB data is stored in a separate segment from the rest of the
table data. It can be stored inline (in the same data block as the table) or in an
out-of-line manner (in a separate segment).
1. GROUP BY Clause:
- The GROUP BY clause is used to group rows from a table based on one or
more columns.
- It divides the result set into groups, where each group has the same values
for the specified column(s).
- It is often used with aggregate functions like COUNT, SUM, AVG, etc., to
perform calculations on each group of data.
- The columns specified in the GROUP BY clause determine the grouping
criteria.
2. HAVING Clause:
- The HAVING clause is used in combination with the GROUP BY clause to
filter the result set based on conditions applied to the grouped data.
- It allows you to specify conditions that must be satisfied by the groups
selected by the GROUP BY clause.
- The HAVING clause operates similarly to the WHERE clause, but it works on
groups rather than individual rows.
- It can use aggregate functions and column aliases defined in the SELECT
statement.
- The conditions in the HAVING clause are applied after the grouping and
aggregation have taken place.
Example:
The GROUP BY and HAVING clauses are powerful tools for aggregating and
filtering data in SQL. They allow you to perform calculations and apply
conditions to grouped data, providing flexibility and control over the result set
based on specific criteria.
Q3. Solve
1. INNER JOIN:
An inner join returns only the rows that have matching values in both tables
being joined. It combines rows from two tables based on the specified join
condition.
Syntax:
SELECT *
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
```
Example:
SELECT *
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id;
```
This query combines the "employees" and "departments" tables based on the
matching "department_id" column. It returns rows where there is a match
between the two tables.
Syntax:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
```
Example:
SELECT *
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;
```
This query performs a left join between the "employees" and "departments"
tables. All rows from the "employees" table are returned, and the corresponding
matching rows from the "departments" table are included. If there is no match,
NULL values are returned for the "departments" columns.
Syntax:
SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
```
Example:
SELECT *
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;
```
This query performs a right join between the "employees" and "departments"
tables. All rows from the "departments" table are returned, and the
corresponding matching rows from the "employees" table are included. If there
is no match, NULL values are returned for the "employees" columns.
Syntax:
SELECT *
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
```
Example:
SELECT *
FROM employees
FULL JOIN departments
ON employees.department_id = departments.department_id;
```
This query performs a full join between the "employees" and "departments"
tables. It returns all rows from both tables and combines them based on the
matching "department_id" column. If there is no match, NULL values are
returned for the non-matching columns.
These are the commonly used join operations in Oracle. Each type of join
allows you to combine data from multiple tables based on different conditions,
providing flexibility in retrieving and analyzing data from related tables.
Oracle supports various types of constraints. Let's explore the different types:
Syntax:
2. UNIQUE Constraint:
The UNIQUE constraint ensures that each value in a column or a set of
columns is unique across the table. It prevents duplicate values from being
inserted or updated.
Syntax:
Syntax:
Syntax:
5. CHECK Constraint:
The CHECK constraint validates the data entered into a column based on a
specified condition or expression. It allows you to define custom rules for data
integrity.
Syntax:
Syntax:
Q4. Solve
1. What is mean by Cursor? Explain different types of Cursor available in
Oracle
Ans: In Oracle, a cursor is a database object used to retrieve and manipulate
data in a result set. It provides a way to iterate over the rows returned by a
SQL query and perform operations on them. A cursor acts as a pointer to the
result set, allowing you to fetch and process data row by row.
1. Implicit Cursor:
- An implicit cursor is automatically created by Oracle whenever a SQL
statement is executed.
- It is used for executing single-row queries or DML (Data Manipulation
Language) statements such as INSERT, UPDATE, DELETE.
- Implicit cursors are managed by Oracle internally and do not need to be
explicitly declared or controlled by the user.
Example:
BEGIN
SELECT column1 INTO variable
FROM table1
WHERE condition;
In this example, the implicit cursor is used to execute the SELECT statement
and fetch the value of "column1" into the "variable" for further processing.
2. Explicit Cursor:
- An explicit cursor is explicitly declared and managed by the user.
- It provides more control and flexibility over result set retrieval and
processing.
- Explicit cursors are used when you need to work with multiple rows or when
you need to explicitly open, fetch, and close the cursor.
Syntax:
DECLARE
cursor_name cursor_type;
BEGIN
-- Cursor declaration
OPEN cursor_name;
-- Cursor processing
FETCH cursor_name INTO variables;
-- Perform operations using the fetched values
CLOSE cursor_name;
END;
```
Example:
DECLARE
CURSOR cursor_name IS
SELECT column1, column2
FROM table1
WHERE condition;
variable1 table1.column1%TYPE;
variable2 table1.column2%TYPE;
BEGIN
OPEN cursor_name;
LOOP
FETCH cursor_name INTO variable1, variable2;
EXIT WHEN cursor_name%NOTFOUND;
CLOSE cursor_name;
END;
```
Explicit cursors provide more control and flexibility in handling result sets,
allowing you to perform various operations on retrieved data. They are
especially useful when dealing with multiple rows or when you need to control
the cursor behavior explicitly.
2. Explain different SQL functions available in oracle
Ans: Oracle provides a wide range of built-in SQL functions that can be used to
perform various operations on data. Here are some commonly used SQL
functions available in Oracle:
1. Aggregate Functions:
- COUNT(): Returns the number of rows or non-null values in a column.
- SUM(): Calculates the sum of values in a column.
- AVG(): Computes the average of values in a column.
- MIN(): Retrieves the minimum value from a column.
- MAX(): Retrieves the maximum value from a column.
2. String Functions:
- CONCAT(): Concatenates two or more strings together.
- SUBSTR(): Extracts a substring from a string.
- INSTR(): Finds the position of a substring within a string.
- REPLACE(): Replaces occurrences of a substring in a string.
- UPPER(): Converts a string to uppercase.
- LOWER(): Converts a string to lowercase.
- TRIM(): Removes leading or trailing spaces from a string.
3. Date Functions:
- SYSDATE: Returns the current system date and time.
- TO_CHAR(): Converts a date or timestamp to a specific format.
- TO_DATE(): Converts a string to a date using a specified format.
- EXTRACT(): Extracts a specific part (year, month, day, etc.) from a date.
4. Numeric Functions:
- ROUND(): Rounds a number to a specified decimal place or precision.
- TRUNC(): Truncates a number to a specified decimal place or precision.
- ABS(): Returns the absolute value of a number.
- MOD(): Computes the remainder of a division operation.
5. Conversion Functions:
- TO_NUMBER(): Converts a string to a number.
- TO_CHAR(): Converts a number or date to a string.
- TO_DATE(): Converts a string to a date.
6. Conditional Functions:
- CASE: Performs conditional operations based on specified conditions.
7. Analytic Functions:
- RANK(): Assigns a rank to each row based on the specified criteria.
- ROW_NUMBER(): Assigns a unique sequential number to each row.
- LAG() and LEAD(): Accesses data from previous or subsequent rows in a
result set.
These are just a few examples of the SQL functions available in Oracle. Oracle
provides a rich set of functions to manipulate, transform, and analyze data in
various ways. By leveraging these functions, you can perform complex
calculations, manipulate strings, convert data types, and more within your
SQL queries.
Q5. Solve
1. Declare a cursor that selects the required columns from the Employee table,
ordered by salary in descending order.
2. Use the FETCH statement to fetch the first five rows from the cursor.
3. Create variables to store the column values fetched from each row.
4. Loop through the cursor result set and display the values.
5. Close the cursor when finished.
DECLARE
CURSOR top_salary_cursor IS
SELECT employee_id, first_name, last_name, salary
FROM employee
ORDER BY salary DESC;
-- Counter variable
v_counter INTEGER := 0;
BEGIN
OPEN top_salary_cursor;
LOOP
FETCH top_salary_cursor INTO v_employee_id, v_first_name, v_last_name,
v_salary;
EXIT WHEN top_salary_cursor%NOTFOUND OR v_counter = 5;
v_counter := v_counter + 1;
END LOOP;
CLOSE top_salary_cursor;
END;
/
```
2. Write a Function Fact() to test whether the input number is ODD or EVEN
Ans: To write a function in Oracle that tests whether an input number is odd
or even, you can use the modulus operator (%). The modulus operator
calculates the remainder when one number is divided by another. If the
remainder is 0, the number is even; otherwise, it is odd. Here's an example:
You can then call this function in a SQL query or PL/SQL code to test whether
a number is odd or even. Here's an example:
DECLARE
input_num NUMBER := 10;
result VARCHAR2(10);
BEGIN
result := is_odd_or_even(input_num);
DBMS_OUTPUT.PUT_LINE('The number ' || input_num || ' is ' || result);
END;
/
```
In this code snippet, the `input_num` variable is set to 10, and the
`is_odd_or_even` function is called to determine if it is odd or even. The result
is stored in the `result` variable and displayed using
`DBMS_OUTPUT.PUT_LINE()`.
You can modify the value of `input_num` variable to test different numbers and
check their odd or even status using the function.