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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:15:52+00:00 2026-05-29T11:15:52+00:00

I have a SQLIte database with tables for places and tables for tags, CREATE

  • 0

I have a SQLIte database with tables for places and tables for tags,

CREATE TABLE places (
    id INTEGER NOT NULL, 
    name VARCHAR(256), 
    address VARCHAR(256),
    PRIMARY KEY (id)
)

CREATE TABLE tags (
    id INTEGER NOT NULL, 
    name VARCHAR(256) NOT NULL, 
    PRIMARY KEY (id)
)

I also have a third table to map the associations between the first two,

CREATE TABLE placestags (
    id INTEGER NOT NULL, 
    placeid INTEGER, 
    tagid INTEGER, 
    PRIMARY KEY (id), 
    FOREIGN KEY(placeid) REFERENCES places (id), 
    FOREIGN KEY(tagid) REFERENCES tags (id)
)

I can grab which places are tagged with which tags with the following command,

SELECT places.id, places.name, tags.id, tags.name
FROM (placestags INNER JOIN places on placestags.placeid = places.id
           INNER JOIN tags on tags.id = placestags.tagid)
WHERE placestags.tagid=tags.id

Which will return something like this,

id | name          | id | name
---------------------------------          
1  | McDonalds     | 1  | Burgers
2  | Pizza Hut     | 2  | Pizza
3  | Burger King   | 1  | Burgers

I am wondering if it is possible to construct a query such that it returns the names of places which share two or more tags. For example, if McDonalds and Burger King shared both the tags Burgers and Fries, then I’d like to list that out like this,

p1id | p1name      | p2id | p2name      | t1id | t1name        | t2id | t2name
--------------------------------------------------------------------------------          
1    | McDonalds   | 3    | Burger King | 1    | Burgers       | 3    | Fries

Is this possible with SQL?

  • 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-29T11:15:53+00:00Added an answer on May 29, 2026 at 11:15 am

    You can use this query to produce results below:

    select p1.name, p2.name, t.name
    from places p1
    join placestags pt1 on p1.id=pt1.placeid
    join placestags pt2 on pt1.tagid=pt2.tagid and pt2.placeid <> p1.id
    join places p2 on pt2.placeid=p2.id
    join tags t on t.id=pt1.tagid
    order by p1.id, t.id
    

    This does not get everything into a single row like you wanted (you’d need a pivot for that, and I don’t think sqlite has it), but it lets you see what is going on. Here is what you’d get from this query:

    Place1      |   Place2       | Shared_Tag
    ------------|----------------|-----------
    McDonalds       Burger King     Burgers
    McDonalds       Burger King     Fries
    Burger King     McDonalds       Burgers
    Burger King     McDonalds       Fries
    

    EDIT (in response to a comment):

    If you are looking to shorten the query time, try reducing the number of joins, and remove the symmetric duplicates, like this:

    select pt1.placeid, pt2.placeid, pt1.tagid
    from placestags pt1
    join placestags pt2 on pt1.tagid=pt2.tagid and pt2.placeid > pt1.placeid
    order by pt1.placeid, pt1.tagid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQLITE database with two tables. Table A has an integer timestamp
I have an SQLite database. I created the tables and filled them with a
I have a DataTable which I want to save to a SQLite Database Table.
I have the following table in a SQLite3 database: CREATE TABLE overlap_results ( neighbors_of_annotation
I am trying to create entities out of a SQLite database. SQLite doesnt have
I have a SQLite database, and several tables within that datbase. I am developing
So what's the best way to create new tables in a Sqlite database in
I created a SQLite database via SQLiteManager. I have a table called Envelopes, and
I have an SQLite database with 4 tables in it that I want to
C# (.NET 3.5) I have an SQLite database with two tables - employees (the

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.