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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:39:20+00:00 2026-05-14T07:39:20+00:00

I have a database with three tables: user_table country_table city_table I want to write

  • 0

I have a database with three tables:

  • user_table
  • country_table
  • city_table

I want to write ANSI SQL which will allow me to fetch all the user data (i.e. user details including the name of the country of the last school and the name of the city they live in now).

The problem I am having is that I have to use a self join, and I am getting slightly confused.

The schema is shown below:

CREATE TABLE user_table (id int, first_name varchar(16), last_school_country_id int, city_id int);

CREATE TABLE country_table (id int, name varchar(32));

CREATE TABLE city_table (id int, country_id int, name varchar(32));

This is the query I have come up with so far, but the results are wrong, and sometimes, the db engine (mySQL), asks me if I want to show all [HUGE NUMBER HERE] results – which makes me suspect that I am unintentionally creating a cartesian product somewhere.

Can someone explain what is wrong with this SQL statement, and what I need to do to fix it?

SELECT usr.id AS id, usr.first_name, ctry1.name as loc_country_name, ctry2.name as school_country_name, city.name as loc_city_name
                FROM user_table usr, country_table ctry1, country_table ctry2, city_table city
                WHERE usr.last_school_country_id=ctry2.id
                      AND usr.city_id=city.id
                      AND city.country_id=ctry1.id
                      AND ctry1.id=ctry2.id;
  • 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-14T07:39:20+00:00Added an answer on May 14, 2026 at 7:39 am

    Try this. I wrote it using ANSI syntax for clarity. I assume that you may not always have usr.city_id or usr.last_school_country_id, so I used a left outer join meaning you will always get usr records back regardless.

    I also removed and ctry1.id=ctry2.id, because that would require the user’s current city to be in the same country as their last_school_country_id, which I don’t think is always the case.

    SELECT usr.id AS id, usr.first_name, ctry1.name as loc_country_name, ctry2.name as school_country_name, city.name as loc_city_name 
    FROM user_table usr
    left outer join city_table city on usr.city_id=city.id 
    left outer join country_table ctry1 on city.country_id=ctry1.id 
    left outer join country_table ctry2 on usr.last_school_country_id=ctry2.id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.