SQL Practice Logo

SQLPractice Online

Oracle: PL/SQL Basics: Functions

Module: Database-Specific Features

**Block Structure**: DECLARE ... BEGIN ... EXCEPTION ... END; /

**Variables**: variable_name datatype [:= initial_value]; Use %TYPE for column type, %ROWTYPE for row

**IF Statement**: IF condition THEN ... ELSIF condition THEN ... ELSE ... END IF;

**CASE**: CASE expression WHEN value THEN result ... ELSE default END;

**LOOP**: LOOP ... EXIT WHEN condition; END LOOP; or WHILE condition LOOP ... END LOOP; or FOR i IN 1..n LOOP ... END LOOP;

**Procedure**: CREATE OR REPLACE PROCEDURE name(param IN/OUT type) AS BEGIN ... END;

**Function**: CREATE OR REPLACE FUNCTION name(param IN type) RETURN type AS BEGIN ... RETURN value; END;

**Exception**: EXCEPTION WHEN exception_name THEN ... WHEN OTHERS THEN ... END;

**Cursor**: CURSOR name IS SELECT ...; OPEN name; FETCH name INTO ...; CLOSE name; or FOR rec IN cursor LOOP ... END LOOP;

Block structure: DECLARE (optional) ... BEGIN (required) ... EXCEPTION (optional) ... END; /

Variables: variable_name datatype [:= value]; Use %TYPE for column type, %ROWTYPE for entire row

IF statement: IF condition THEN ... ELSIF condition THEN ... ELSE ... END IF;

CASE: CASE expression WHEN value THEN result ... ELSE default END; or CASE WHEN condition THEN ... END;

Loops: FOR i IN 1..n LOOP ... END LOOP; or WHILE condition LOOP ... END LOOP; or LOOP ... EXIT WHEN condition; END LOOP;

Procedure: CREATE OR REPLACE PROCEDURE name(param IN/OUT/IN OUT type) AS BEGIN ... END;

Function: CREATE OR REPLACE FUNCTION name(param IN type) RETURN type AS BEGIN ... RETURN value; END;

Exception: EXCEPTION WHEN exception_name THEN ... WHEN OTHERS THEN ... END;

PL/SQL: AS keyword, %TYPE/%ROWTYPE, PRAGMA, DBMS_OUTPUT, forward slash to execute

T-SQL: AS keyword, no %TYPE (use table types), PRINT instead of DBMS_OUTPUT, GO to execute

PL/pgSQL: AS $$ keyword, %TYPE/%ROWTYPE supported, RAISE NOTICE, semicolon to execute

Stored procedures: BEGIN/END, no %TYPE, SELECT for output, DELIMITER to change statement terminator

Core references in this topic include WHERE, =, <, >, <=, >=. Learn what each one does, when to use it, and the execution or engine rules that matter.

WHERE

Filters rows before projection and sorting. It decides which rows continue through the query pipeline.

SELECT ... FROM table WHERE condition;

Most performance issues start with a weak WHERE clause or a missing supporting index.

=

Returns rows where the left and right values are exactly equal.

column = value

Use with exact matches. Do not use = NULL.

<, >, <=, >=

Range comparison operators for less-than, greater-than, and inclusive boundary checks.

salary >= 80000

IS NULL / IS NOT NULL

Tests whether a value is missing. SQL NULL semantics require dedicated NULL predicates.

manager_id IS NULL

Never use = NULL or != NULL.

BETWEEN

Checks whether a value falls inside an inclusive lower/upper range.

order_total BETWEEN 100 AND 500