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

  • SEARCH
  • Home
  • 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 8965805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:53:23+00:00 2026-06-15T16:53:23+00:00

I have a database table like following: column 1 | column 2 x |

  • 0

I have a database table like following:

column 1 | column 2

x | Y

y | z

z | x

So basically x->y->z->x.

Now I have to detect such entries in the DB. One solution could be take these entries and make a graph in memory and then detect the cycle with a cycle finding algorithm. I would rather do it with an SQL query if possible though.

  • 1 1 Answer
  • 0 Views
  • 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-06-15T16:53:24+00:00Added an answer on June 15, 2026 at 4:53 pm

    SQLite does not support recursive queries, so the best you can do is to detect cycles of fixed length using self-joins. You can’t detect cycles of arbitrary length.

    Here’s an example of a join that finds cycles of length 2, like the example you showed in your question.

    SELECT ...
    FROM t AS t0
    JOIN t AS t1 ON t0.column2 = t1.column1
    JOIN t AS t2 ON t1.column2 = t2.column1
    WHERE t2.column2 = t0.column1
    

    Re your comment:

    If you need to detect cycles of arbitrary length, all I can suggest is that you must store more than the direct links. You must also store the transitive closure, i.e. every path through your graphs. That’s the best way to make this search efficient. Of course it takes a lot more storage space to do that, but there’s your tradeoff. And it takes less extra space than you think.

    I’ve done a transitive closure design for trees, but not acyclic graphs. See my answer to What is the most efficient/elegant way to parse a flat table into a tree? or my presentation Models for Hierarchical Data with SQL.

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

Sidebar

Related Questions

I have a table mytable in ORACLE database having only one column Roll_no like
Say I have a database table like the following: FileID | FileName | FileSize
i have a table in sqlite database like following. id status ============= x 0
I have one large database table of request data, much like Apache request logs,
I have a database with a table which includes among others, one column of
Hi I have database table struture Like following table - ItemLocation ItemId varchar(10) ItemGeo
i have database table like this +-------+--------------+----------+ | id | ip | date |
that my problem: I have database table like that: id (AI) market_id 1 6
Let's say you have a database with a single table like... --------------------------------------------- | Name
I always forget how to do things like this. I have a database table

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.