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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:58:24+00:00 2026-06-16T13:58:24+00:00

Possible Duplicate: How can an SQL query return data from multiple tables I have

  • 0

Possible Duplicate:
How can an SQL query return data from multiple tables

I have 3 table

Attribute

attr_id | attr_name 
1       | oval<
2       | white

Product

product_id|product_name 
1         | amazonite 
2         | agate

attr_detail

attr_detail_id | attr_id | product_id
1              | 1       | 1
2              | 2       | 1
3              | 1       | 2
4              | 2       | 2

Now I want product those are oval shape and white color. How to use join.

  • 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-16T13:58:25+00:00Added an answer on June 16, 2026 at 1:58 pm

    Try this:

    SELECT DISTINCT
      p.product_id, 
      p.product_name 
    FROM Product p 
    INNER JOIN attr_detail ad ON p.product_id = ad.product_Id
    INNER JOIN Attribute a    ON a.attr_id = ad.attr_id
    WHERE a.attr_id IN(1, 2);
    

    SQL Fiddle Demo

    This will give you:

    | PRODUCT_ID |           PRODUCT_NAME |
    ---------------------------------------
    |          1 |              amazonite |
    |          2 |                  agate |
    |          3 | Product has only white |
    

    Note that: This will give you the products names that have either the white or oval shapes. However, if you are looking for only those products that has both the two shapes, you have to modify your query like so:

    SELECT DISTINCT
      p.product_id, 
      p.product_name 
    FROM Product p 
    INNER JOIN
    (
      SELECT product_id
      FROM attr_detail
      WHERE attr_id IN(1, 2)
      GROUP BY product_id
      HAVING COUNT(DISTINCT attr_id) = 2
    ) ad ON p.product_id = ad.product_Id;
    

    Updated SQL Fiddle Demo

    This will give you:

    | PRODUCT_ID | PRODUCT_NAME |
    -----------------------------
    |          1 |    amazonite |
    |          2 |        agate |
    

    Please read more about JOINs. Here you can find a useful information about this:

    • Join (SQL)From Wikipedia.

    • Visual Representation of SQL Joins.

    • Another Visual Explanation of SQL Joins.

    • SQL Queries for Mere Mortals(R): A Hands-On Guide to Data Manipulation in SQL, a great book for SQL Basics.

    For what I did in the second query:

      SELECT product_id
      FROM attr_detail
      WHERE attr_id IN(1, 2)
      GROUP BY product_id
      HAVING COUNT(DISTINCT attr_id) = 2
    

    This is called Relational Division.

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

Sidebar

Related Questions

Possible Duplicate: How can an SQL query return data from multiple tables i want
Possible Duplicate: SQL query multiple tables I have these 2 tables: ASSOCIATION_TABLE: [id, key,
Possible Duplicate: sql query to sum the data I have following table structure TradeId
Possible Duplicate: Selecting data from two different servers in SQL Server How can I
Possible Duplicate: SQL left join vs multiple tables on FROM line? SELECT messages.message_title, messages.message_content,
Possible Duplicate: How to get table names from access? can we list all tables
Possible Duplicate: SQL Delete: can't specify target table for update in FROM clause I'm
Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: Can you define “literal” tables in SQL? Occasionally I find myself in

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.