Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1108791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:08:15+00:00 2026-05-17T02:08:15+00:00

So I have a table that’s basically a tree. Each row has a parent

  • 0

So I have a table that’s basically a tree. Each row has a parent in the same table, and a lvl column that tells me the level in the tree, the root being lvl = 0. Now say I want to select all the rows that have a name starting with A, but only those that are on the highest level in this group. So the tree might have 9 levels, but the highest level containing a row with name starting with A might be on level 7, so I want all rows on level 7 starting with A (ignoring those on lower levels). I could do something like this:

select id, name, lvl
  from my_table
 where name like 'A%'
   and lvl = (select max(lvl) from my_table
               where name like 'A%')

The problem with this is that I want to do something more complex than getting the rows starting with A, so the condition would be something with a bit more clauses, and the select and subselect will have a few joins as well, and I don’t like to repeat myself. Especially since I may need to modify this in the future, and I’m afraid I may forget some clause in one of the selects.

So is there an easier way of doing this, without having to repeat the select, to get the maximum level.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:08:15+00:00Added an answer on May 17, 2026 at 2:08 am

    Analytics might help you here:

    SELECT ID, NAME, rn
      FROM (SELECT id, NAME, rank() over(ORDER BY LVL DESC) rn 
              FROM my_table 
             WHERE NAME LIKE 'A%')
     WHERE rn = 1
    

    As an added benefit, in most cases the removal of the self-join will be a performance improvement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table that shows some records from database. each table has a column
I have a table that currently has two columns per row and I need
I have a table that has one column named Siniflar in my database. I
I have a table that has one column. How do I write a T-SQL
I have table that has a column of type DateTime, I would like to
I have a table that has account numbers in (account_num) and user profiles (profile_id).
I have a table that stores data that has been entered regarding the amount
I have a table that looks like this: ID |Name |Parent 1 |A |NULL
I have a table that has a link field that I would like to
I have a table that inserts a row when a certain function is used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.