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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:01:09+00:00 2026-06-12T15:01:09+00:00

I have a table schema as create table Location( id int primary key, city

  • 0

I have a table schema as

create table Location(
id int primary key,
city varchar(255),
state varchar(100),
country varchar(255)
);
create table Person( 
id int primary key,
name varchar(100)
);
create table Photographer(
id int primary key references Person(id) on update cascade on delete cascade,
livesIn int not null references Location(id) on update cascade on delete no action
);
create table Specialty(
photographer int references Photographer(id) on update cascade on delete cascade,
type enum('portrait','landscape','sport'),
primary key(photographer, type)
);
create table Photo(
id int primary key,
takenAt timestamp not null,
takenBy int references Photographer(id) on update cascade on delete no action,
photographedAt int references Location(id) on update cascade on delete no action
);
create table Appearance(
  shows int references Person(id) on update cascade on delete cascade,
isShownIn int references Photo(id) on update cascade on delete cascade,
primary key(shows, isShownIn)
);

I am stuck at two queries :

1) The photos such that the photo only shows photographers that live in the same location. List each photo once. That is, photos must have persons that are photographers, and they all need to live in the same place.

2) The locations that have the property that every photo in the location was taken by a photographer who is not shown in any photo in Massachusetts? For each location show only the city, and show each location only once.

My tries :
1)

SELECT ph.id, ph.takenAt, ph.takenBy, ph.photographedAt FROM 
(SELECT * FROM Photo p, Appearance ap WHERE p.id = ap.isShownIn 
HAVING ap.shows IN (SELECT person.id FROM Person,Photographer WHERE person.id
photographer.id)) ph
WHERE ph.photographedAt = (SELECT location.id FROM location WHERE location.id = 
(SELECT livesIn FROM Photographer WHERE id = ph.takenBy))

2)

select distinct city from location where location.id in (
select photographedAt from photo, (select * from appearance where appearance.shows in
(select photographer.id from photographer)) ph
where photo.id = ph.isShownIn )
and location.state <> 'Massachusetts'

Can anyone help in creating these queries ??

  • 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-12T15:01:10+00:00Added an answer on June 12, 2026 at 3:01 pm

    Your queries are both of the “list individual items that have properties X and Y, where X and Y are in different tables” variety.

    These types of questions are commonly solved using correlated sub-queries with EXISTS and NOT EXISTS.

    Using EXISTS takes care of the “show each item only once” part. Otherwise you would need to use grouping in conjunction with complex joins, and this can get messy very quickly.

    Question 1 requires:

    […] photos must have persons that are photographers, and they all need to live in the same place.

    Note that this definition doesn’t say “do not show photos if they contain other people, too”. If that’s what you really meant, it’s upon you to draw conclusions from the SQL below and to write better definitions next time. 😉

    SELECT
      *
    FROM
      Photo p
    WHERE
      EXISTS (
        -- ...that has at least one appearance of a photographer
        SELECT 
          1 
        FROM
          Appearance              a 
          INNER JOIN Photographer r ON r.id = a.shows
          INNER JOIN Location     l ON l.id = r.livesIn
        WHERE
          a.isShownIn = p.id
          -- AND l.id = <optional location filter would go here>
          AND NOT EXISTS (
            -- ...that does not have an appearance of a photographer from 
            --    some place else
            SELECT 
              1 
            FROM
              Appearance              a1 
              INNER JOIN Photographer r1 ON r1.id = a1.shows
              INNER JOIN Location     l1 ON l1.id = r1.livesIn
            WHERE
              a1.isShownIn = p.Id
              AND l1.id <> l.id
          )
      )
    

    The second question reads

    […] locations that have the property that every photo in the location was taken by a photographer who is not shown in any photo in Massachusetts? For each location show only the city, and show each location only once.

    The according SQL would look like:

    SELECT
      city
    FROM
      Location l
    WHERE
      NOT EXISTS (
        -- ...a photo at this location taken by a photographer who makes 
        --    an apperance on another photo which which was taken in Massachusetts
        SELECT
          1
        FROM
          Photo                    p
          INNER JOIN Photographer  r ON r.id = p.takenBy
          INNER JOIN Appearance    a ON a.shows = r.id
          INNER JOIN Photo        p1 ON p1.id = a.isShownIn
        WHERE
          p.photographedAt = l.Id
          AND p1.photographedAt = <the location id of Massachusetts>
      )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have the following table schema: create table SerialNo2( IncarnationID_UID counter primary key, Mark
I have following table schema - CREATE TABLE [dbo].[TEST_TABLE] ( [TEST_TABLE_ID] [int] IDENTITY(1,1) NOT
I have the following table schema: CREATE TABLE [Foo]( [id] [int] NOT NULL, [name]
I have an orders table with a schema like this. CREATE TABLE orders (
I have a schema that essentially looks like this: CREATE TABLE `data` ( `id`
I have a table schema similar to the following (simplified): CREATE TABLE Transactions (
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date
I've defined table with this schema : CREATE TABLE [dbo].[Codings] ( [Id] [int] IDENTITY(1,1)
I have a table with the following schema: CREATE TABLE IF NOT EXISTS `feeds`

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.