So I have a Databases exam in the coming weeks, and as usual I decided to go over the past papers to see how the question might be. Everything went along fine until I came across this question.
Write a SQL construct for the following query: Which forests are found
in the same geographical region (fo_loc) and are owned by the same
company. The output should have the following structure: Larger Forest
name, Smaller Forest Name, and Company Name. Try to avoid printing the
same forest names pairs more than once.
This is the schema relevant to the question:
TABLE: FOREST
Fo_name character(10) P.K.
Fo_Size Integer
Fo_loc Character(10)
Fo_Comp Character(10))
With my limited knowledge of SQL I do not think this question can be solved declaratively, so I tried also PL/SQL (or rather plpgsql) and I was getting closer and closer to the solution, but it soon dawned on me that I was using many commands we did not cover in class not to mention the solution was getting far from what one would expect out of a 3 mark question.
And so I decided to write here… what is it that I’m missing? Any help would be greatly appreciated.
Thanks
Just a self join should work: