Khushboo Tiwari Khushboo Tiwari - 2 months ago 6
SQL Question

Datalog for query

I need some help with the following query:

With given relations:

hiker(HN, Name), mountain(MN, Height), climbed(HN, MN, Date).


Q.: Give safe Datalog rules(with negation if necessary) for the following queries:

(a) Find the names of hikers who climbed some mountains with
Height > 5000m
.

I wrote like this

hiker(Name): - moutain(MN, Height), climbed(HN, MN, Date), Height > 5000


But if I have to find names of hikers who climbed every mountain with Height > 5000m, I am having difficulty in understanding the difference between some and every in these two questions.

Please guide me.

Answer

The 'some' question is easy to express in language like Datalog and SQL because you only have to find one mountain per hiker. It doesn't matter whether the hiker has hiked one, two, or more mountains. Datalog rules are perfect for this.

result(Name) :-
  hiker(H, Name),
  climbed(H, M, _),
  mountain(M, Height), Height > 5000.

The 'every' question is harder to answers because you need to now confirm that the hiker has climbed every single mountain, not just one of them.

Assuming a closed world database, the 'every' question can be translated in a double negation though, which is easy to express. You need to search for hikers for whom there does not exist a mountain > 5000m that they have not climbed.

I'll help you a little bit with the first part of this query, and then you can probably find the answer.

// this is just for convenience
high_mountain(M) :-
  mountain(M, Height),
  Height > 5000.

// there exists a mountain hiker H has not climbed.
some_not_climbed(H) :-
  hiker(H, _),
  high_mountain(M),
  !climbed(H, M).

// for hiker H there is no mountain that he has not climbed.
result(H) :-
  hiker(H, _),
  ...
Comments