Skip to content
Advertisement

Select from two tables by unique Id [closed]

I have two tables calendar and team, to simplify : in calendar table i have Id, team_id1, team_id2 and date, and in team table i have Id, name and image. I have to select all columns of calendars and also team names and images associated, i used this query, it works but i think i execute more than 3 queries in one. Can you suggest me more effective please :

Thank you.

Advertisement

Answer

I really can´t see a reason to make use of subqueries to solve the related problem.

So that, the solution is a basic query with 2 JOINs on table “team”, as follow sql:

See that above I have replaced 4 subSelects for 2 joins clauses, it is more direct and even more efficient.

SubSelects are useful in many situations for select-fields (projection) and where-statament (selection), but not for this simple relation beetwen 2 tables that can be joined by its PK e FK fields.

note: in order to test the syntax of my sugested sql, I use follow DML in h2DB:

You can see discussion about JOIN VS SubQuery here: Join vs. sub-query

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement