Aan de slagGa gratis aan de slag

Counting numbers recursively

In this first exercise, you will start with a math function to count numbers recursively. It is the series from 1 to target and in this case your target value is 50.

This means the task is to count from 1 to 50 using a recursive query. The video showed you the pseudo-code version, and now it's your turn to write it in real code! You will have to define:

  1. The CTE with the definition of the initial and recursive query
  2. The appropriate termination condition for the recursion

Deze oefening maakt deel uit van de cursus

Hierarchical and Recursive Queries in SQL Server

Cursus bekijken

Oefeninstructies

  • Define the CTE with the name counting_numbers.
  • Initialize number in the initial query.
  • Add 1 to number each recursion step.
  • Limit the recursion step to 50 in the recursive query.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

-- Define the CTE
___ ___ ___ ( 
	SELECT 
  		-- Initialize number
  		___ AS number
  	UNION ALL 
  	SELECT 
  		-- Increment number by 1
  		___ 
  	FROM counting_numbers
	-- Set the termination condition
  	WHERE number < ___)

SELECT number
FROM counting_numbers;
Code bewerken en uitvoeren