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

Primary Key VS Unique Key In Database.
PRIMARY KEY UNIQUE KEY Primary key is a set of one or more fields/columns of a table that uniquely identify a record in database table. Primary keys must contain unique values. Unique-key...

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


SQL
Structured query language-It is a language to interact with the database. Database-It is a collection of data's in the form of tables. Tables-Collection of rows and columns Rows are also called as tuples...

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