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 6635071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:00:20+00:00 2026-05-25T23:00:20+00:00

I am trying to fabricate an SQL query that will provide these results: |

  • 0

I am trying to fabricate an SQL query that will provide these results:

  | Category Title | Subcategory Of |
  -----------------------------------
  |   Category 1   |                |
  |   Category 2   |                |
  |   Category 3   |                |
  |   Category 4   |                |
  |   Category 5   |                |
  |   Category 6   |   Category 4   |
  |   Category 7   |   Category 5   |

This is what my database looks like:

CREATE TABLE `categories` (
  `category_id` int(4) NOT NULL AUTO_INCREMENT,
  `subcategory_id` int(4) NOT NULL,
  `category_title` longtext COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`category_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

INSERT INTO `categories` (`category_id`, `subcategory_id`, `category_title`) VALUES
(1, 0, 'Category 1'),
(2, 0, 'Category 2'),
(3, 0, 'Category 3'),
(4, 0, 'Category 4'),
(5, 0, 'Category 5'),
(6, 4, 'Category 6'),
(7, 5, 'Category 7');

I thought that you would use JOIN, but I wasn’t able to mentally think of what kind of query to run, since as far as I knew JOIN was for joining two tables, not two columns. I’m new to these advanced queries (I’m good with INSERT, UPDATE, DELETE, etc. though). Any help is appreciated.

This is what I was trying, which makes no sense really.

SELECT * FROM categories RIGHT JOIN categories ON subcategory_id = category_id
  • 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-05-25T23:00:21+00:00Added an answer on May 25, 2026 at 11:00 pm

    It’s called a self-join. You incldue the table name twice in the query, but giving it two different aliases and then it’s just like a normal join:

    SELECT
        C1.category_title AS category_title,
        C2.category_title AS subcategory_of
    FROM categories C1
    LEFT JOIN categories C2
    ON C1.subcategory_id = C2.category_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to find some information on this but am unable to get any results
Trying to get this query to work. I'm having issues with brackets I think.
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to match this data, that is contained in one string: vsan 1 interfaces:
trying to get a regex that will match a url e.g. 'http://www.test.com' and then
Trying to display a message if no results found in the search query. Here
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to do this, but is it wrong? Below is my style and script
Trying to write this small program to help me in my Stats class, everything

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.