UrbanPro
true

Learn SQL Programming from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Looping In Oracle SQL Using Level

Arun
23/08/2017 0 0

We will see about the syntax LEVEL in Oracle sql. If we want to loop through and print a set of values in SQL, LEVEL could come handy.

Example query:

SELECT 'hi', LEVEL FROM DUAL
CONNECT BY LEVEL <= 5;

output:

hi 1
hi 2
hi 3
hi 4
hi 5

CONNECT BY LEVEL syntax does the trick here of incrementing the levels for looping till the value 5 is reached. So for above example, the SELECT columns will be fired 5 times as you can see in the output. The keyword LEVEL can be used in SELECT statement too, to print the current level, as you can see in the output.

There are so many interesting analytical applications of LEVEL in DUAL

SELECT SUBSTR('bala',1,LEVEL) LETTER FROM DUAL
CONNECT BY LEVEL <= LENGTH('bala');

output:

b
ba
bal
bala

SELECT SUBSTR('bala',LEVEL,1) LETTER FROM DUAL
CONNECT BY LEVEL <= LENGTH('bala');

output:

b
a
l
a

SELECT RPAD(SUBSTR('Bala',LEVEL,1), LENGTH('Bala') + 2 - level, '*')
FROM DUAL
CONNECT BY LEVEL <= LENGTH('Bala');

output

B****
a***
l**
a*

The requirement for below example is - convert Case (flipflop - upper to lower and viceversa) for given input. It can be achieved using different ways in Oracle. I have tried with LEVEL and LISTAGG. LISTAGG aggregates data. Please check for it online. I will add a post on LISTAGG shortly.

SELECT LISTAGG(changed,'') WITHIN GROUP (ORDER BY lvl) finally_changed
FROM
(
    SELECT lvl, CASE WHEN letter = UPPER(letter) THEN LOWER(letter)
    ELSE UPPER(letter) END changed
    FROM
    (
         SELECT LEVEL lvl,SUBSTR('bAlA',LEVEL,1) LETTER FROM DUAL
         CONNECT BY LEVEL <= LENGTH('bAlA')
    )
);

The query structure comprises of subqueries, please understand and run the queries starting from the inner most query.

The inner most query - query 3 is same as we have seen earlier - it splits given string into rows of letters.

The second query - query 2 has the main logic to convert Upper case to lower and vice versa using CASE.

The First main query does the regrouping process using LISTAGG function

Input: bAlA

output: BaLa

This is a basic post about LEVEL. Please add on or correct any mistakes. Welcome your suggestions. Thanks for your time.

0 Dislike
Follow 2

Please Enter a comment

Submit

Other Lessons for You


SQL Union Vs Union ALL
SQL UNION vs UNION ALL Operator Usage UNION: UNION combines the result set of two or more queries into a single result set. UNION removes duplicate records (where all columns in the results are the same). UNION...

Truncate, Drop And Delete Commands In SQL
Truncate: Truncate command will delete the Table data only, it keeps the table schema as it is. Table data Can be rollback Example : TRUNCATE TABLE User; Drop: Drop command will delete both...

Derived Tables
Following are the signs those qualify a query to be a derived table: These are defined in FROM clause of an outer query. It is surrounded by parenthesis followed by AS clause to define the derived...

SQL Tips (1 to 3)
SQL tip 1: Instead of Full Outer Join, you can use Union All. Difference is, Full Outer Join provides results side by side. Union All provides result s one by one. Note: MYSQL does not support Full...
X

Looking for SQL Programming Classes?

The best tutors for SQL Programming Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn SQL Programming with the Best Tutors

The best Tutors for SQL Programming Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more