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

Difference Between Stored Procedure And Function
Stored Procedures Functions Stored procedures may or may not return a value Function should return a value Stored procedure should be executed by ‘EXECUTE’...

Database Normalization
Database Normalization is the process of reducing duplication in database by decomposing the table1. Normal Form (1NF): Every attribute is atomic, Each cell to be single valued No multivalued attribute:...

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...

Top 10 SQL Concepts for Job Interview
1. SELECT and FROM: This is the “heart” of any SQL query - SELECT columns FROM table.2. WHERE: This acts as a filter and allows you to select only relevant rows based on conditions.3. GROUP...

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...
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