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 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'm new to SQLite3 with iOS I'm trying to create an SQL Query that
I am totally new to SQL . I have a simple select query similar
Im very new to SQL but need to write a query to do the
I'm new to SQL and relational databases and I have what I would imagine
So I'm getting a new job working with databases (Microsoft SQL Server to be
I am new to SQL Server, and I have been tasked with setting permissions
Hi I'm very new to sql but have been passed a job in which
I have a query in Data Access Object DAOComments that joins users table and
I have a new web app that is packaged as a WAR as part
I have an array with 30000 plus entries that need to go into a

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.