SQL JOIN Query for Retrieving Pics in Multiple Categories

  • Thread starter Thread starter DaveC426913
  • Start date Start date
  • Tags Tags
    Sql
Click For Summary

Discussion Overview

The discussion revolves around constructing SQL queries to retrieve pictures from a database based on their associated categories. Participants explore different methods to achieve this, focusing on the use of subqueries and JOIN operations, while clarifying the structure of the involved tables.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes the structure of three tables: Pics, Categories, and PicsCategories, and expresses the need to retrieve pictures based on category parameters.
  • Another participant suggests using a subquery to select distinct pictures from the Pics table based on category conditions.
  • A similar subquery approach is reiterated by multiple participants, emphasizing the use of the IN clause to filter results.
  • One participant proposes an alternative method using a JOIN syntax, questioning the meaning of aliases used in the query.
  • A later reply clarifies the use of aliases in SQL queries, explaining their purpose and benefits, particularly in cases where the same table is referenced multiple times.

Areas of Agreement / Disagreement

Participants generally agree on the use of subqueries and JOINs to achieve the desired results, but there is some discussion regarding the clarity and preference for using aliases in SQL syntax. No consensus on a single best approach is reached.

Contextual Notes

Participants do not resolve the nuances of SQL syntax or the implications of using different query structures. There are also no explicit discussions on performance considerations or the impact of database design on query efficiency.

Who May Find This Useful

Database developers, SQL learners, and those interested in optimizing SQL queries for data retrieval in relational databases may find this discussion relevant.

DaveC426913
Gold Member
2025 Award
Messages
24,473
Reaction score
8,736
I've got 3 tables:
Pics - table of my pictures
Categories - table of categories that pictures will fall into
PicsCategories - a many-to-many table relating pictures to categories. So the primary key of this table consists of a pic field and a category field

So now I want to provide a couple of categories as params and have it return all rows from the Pics table that are in those categories.

Code:
Table Pics
id Name
1 Bob
2 Mary
3 Jill
4 Jane

Table Categories
id Name
1 Male
2 Female
3 Parent

Table PicsCategories
Pic Cat
1   1
1   3
2   2
3   2
3   3
4   2
4   3
So if I supply Category1=2 OR Category2=3 (females and/or parents)
It should return Pic rows:
2
3
4
I'm looking at JOIN but JOIN is meant to extract data from two tables. I do not need ANY data from the PicsCategories table.

This is what I want:
SELECT UNIQUE(*) FROM Pics WHERE id=(SELECT Pic FROM PicsCategories WHERE Categories=2 OR Categories=3)
 
Last edited:
Technology news on Phys.org
Ah. It's a subquery.

Code:
SELECT DISTINCT * 
FROM Pics
WHERE id
IN (
SELECT Pic
FROM tblPicsCategories
WHERE Category=2
OR Category=3
)
Thanks for all your help! Couldn't have done it without you!
 
DaveC426913 said:
Ah. It's a subquery.

Code:
SELECT DISTINCT * 
FROM Pics
WHERE id
IN (
SELECT Pic
FROM tblPicsCategories
WHERE Category=2
OR Category=3
)
Thanks for all your help! Couldn't have done it without you!
I think this works as well:

select distinct p.* from pics p, tblPicsCategories pc where p.ID = pc.pic and (pc.Category = 2 or pc.Category = 3)
 
DavidSnider said:
I think this works as well:

select distinct p.* from pics p, tblPicsCategories pc where p.ID = pc.pic and (pc.Category = 2 or pc.Category = 3)
What are the p and pc?

I mean, I get that p means pic and pc means piccategories but...

select distinct tblPic.* from pics tblPic, tblPicsCategories tblPicsCategories where tblPic.ID = tblPicsCategories.pic and (tblPicsCategories.Category = 2 or tblPicsCategories.Category = 3)
 
p and pc are called aliases in this case. Sometimes written with an 'as' i.e.

...from pics as p, ...

I prefer the 'as' because I think it's clearer. There are two reasons for using them

1. It's quicker
2. You need it if the same table appears twice as a join

i.e.

select a.name, b.name
from mytrees as a, mytrees as b
where a.parent_id=b.id

Where parent_id contains the id of the row that contains its parent. You need an alias just to refer to the individual tables.
 

Similar threads

  • · Replies 51 ·
2
Replies
51
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
1
Views
4K