Oracle FOR LOOP SELECT Statement Example

In this tutorial, I am giving an Oracle FOR LOOP SELECT statement example. You can use SELECT statement inside FOR LOOP in Oracle to loop through SELECT results using PL/SQL.

FOR LOOP IN SELECT Syntax

FOR cursor_variable IN (select_statement) LOOP
-- commands to execute
END LOOP;

Oracle FOR LOOP SELECT Statement Example

In the following example, it will loop through all the records from EMP table where department is equal to 10 and will print the employees salary.

SET SERVEROUTPUT ON;
BEGIN
   FOR c IN (SELECT EMPNO, ENAME, SAL
               FROM emp
              WHERE deptno = 10)
   LOOP
      DBMS_OUTPUT.PUT_LINE (
         'Salary for the employee ' || c.ename || ' is: ' || c.sal);
   END LOOP;
END;
/

Output:

Salary for the employee CLARK is: 2450
Salary for the employee KING is: 5000
Salary for the employee MILLER is: 1300
PL/SQL procedure successfully completed.

See also:

Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 20 years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.