"page" : "Total Drama Presents: The Ridonculous Race"
}, {
"count" : 18,
"page" : "User talk:Dudeperson176123"
}, {
"count" : 18,
"page" : "Wikipédia:Le Bistro/12 septembre 2015"
}, {
"count" : 17,
"page" : "Wikipedia:In the news/Candidates"
}, {
"count" : 17,
"page" : "Wikipedia:Requests for page protection"
} ]
} ]
```
## Druid SQL queries
Druid also supports a dialect of SQL for querying. Let's run a SQL query that is equivalent to the native JSON query shown above:
```
SELECT page, COUNT(*) AS Edits FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY page ORDER BY Edits DESC LIMIT 10;
```
The SQL queries are submitted as JSON over HTTP.
### TopN query example
The tutorial package includes an example file that contains the SQL query shown above at `quickstart/wikipedia-top-pages-sql.json`. Let's submit that query to the Druid broker:
dsql> SELECT page, COUNT(*) AS Edits FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY page ORDER BY Edits DESC LIMIT 10;
`SELECT FLOOR(__time to HOUR) AS HourTime, SUM(deleted) AS LinesDeleted FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY FLOOR(__time to HOUR);`
dsql> SELECT FLOOR(__time to HOUR) AS HourTime, SUM(deleted) AS LinesDeleted FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY FLOOR(__time to HOUR);
┌──────────────────────────┬──────────────┐
│ HourTime │ LinesDeleted │
├──────────────────────────┼──────────────┤
│ 2015-09-12T00:00:00.000Z │ 1761 │
│ 2015-09-12T01:00:00.000Z │ 16208 │
│ 2015-09-12T02:00:00.000Z │ 14543 │
│ 2015-09-12T03:00:00.000Z │ 13101 │
│ 2015-09-12T04:00:00.000Z │ 12040 │
│ 2015-09-12T05:00:00.000Z │ 6399 │
│ 2015-09-12T06:00:00.000Z │ 9036 │
│ 2015-09-12T07:00:00.000Z │ 11409 │
│ 2015-09-12T08:00:00.000Z │ 11616 │
│ 2015-09-12T09:00:00.000Z │ 17509 │
│ 2015-09-12T10:00:00.000Z │ 19406 │
│ 2015-09-12T11:00:00.000Z │ 16284 │
│ 2015-09-12T12:00:00.000Z │ 18672 │
│ 2015-09-12T13:00:00.000Z │ 30520 │
│ 2015-09-12T14:00:00.000Z │ 18025 │
│ 2015-09-12T15:00:00.000Z │ 26399 │
│ 2015-09-12T16:00:00.000Z │ 24759 │
│ 2015-09-12T17:00:00.000Z │ 19634 │
│ 2015-09-12T18:00:00.000Z │ 17345 │
│ 2015-09-12T19:00:00.000Z │ 19305 │
│ 2015-09-12T20:00:00.000Z │ 22265 │
│ 2015-09-12T21:00:00.000Z │ 16394 │
│ 2015-09-12T22:00:00.000Z │ 16379 │
│ 2015-09-12T23:00:00.000Z │ 15289 │
└──────────────────────────┴──────────────┘
Retrieved 24 rows in 0.08s.
```
#### GroupBy
`SELECT channel, SUM(added) FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY channel ORDER BY SUM(added) DESC LIMIT 5;`
dsql> SELECT channel, SUM(added) FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY channel ORDER BY SUM(added) DESC LIMIT 5;
┌───────────────┬─────────┐
│ channel │ EXPR$1 │
├───────────────┼─────────┤
│ #en.wikipedia │ 3045299 │
│ #it.wikipedia │ 711011 │
│ #fr.wikipedia │ 642555 │
│ #ru.wikipedia │ 640698 │
│ #es.wikipedia │ 634670 │
└───────────────┴─────────┘
Retrieved 5 rows in 0.05s.
```
#### Scan
` SELECT user, page FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 02:00:00' AND TIMESTAMP '2015-09-12 03:00:00' LIMIT 5;`
By prepending `EXPLAIN PLAN FOR ` to a Druid SQL query, it is possible to see what native Druid queries a SQL query will plan into.
Using the TopN query above as an example:
`EXPLAIN PLAN FOR SELECT page, COUNT(*) AS Edits FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY page ORDER BY Edits DESC LIMIT 10;`
dsql> EXPLAIN PLAN FOR SELECT page, COUNT(*) AS Edits FROM wikipedia WHERE "__time" BETWEEN TIMESTAMP '2015-09-12 00:00:00' AND TIMESTAMP '2015-09-13 00:00:00' GROUP BY page ORDER BY Edits DESC LIMIT 10;