Wikidata talk:SPARQL tutorial

From Wikidata
Jump to navigation Jump to search

To Wikibooks?

[edit]

Would you consider moving the contents of the page to Wikibook in order to create a book about WDQS? --★ → Airon 90 17:00, 28 December 2016 (UTC)[reply]

Is it easier to translate at Wikidata or at Wikibooks? d1g (talk) 19:52, 27 January 2017 (UTC)[reply]

Name suggestions

[edit]

 Resolved renamed; please update Wikidata:SPARQL query service/Wikidata Query Help if name will be changed again

How about "Expanding SPARQL queries" or "Exploring with SPARQL"?

Title should indicate that this material would help to write more complex queries and not about list of examples or materials from "first steps" at Wikidata:SPARQL_query_service/Wikidata_Query_Help d1g (talk) 19:59, 27 January 2017 (UTC)[reply]

Translation

[edit]

It's possible prepare the page for translation? --ValterVB (talk) 19:12, 9 February 2017 (UTC)[reply]

This page is still heavily edited. I would prefer to active translations only if the page reached a stable version. Otherwise people spend their time to write translations and then when somebody is changing the English version the translations are lost. --Pasleim (talk) 10:11, 10 February 2017 (UTC)[reply]
OK --ValterVB (talk) 21:50, 10 February 2017 (UTC)[reply]
Sections 7 and 8 need some review: some advices are quite trivial e.g. list of types (a copy-paste of SPARQL spec is not what we want here)
I would like to invite other SPARQL users to review SPARQL-related advices given here. d1g (talk) 12:08, 12 March 2017 (UTC)[reply]
@ValterVB, Pasleim, D1gggg: this tutorial is now translatable. Pamputt (talk) 15:40, 28 January 2018 (UTC)[reply]

errors in Property Paths section

[edit]

All the queries about "grandson" in section Property Paths are wrong, since they ask wdt:P21 wd:Q6581097 of the wrong node. Eg the first one checks the gender of ?child, not of ?gs. --Vladimir Alexiev (talk) 11:44, 17 May 2017 (UTC)[reply]

Thanks for reporting!
Can we use brackets here somehow? [wdt:P21 wd:Q6581072] d1g (talk) 13:10, 17 May 2017 (UTC)[reply]

advanced course?

[edit]

 SPARQL tutoriald1g (talk) 12:31, 31 July 2017 (UTC)[reply]

During the last six months this page was heavily extended with many explanations, links and references. IMHO this page should no longer be called "beginner-friendly course" but "advanced course". Beginners might be overhelmed by the depth of information and the amount of new terms, on the other hand, advanced user probably don't even open the page because the current title sounds too boring to them. --Pasleim (talk) 12:26, 10 July 2017 (UTC)[reply]

True, many things are explained in one page
I think that 2/3 of data model is explained now if readers are patient enough during introduction.
I don't know how to name it in order to get it along "SPARQL" results, current title is not searchable.
Some trivial statements could be merged into appendix(es), but I don't have ideas how to select trivial statements. d1g (talk) 20:06, 18 July 2017 (UTC)[reply]
ideas for searchable titles: "SPARQL advanced course", "SPARQL course", "SPARQL tutorial". --Pasleim (talk) 09:13, 21 July 2017 (UTC)[reply]
@Pasleim: "SPARQL tutorial" is used 50 times more than other two, so I will rename to this for now. d1g (talk) 13:00, 27 July 2017 (UTC)[reply]

@D1gggg: Sadly I have to agree with Pasleim. Compared to eg the version of 25 December 2016, with its previous outstanding gentle tutorial style that was perfect for intermediate learners, in my view the present page is now unreadable -- even for someone like myself who has written a lot of WDQS queries.

In my view, it would be sensible to restore an previous version of the earlier outstanding tutorial -- and to comprehensively re-write the present page as an easy-to-follow advanced quick reference with examples. Jheald (talk) 04:41, 19 August 2017 (UTC)[reply]

It has so many inaccuracies and omissions, that I'm not going make any edits. d1g (talk) 10:18, 19 August 2017 (UTC)[reply]

Only Queries lieke "book has author(x)" but nowhere "author(x) wrote books"

[edit]

Why is every tutorial limited to explain the default syntax. Is there any example that explains how to query only the values from one object. For example all population values for a given city? I asked before on the request a query page but my question and the damaged query has been deleted without an answer. Ogmios (Tratsch) 18:07, 6 December 2017 (UTC)[reply]

This tutorial just keeps getting more and more helpful.

[edit]

Thank you for creating this resource. I have been learning from it since you created the page. Every time I check back there are new sections and examples. I find your explanatory text to be very clear and readable. This page helps make the amazing WQS even more approachable for me. YULdigitalpreservation (talk) 15:07, 21 December 2017 (UTC)[reply]

Indeed. It would be great if it is expanded to describe sub-queries as well. Ijon (talk) 10:53, 11 January 2018 (UTC)[reply]

Ten most populous countries has duplicates

[edit]

The query for ten most populous countries currently returns certain countries multiple times (PR of China is rank one to three). Can you explain and modify the query so that it works?--37.201.181.45 12:36, 12 February 2018 (UTC)[reply]

The problem is that People's Republic of China (Q148) is instance of sovereign state (Q3624078), member states of the United Nations (Q160016) and Permanent members of the United Nations Security Council (Q6688521). The latter two are subclasses of the first one. Therefore, when querying for instances of sovereign state (Q3624078) or subclasses thereof, People's Republic of China (Q148) appears three times. The easiest fix is to use the keyword DISTINCT but that gets only introduced later in the tutorial. --Pasleim (talk) 13:39, 12 February 2018 (UTC)[reply]

VisualEditor broke content

[edit]

@Fuzheado: It looks like your edit to this page broke the markup in some places. I assume that’s VisualEditor messing up? There’s an edit section link in some headings, {{Q}} and {{P}} have been replaced by plain wikitext, and the translation markers are gone.

Unfortunately, I don’t really understand the diff output :/ did you actually change anything in the “Qualifiers” and “Painting materials” sections, or can we restore the old wikitext there completely? --TweetsFactsAndQueries (talk) 11:49, 12 September 2018 (UTC)[reply]

Yeah, ick. I'm happy to have it reverted and then re-apply the edits as needed. -- Fuzheado (talk) 14:36, 12 September 2018 (UTC)[reply]
Okay, I’ve reverted those parts (but kept the changes to the introduction, there weren’t any problems there). --TweetsFactsAndQueries (talk) 15:25, 12 September 2018 (UTC)[reply]

The Apollo example on bound and filter

[edit]

The « bound » trick is not necessary here,

SELECT distinct ?astronaut ?astronautLabel
WHERE
{
  ?astronaut wdt:P31 wd:Q5;
             wdt:P106 wd:Q11631.
  {
    ?astronaut wdt:P450/wdt:P31 wd:Q495307.
  } union {
    ?astronaut wdt:P450 wd:Q182252.
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
Try it!

or even

SELECT ?astronaut ?astronautLabel
WHERE
{
  ?astronaut wdt:P31 wd:Q5;
             wdt:P106 wd:Q11631 filter exists
  {
    {
      ?astronaut wdt:P450/wdt:P31 wd:Q495307.
    } union {
      ?astronaut wdt:P450 wd:Q182252.
    }
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
Try it!

It may be worth mentioning.

@TweetsFactsAndQueries:

author  TomT0m / talk page 20:10, 13 March 2019 (UTC)[reply]

@TomT0m: hm, yeah, that sure looks like I wasn’t aware of UNION at the time :) but the BIND/BOUND/IF section isn’t really the right place to introduce UNION, so I’m not sure how best to proceed here. Perhaps just remove the “astronauts” example completely, and mention UNION in the “and beyond…” section at the end? --TweetsFactsAndQueries (talk) 22:32, 13 March 2019 (UTC)[reply]

Group by and ordering

[edit]

« Since we’re ordering the results by country, all cities belonging to a country form one contiguous block in the results. …»

This seems to imply that to group by country you have to sort by country first. This is wrong. Is this really what you meant or is this just a trick to help the reader to learn the agregation concept ?

@TweetsFactsAndQueries: author  TomT0m / talk page 18:56, 14 March 2019 (UTC)[reply]

@TomT0m: it’s just meant to help understand the concept – first write the query with ORDER BY, then replace that with GROUP BY to aggregate all the adjacent results together. --TweetsFactsAndQueries (talk) 08:28, 15 March 2019 (UTC)[reply]

wd, wdt?

[edit]

What the wd and wdt stays for? Is it just for Wikidata quering or does it work elsewhere?--Juandev (talk) 15:36, 18 May 2019 (UTC)[reply]

Asterisk operator ("/wdt:P279*") for any number of subclasses running into a timeout

[edit]

We had some problem with queries that worked before (until about end of July 2019), but now are running into a timeout ("java.util.concurrent.TimeoutException").

See

We used the asterisk operator ("/wdt:P279*") for *any* number of instances (zero to unlimited/infinite) from the instance to the subclasses, like all the examples of the tutorial (which are not working anymore neither):

[...] The solution: ?item wdt:P31/wdt:P279* ?class. This means that there’s one “instance of” and then any number of “subclass of” statements between the item and the class.

?work wdt:P31/wdt:P279* wd:Q838948. # instance of any subclass of work of art

First I assumed, that the query runs into an endless loop, if A is subclass of B and B is subclass of A, but i could not confirm that by viewing the tree structure (here for the class "film"):


When I removed the "/wdt:P279*" in the query, I got all films, but not the subclasses (for example d:Q18517638 was missing, since it is a "animation film"/"3D film" as subclass of "film").

When I used "/wdt:P279+", I got only the subclasses of "film", but not the instances of "film" themselves.


From my point of view, a solution could be to combine those two by using "UNION"

  {
    ?film wdt:P31 wd:Q11424 .
  } 
    UNION 
  {
    ?film wdt:P31/wdt:P279+ wd:Q11424 .
  }

For example, to get all films released in 2019 in Germany the query would be:

SELECT DISTINCT ?film ?release ?filmLabel WHERE {
  { ?film wdt:P31 wd:Q11424. }
  UNION
  { ?film (wdt:P31/(wdt:P279+)) wd:Q11424. }
  ?film p:P577 ?release_statement.
  ?release_statement (psv:P577/wikibase:timePrecision) 11 ;
    ps:P577 ?release.
  FILTER((YEAR(?release)) = 2019 )
  ?release_statement pq:P291 wd:Q183.
  FILTER(NOT EXISTS { ?release_statement wikibase:rank wikibase:DeprecatedRank. })
  FILTER(NOT EXISTS { ?release_statement pq:P400 ?platform. })
  FILTER(NOT EXISTS { ?release_statement pq:P437 ?distribution. })
  FILTER(NOT EXISTS { ?film wdt:P31 wd:Q506240. })
  FILTER(NOT EXISTS { ?film wdt:P31 wd:Q21191270. })
  SERVICE wikibase:label { bd:serviceParam wikibase:language "de". }
}
ORDER BY (?release) (?filmLabel)
Try it!

--M2k~dewiki (talk) 00:30, 15 August 2019 (UTC)[reply]

Average number of pages example timing out

[edit]

This query times out - even removing the ORDER BY clause doesn't help. The version with the HAVING clause works okay. John Womble (talk) 12:32, 24 December 2019 (UTC)[reply]

Autocompletion on a Mac

[edit]

Dunno if these are default, but on my Mac none of these invokes autocompletion in the Query editor:

  • Ctrl-Space = change keyboard layout
  • Alt-Return = runs the query
  • Ctrl-Alt-Return = runs the query
  • Alt-Enter = runs the query
  • Ctrl-Alt-Enter = runs the query

I have a standard keyboard without a numeric keypad, with a Return but no Enter key, but I can use Fn-Return to type Enter. No idea if Fn-Alt-Return is the same as Alt-Enter.

Any idea how to make autocompletion work? —Michael Z. 03:11, 6 February 2021 (UTC)[reply]

@Mzajac: On my Linux, Ctrl+Space works for autocompletion, which is the standard autocompletion shortcut among code editors on Linux/Windows. Of course, if macOS intercepts this before hitting the browser, it won’t work. However, as far as I know, commands which use Ctrl on Windows and Linux usually use Command on macOS—does ⌘+Space work for you? If not, are you familiar with code editors on macOS that offer code completion? What keys do they use? —Tacsipacsi (talk) 21:19, 7 February 2021 (UTC)[reply]
Thanks. After some frustration, I discovered that pressing Fn-Option-Return (which sends “Alt-Enter”) works on my Mac keyboard.
On a Mac, Return and Enter are two different keys: the usual keyboard has Return, and a costlier extended keyboard also has Enter by the numeric keypad. Would be nice if this keystroke were user-configurable, or had an always-on option. —Michael Z. 23:43, 14 February 2021 (UTC)[reply]

Guns_by_manufacturer

[edit]

In the exercise Guns_by_manufacturer, where we have to sum the production number, I think that the order should be

ORDER BY DESC(?totalProduced)

instead of ORDER BY DESC(?produced) .

--Meskalam (talk) 17:01, 26 March 2021 (UTC)[reply]

Placeholder

[edit]

Hi, how can one enter a placeholder (any sign, like *)? --TadejM (talk) 14:47, 20 February 2023 (UTC)[reply]