Database Concepts Explanation
1. Oracle View: Syntax & Example
A view in Oracle is a virtual table based on the result of a SQL query. It does not store data itself but
retrieves it from the underlying tables.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
CREATE VIEW emp_view AS
SELECT emp_id, emp_name, department
FROM employees
WHERE department = 'HR';
2. SQL Functions
SQL functions are built-in functions used to perform calculations or manipulate data. They can be
categorized as follows:
- Aggregate Functions: SUM(), AVG(), COUNT(), MAX(), MIN()
- String Functions: CONCAT(), SUBSTR(), LENGTH(), UPPER(), LOWER()
- Date Functions: NOW(), CURDATE(), DATE_ADD(), DATE_SUB()
- Numeric Functions: ROUND(), CEIL(), FLOOR(), ABS()
Example:
SELECT AVG(salary) FROM employees;
3. GROUP BY & HAVING Clause
GROUP BY is used to group rows with the same values in specified columns, often used with
aggregate functions. HAVING is used to filter groups based on a condition.
Syntax:
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1
HAVING condition;
Example:
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 5000;
4. Oracle Operators
Operators in Oracle are used to perform operations on data. Some key types include:
- Arithmetic Operators: +, -, *, /
- Comparison Operators: =, !=, >, <, >=, <=
- Logical Operators: AND, OR, NOT
- Special Operators: IN, BETWEEN, LIKE, IS NULL
Example:
SELECT * FROM employees WHERE salary BETWEEN 3000 AND 5000;
5. Cursor & Its Attributes
A cursor in PL/SQL is a pointer to the result set of a query. It is used to process each row returned
by the query.
Attributes:
- %FOUND: Returns TRUE if the query returns a row.
- %NOTFOUND: Returns TRUE if the query does not return a row.
- %ROWCOUNT: Returns the number of rows affected by the query.
- %ISOPEN: Returns TRUE if the cursor is open.
Example:
DECLARE
CURSOR emp_cursor IS SELECT * FROM employees;
BEGIN
OPEN emp_cursor;
FETCH emp_cursor INTO emp_record;
CLOSE emp_cursor;
END;
6. SELECT Statement
The SELECT statement is used to retrieve data from one or more tables.
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
SELECT emp_name, salary FROM employees WHERE department = 'IT';
7. Procedure Explanation
A procedure in PL/SQL is a named block of code that performs a specific task. Procedures can
accept parameters and can be reused.
Syntax:
CREATE OR REPLACE PROCEDURE procedure_name (parameter_name datatype)
AS
BEGIN
-- Code logic here
END;
Example:
CREATE OR REPLACE PROCEDURE increase_salary (emp_id NUMBER, increment NUMBER)
AS
BEGIN
UPDATE employees
SET salary = salary + increment
WHERE id = emp_id;
END;