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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:54:02+00:00 2026-05-24T16:54:02+00:00

I am new to SQL and databases.. I need to create an SQL query

  • 0

I am new to SQL and databases.. I need to create an SQL query to find

all posts that have tags with tagname='t1'

relation between posts and tags is many-to-many

So, I created the schema and some insert statements as below:

create table Posts(
p_id int ,
p_name varchar 
);

create table Tags(
t_id int ,
t_name varchar 
);

Since many-to-many relation needs a join table, I created that as well

create table Posts_Tags(
p_id int,
t_id int 
);

insert into Posts values('1','P1' );
insert into Posts values('2','P2' );
insert into Posts values('3','P3' );

insert into Tags values ('1','t1');
insert into Tags values ('2','t2');
insert into Tags values ('3','t3');
insert into Tags values ('4','t4');
insert into Tags values ('5','t5');

insert into Posts_Tags values('1','1' );
insert into Posts_Tags values('1','2' );
insert into Posts_Tags values('2','1' );
insert into Posts_Tags values('2','3' );
insert into Posts_Tags values('3','5' );

Now, how should I make the SQL query to get all posts that have tags with t_name='t1'? Is it possible to query only the two tables Posts and Tags and get the correct result? Or should I use Posts_Tags table also?

Forgive me but I have little knowledge of rdbms and 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-24T16:54:04+00:00Added an answer on May 24, 2026 at 4:54 pm

    No – you need the three tables:

    select p.*
    from Posts p
    join Posts_Tags pt on pt.p_id = p.p_id
    join Tags t on t.t_id = pt.t_id
    where t.t_name = 't1';
    

    This table layout is the correct way to create a mant-to-many relationship between Posts and Tags. Posts_Tags is called an associative table.

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

Sidebar

Related Questions

I have related data living on two completely separate databases, and I need information
I'm creating a series of webservices for my application and i have the need
I am using LINQ-to-SQL. I have retrieved an Entity called FileType using FileTypeID from
I have a table like: | id | lastname | firstname | | 1
Imagine I have something like this: def example = { def temp = ConferenceUser.findAllByUser(User.get(session.user))
I've been developing in PHP for a while now but only recently made the
I've been learnging php/mysql for a short while and I got some basic stuff
I'm having a problem with copying or scripting my database at a web host
I am a newbie to Android app development. For 1 of my project, I
This is an easy question but I am not sure the correct jargon to

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.