Beyond SELECT, SQL offers tools for reusing logic: views, stored procedures, functions, cursors and triggers. The syntax below follows Oracle PL/SQL, the style used in the syllabus.
Analytical, hierarchical and recursive queries
- Analytical queries use window functions to rank or total rows without collapsing them.
SELECT ename, salary,
RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employee;
The 5th highest salary is the row with rnk = 5.
- Hierarchical queries (Oracle
CONNECT BY) walk a tree such as manager and employee.
SELECT id, parent_id, LEVEL
FROM employee
START WITH parent_id IS NULL
CONNECT BY PRIOR id = parent_id;
- Recursive queries use a common table expression that refers to itself.
WITH RECURSIVE tree AS (
SELECT id, parent_id, 1 AS lvl FROM employee WHERE parent_id IS NULL
UNION ALL
SELECT e.id, e.parent_id, t.lvl + 1
FROM employee e JOIN tree t ON e.parent_id = t.id
)
SELECT * FROM tree;
Views
A view is a saved query that behaves like a table. It hides columns, simplifies queries and adds a layer of security.
CREATE VIEW cs_students AS
SELECT roll_no, name FROM student WHERE dept_id = 1;
Stored procedures and functions
A procedure performs an action; a function returns a value.
CREATE OR REPLACE PROCEDURE raise_price (p_pid IN NUMBER) AS
BEGIN
UPDATE part SET price = price + 1000 WHERE pid = p_pid;
END;
CREATE OR REPLACE FUNCTION supplier_count RETURN NUMBER AS
n NUMBER;
BEGIN
SELECT COUNT(*) INTO n FROM supplier;
RETURN n;
END;
A package groups related procedures and functions together.
Cursors
A cursor lets PL/SQL process a query result one row at a time.
DECLARE
CURSOR c IS SELECT sid, city FROM supplier;
r c%ROWTYPE;
BEGIN
OPEN c;
LOOP
FETCH c INTO r;
EXIT WHEN c%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(r.sid || ' ' || r.city);
END LOOP;
CLOSE c;
END;
Attributes %FOUND, %NOTFOUND, %ROWCOUNT describe the latest fetch.
Triggers
A trigger runs automatically when an event such as INSERT, UPDATE or DELETE happens.
CREATE OR REPLACE TRIGGER price_check
BEFORE UPDATE ON part
FOR EACH ROW
BEGIN
IF :NEW.price < 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Price cannot be negative');
END IF;
END;
Dynamic SQL builds and runs a statement from a text string at run time (EXECUTE IMMEDIATE), useful when table names are not known in advance. Never build it by pasting user input into the text, because that allows SQL injection.
Common mistakes
- Expecting a view to store its own data. It stores only the query.
- Using a trigger for work that a constraint can do more simply.
Practice questions
- What is a view? Give two benefits.
- Differentiate procedure and function.
- What is a cursor? Name two cursor attributes.
- What is a trigger? Write a simple example.
- What is dynamic SQL and what risk does it carry?
Answers to the practice questions
- A saved query used like a table; it simplifies queries and hides data.
- A procedure performs an action; a function returns a value.
- A pointer that processes query rows one at a time;
%FOUNDand%NOTFOUND(also%ROWCOUNT). - Code that runs automatically on an event, such as a BEFORE UPDATE trigger that rejects a negative price.
- SQL built as text at run time; it risks SQL injection if user input is pasted into it.
Sources
- Official BCA syllabus Database Management Systems, course code CC202, Unit II
- Raghu Ramakrishnan and Johannes Gehrke, Database Management Systems, 3rd edition, McGraw-Hill
