Branching and looping constructs in ANSI SQL

December 4, 2026 · Team EasyExamNotes · 3 min read (est.) · 0 comments

Share on WhatsApp

← RGPV CS-502 DBMS December 2024 solved question paper

← RGPV CS-502 DBMS May 2024 solved question paper

← RGPV CS-502 DBMS November 2019 solved question paper

RGPV Previous Year Examination Question

B.Tech. V Semester | CS-502 (GS) | December 2024 | 8(b)(ii)

Question (restated): Write a short note on branching and looping constructs in ANSI SQL.

Answer

Procedural SQL constructs

Branching and looping allow conditional execution and repetition in procedural SQL routines. Standard SQL persistent stored modules (SQL/PSM) define procedural constructs, but database products implement different dialects. Oracle PL/SQL, for example, uses its own syntax.

Branching

IF selects a statement sequence according to a condition. ELSEIF and ELSE provide alternative branches in SQL/PSM-style syntax. CASE can express multiple alternatives; the SQL CASE expression can also be used inside ordinary queries to compute conditional values.

SELECT Name,
  CASE WHEN Marks >= 40 THEN 'Pass'
       ELSE 'Fail' END AS Result
FROM Student WHERE Marks IS NOT NULL;

For marks 75 and 32, the result is Pass and Fail respectively.

Looping

WHILE tests a condition before each iteration. REPEAT executes the body before testing its termination condition. LOOP provides unconditional repetition with an explicit exit such as LEAVE in SQL/PSM-style routines.

-- Illustrative SQL/PSM-style routine body
BEGIN ATOMIC
  DECLARE i INTEGER DEFAULT 1;
  DECLARE total INTEGER DEFAULT 0;
  WHILE i <= 5 DO
    SET total = total + i;
    SET i = i + 1;
  END WHILE;
END

The successive totals are 1, 3, 6, 10 and 15. The loop terminates when i becomes 6. This illustrates procedural syntax rather than a standalone portable SQL query; routine declaration and output syntax depend on the DBMS. Set-based SQL is generally preferable when the same task can be expressed directly as a relational operation.

Further reading

Silberschatz, Korth and Sudarshan, Database System Concepts; Elmasri and Navathe, Fundamentals of Database Systems.


May 2024 — Q8(b)(ii)

Question (restated): Write a short note on branching and looping constructs in ANSI SQL.

Answer

SQL CASE is a standard conditional expression usable in queries. Procedural SQL/PSM provides control structures such as IF, CASE statements, WHILE, REPEAT and LOOP for stored routines. IF chooses a branch according to a condition; WHILE tests before each iteration; REPEAT tests after executing its body; LOOP repeats until an explicit exit. LEAVE exits a labeled block or loop, and ITERATE continues a labeled loop in SQL/PSM-style syntax. Vendor implementations differ: Oracle PL/SQL, PostgreSQL PL/pgSQL and MySQL stored programs have different syntax. Use set-based SQL where possible rather than looping through rows. A CASE expression can classify marks into Pass and Fail; a procedural loop can repeat a bounded administrative operation. Ensure termination and handle NULL conditions explicitly.

SELECT Name, CASE WHEN Marks IS NULL THEN 'Pending' WHEN Marks >= 40 THEN 'Pass' ELSE 'Fail' END AS Result FROM Student;

SQL/PSM-style routine body (dialect support varies):

BEGIN ATOMIC
 DECLARE i INTEGER DEFAULT 0;
 WHILE i < 5 DO
  SET i = i + 1;
 END WHILE;
END
Condition|True: execute branch|False: alternate branch|Loop until termination
SQL branching and looping — schematic illustration

November 2019 – Question 8(c)

Question (restated): Write a short note on branching and looping constructs in ANSI SQL.

Answer

SQL/PSM provides procedural control structures including IF, CASE, WHILE, REPEAT and LOOP. Branching selects statements based on conditions; looping repeats statements until a condition changes. For example, inside a SQL/PSM routine with declared integer i:

SET i = 1;
WHILE i <= 5 DO
 IF i = 3 THEN
  SET i = i + 2;
 ELSE
  SET i = i + 1;
 END IF;
END WHILE;

Actual routine syntax differs across Oracle PL/SQL, PostgreSQL PL/pgSQL and MySQL. SQL CASE expressions are also useful in ordinary SELECT queries, but are distinct from procedural CASE statements.

← RGPV CS-502 DBMS December 2024 solved question paper

← RGPV CS-502 DBMS May 2024 solved question paper

← RGPV CS-502 DBMS November 2019 solved question paper

Discussion

Ask a question, share your thoughts or report an error.

Sign in to participate in the discussion.

Join the discussion