blob: f1c730ca6b2a783ddf5aaf187f08d0c91955d8e2 [file] [log] [blame]
<!doctype html>
<html lang="en" dir="ltr" class="docs-wrapper docs-doc-page docs-version-current plugin-docs plugin-id-default docs-doc-id-tutorials/tutorial-sql-query-view">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v2.4.1">
<title data-rh="true">Get to know Query view | Apache® Druid</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://druid.apache.org/img/druid_nav.png"><meta data-rh="true" name="twitter:image" content="https://druid.apache.org/img/druid_nav.png"><meta data-rh="true" property="og:url" content="https://druid.apache.org/docs/27.0.0/tutorials/tutorial-sql-query-view"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="Get to know Query view | Apache® Druid"><meta data-rh="true" name="description" content="&lt;!--"><meta data-rh="true" property="og:description" content="&lt;!--"><link data-rh="true" rel="icon" href="/img/favicon.png"><link data-rh="true" rel="canonical" href="https://druid.apache.org/docs/27.0.0/tutorials/tutorial-sql-query-view"><link data-rh="true" rel="alternate" href="https://druid.apache.org/docs/27.0.0/tutorials/tutorial-sql-query-view" hreflang="en"><link data-rh="true" rel="alternate" href="https://druid.apache.org/docs/27.0.0/tutorials/tutorial-sql-query-view" hreflang="x-default"><link rel="preconnect" href="https://www.google-analytics.com">
<link rel="preconnect" href="https://www.googletagmanager.com">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-131010415-1"></script>
<script>function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","UA-131010415-1",{})</script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script><link rel="stylesheet" href="/assets/css/styles.f80751b3.css">
<link rel="preload" href="/assets/js/runtime~main.5371e784.js" as="script">
<link rel="preload" href="/assets/js/main.832012d1.js" as="script">
</head>
<body class="navigation-with-keyboard">
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
<div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><nav aria-label="Main" class="navbar navbar--fixed-top navbar--dark"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="/img/druid_nav.png" alt="Apache® Druid" class="themedImage_ToTc themedImage--light_HNdA"><img src="/img/druid_nav.png" alt="Apache® Druid" class="themedImage_ToTc themedImage--dark_i4oU"></div></a></div><div class="navbar__items navbar__items--right"><a class="navbar__item navbar__link" href="/technology">Technology</a><a class="navbar__item navbar__link" href="/use-cases">Use Cases</a><a class="navbar__item navbar__link" href="/druid-powered">Powered By</a><a class="navbar__item navbar__link" href="/docs/27.0.0/design/">Docs</a><a class="navbar__item navbar__link" href="/community/">Community</a><div class="navbar__item dropdown dropdown--hoverable dropdown--right"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">Apache®</a><ul class="dropdown__menu"><li><a href="https://www.apache.org/" target="_blank" rel="noopener noreferrer" class="dropdown__link">Foundation<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://apachecon.com/?ref=druid.apache.org" target="_blank" rel="noopener noreferrer" class="dropdown__link">Events<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://www.apache.org/licenses/" target="_blank" rel="noopener noreferrer" class="dropdown__link">License<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://www.apache.org/foundation/thanks.html" target="_blank" rel="noopener noreferrer" class="dropdown__link">Thanks<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://www.apache.org/security/" target="_blank" rel="noopener noreferrer" class="dropdown__link">Security<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://www.apache.org/foundation/sponsorship.html" target="_blank" rel="noopener noreferrer" class="dropdown__link">Sponsorship<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div><a class="navbar__item navbar__link" href="/downloads/">Download</a><div class="searchBox_ZlJk"><div class="navbar__search"><span aria-label="expand searchbar" role="button" class="search-icon" tabindex="0"></span><input type="search" id="search_input_react" placeholder="Loading..." aria-label="Search" class="navbar__search-input search-bar" disabled=""></div></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div id="__docusaurus_skipToContent_fallback" class="main-wrapper mainWrapper_z2l0 docsWrapper_BCFX"><button aria-label="Scroll back to top" class="clean-btn theme-back-to-top-button backToTopButton_sjWU" type="button"></button><div class="docPage__5DB"><aside class="theme-doc-sidebar-container docSidebarContainer_b6E3"><div class="sidebarViewport_Xe31"><div class="sidebar_njMd"><nav aria-label="Docs sidebar" class="menu thin-scrollbar menu_SIkG"><ul class="theme-doc-sidebar-menu menu__list"><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/design/">Getting started</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret menu__link--active" aria-expanded="true" href="/docs/27.0.0/tutorials/tutorial-msq-extern">Tutorials</a></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-msq-extern">Load files using SQL</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-kafka">Load from Apache Kafka</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-batch-hadoop">Load from Apache Hadoop</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-query">Query data</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-rollup">Aggregate data with rollup</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-sketches-theta">Theta sketches</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-retention">Configure data retention</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-update-data">Update existing data</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-compaction">Compact segments</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-delete-data">Deleting data</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-ingestion-spec">Write an ingestion spec</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-transform-spec">Transform input data</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-msq-convert-spec">Convert ingestion spec to SQL</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/docker">Run with Docker</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-kerberos-hadoop">Kerberized HDFS deep storage</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-sql-query-view">Get to know Query view</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-unnest-arrays">Unnesting arrays</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-query-deep-storage">Query from deep storage</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-jupyter-index">Jupyter Notebook tutorials</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-jupyter-docker">Docker for tutorials</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/27.0.0/tutorials/tutorial-jdbc">JDBC connector</a></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/design/architecture">Design</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/ingestion/">Ingestion</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/data-management/">Data management</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/querying/sql">Querying</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/api-reference/">API reference</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/configuration/">Configuration</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/operations/web-console">Operations</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/development/overview">Development</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/27.0.0/misc/papers-and-talks">Misc</a></div></li></ul></nav></div></div></aside><main class="docMainContainer_gTbr"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_VOVn"><div class="docItemContainer_Djhp"><article><nav class="theme-doc-breadcrumbs breadcrumbsContainer_Z_bl" aria-label="Breadcrumbs"><ul class="breadcrumbs" itemscope="" itemtype="https://schema.org/BreadcrumbList"><li class="breadcrumbs__item"><a aria-label="Home page" class="breadcrumbs__link" href="/"><svg viewBox="0 0 24 24" class="breadcrumbHomeIcon_YNFT"><path d="M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z" fill="currentColor"></path></svg></a></li><li class="breadcrumbs__item"><span class="breadcrumbs__link">Tutorials</span><meta itemprop="position" content="1"></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item breadcrumbs__item--active"><span class="breadcrumbs__link" itemprop="name">Get to know Query view</span><meta itemprop="position" content="2"></li></ul></nav><div class="tocCollapsible_ETCw theme-doc-toc-mobile tocMobile_ITEo"><button type="button" class="clean-btn tocCollapsibleButton_TO0P">On this page</button></div><div class="theme-doc-markdown markdown"><header><h1>Get to know Query view</h1></header><p>This tutorial demonstrates some useful features built into Query view in Apache Druid.</p><p>Query view lets you run <a href="/docs/27.0.0/querying/sql">Druid SQL queries</a> and <a href="/docs/27.0.0/querying/">native (JSON-based) queries</a> against ingested data. Try out the <a href="/docs/27.0.0/tutorials/tutorial-jupyter-index#tutorials">Introduction to Druid SQL</a> tutorial to learn more about Druid SQL.</p><p>You can use Query view to test and tune queries before you use them in API requests<!-- --><!-- -->for example, to perform <a href="/docs/27.0.0/api-reference/sql-ingestion-api">SQL-based ingestion</a>. You can also ingest data directly in Query view.</p><p>The tutorial guides you through the steps to ingest sample data and query the ingested data using some Query view features.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="prerequisites">Prerequisites<a href="#prerequisites" class="hash-link" aria-label="Direct link to Prerequisites" title="Direct link to Prerequisites"></a></h2><p>Before you follow the steps in this tutorial, download Druid as described in the <a href="/docs/27.0.0/tutorials/">quickstart</a> and have it running on your local machine. You don&#x27;t need to have loaded any data.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="run-a-demo-query-to-ingest-data">Run a demo query to ingest data<a href="#run-a-demo-query-to-ingest-data" class="hash-link" aria-label="Direct link to Run a demo query to ingest data" title="Direct link to Run a demo query to ingest data"></a></h2><p>Druid includes demo queries that each demonstrate a different Druid feature<!-- --><!-- -->for example transforming data during ingestion and sorting ingested data. Each query has detailed comments to help you learn more.</p><p>In this section you load the demo queries and run a SQL task to ingest sample data into a <a href="/docs/27.0.0/querying/datasource#table">table datasource</a>.</p><ol><li><p>Navigate to the Druid console at <a href="http://localhost:8888" target="_blank" rel="noopener noreferrer">http://localhost:8888</a> and click <strong>Query</strong>.</p></li><li><p>Click the ellipsis at the bottom of the query window and select <strong>Load demo queries</strong>. Note that loading the demo queries replaces all of your current query tabs. The demo queries load in several tabs:</p><p><img loading="lazy" alt="demo queries" src="/assets/images/tutorial-sql-demo-queries-18b129cf96aca4bf1f61903039a0d462.png" width="1250" height="640" class="img_ev3q"></p></li><li><p>Click the <strong>Demo 1</strong> tab. This query ingests sample data into a datasource called <strong>kttm_simple</strong>. Click the <strong>Demo 1</strong> tab heading again and note the options<!-- --><!-- -->you can rename, copy, and duplicate tabs.</p></li><li><p>Click <strong>Run</strong> to ingest the data.</p></li><li><p>When ingestion is complete, Druid displays the time it took to complete the insert query, and the new datasource <strong>kttm_simple</strong> displays in the left pane.</p></li></ol><h2 class="anchor anchorWithStickyNavbar_LWe7" id="view-and-filter-query-results">View and filter query results<a href="#view-and-filter-query-results" class="hash-link" aria-label="Direct link to View and filter query results" title="Direct link to View and filter query results"></a></h2><p>In this section you run some queries against the new datasource and perform some operations on the query results.</p><ol><li><p>Click <strong>+</strong> to the right of the existing tabs to open a new query tab.</p></li><li><p>Click the name of the datasource <strong>kttm_simple</strong> in the left pane to display some automatically generated queries:</p><p><img loading="lazy" alt="auto queries" src="/assets/images/tutorial-sql-auto-queries-eb62d0f4f2bb563fddf353a7afbd18a2.png" width="1250" height="640" class="img_ev3q"></p></li><li><p>Click <strong>SELECT * FROM kttm_simple</strong> and run the query.</p></li><li><p>In the query results pane, click <strong>Chrome</strong> anywhere it appears in the <strong>browser</strong> column then click <strong>Filter on: browser = &#x27;Chrome&#x27;</strong> to filter the results.</p></li></ol><h2 class="anchor anchorWithStickyNavbar_LWe7" id="run-aggregate-queries">Run aggregate queries<a href="#run-aggregate-queries" class="hash-link" aria-label="Direct link to Run aggregate queries" title="Direct link to Run aggregate queries"></a></h2><p><a href="/docs/27.0.0/querying/sql-aggregations">Aggregate functions</a> allow you to perform a calculation on a set of values and return a single value.</p><p>In this section you run some queries using aggregate functions and perform some operations on the results, using shortcut features designed to help you build your query.</p><ol><li><p>Open a new query tab.</p></li><li><p>Click <strong>kttm_simple</strong> in the left pane to display the generated queries.</p></li><li><p>Click <strong>SELECT COUNT(*) AS &quot;Count&quot; FROM kttm_simple</strong> and run the query.</p></li><li><p>After you run a query that contains an aggregate function, additional Query view options become available. </p><p>Click the arrow to the left of the <strong>kttm_simple</strong> datasource to display the columns, then click the <strong>country</strong> column. Several options appear to apply country-based filters and aggregate functions to the query:</p><p><img loading="lazy" alt="count distinct" src="/assets/images/tutorial-sql-count-distinct-d6020a15c19fff26ed0570fb2a7c7a64.png" width="1250" height="640" class="img_ev3q"></p></li><li><p>Click <strong>Aggregate &gt; COUNT(DISTINCT &quot;country&quot;)</strong> to add this clause to the query. The query now appears as follows:</p><div class="language-sql codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-sql codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token keyword" style="font-style:italic">SELECT</span><span class="token plain"> </span><span class="token function" style="color:rgb(130, 170, 255)">COUNT</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token operator" style="color:rgb(137, 221, 255)">*</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"> </span><span class="token keyword" style="font-style:italic">AS</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">&quot;Count&quot;</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"> </span><span class="token function" style="color:rgb(130, 170, 255)">COUNT</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token keyword" style="font-style:italic">DISTINCT</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">&quot;country&quot;</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"> </span><span class="token keyword" style="font-style:italic">AS</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">&quot;dist_country&quot;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">FROM</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">&quot;kttm_simple&quot;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">GROUP</span><span class="token plain"> </span><span class="token keyword" style="font-style:italic">BY</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>Note that you can use column names such as <code>dist_country</code> in this example as shortcuts when building your query.</p></li><li><p>Run the updated query:</p><p><img loading="lazy" alt="aggregate-query" src="/assets/images/tutorial-sql-aggregate-query-cda2de57ff7e679c2ebb5d1373b3ff62.png" width="1250" height="640" class="img_ev3q"></p></li><li><p>Click <strong>Engine: auto (sql-native)</strong> to display the engine options<!-- --><strong>native</strong> for native (JSON-based) queries, <strong>sql-native</strong> for Druid SQL queries, and <strong>sql-msq-task</strong> for SQL-based ingestion. </p><p>Select <strong>auto</strong> to let Druid select the most efficient engine based on your query input.</p></li><li><p>From the engine menu you can also edit the query context and turn off some query defaults. </p><p>Deselect <strong>Use approximate COUNT(DISTINCT)</strong> and rerun the query. The country count in the results decreases because the computation has become more exact. See <a href="/docs/27.0.0/querying/sql-aggregations">SQL aggregation functions</a> for more information.</p></li><li><p>Query view can provide information about a function, in case you aren&#x27;t sure exactly what it does.</p><p>Delete the contents of the query line <code>COUNT(DISTINCT country) AS dist_country</code> and type <code>COUNT(DISTINCT)</code> to replace it. A help dialog for the function displays:</p><p><img loading="lazy" alt="count distinct help" src="/assets/images/tutorial-sql-count-distinct-help-4c7ab10ef4a68ccd07adc09287bbe3e5.png" width="1250" height="640" class="img_ev3q"></p><p>Click outside the help window to close it.</p></li><li><p>You can perform actions on calculated columns in the results pane.</p><p>Click the results column heading <strong>dist_country COUNT(DISTINCT &quot;country&quot;)</strong> to see the available options:</p><p><img loading="lazy" alt="result columns actions" src="/assets/images/tutorial-sql-result-column-actions-f40697022055f14315241ca973c0847d.png" width="1250" height="640" class="img_ev3q"></p></li><li><p>Select <strong>Edit column</strong> and change the <strong>Output name</strong> to <strong>Distinct countries</strong>.</p></li></ol><h2 class="anchor anchorWithStickyNavbar_LWe7" id="generate-an-explain-plan">Generate an explain plan<a href="#generate-an-explain-plan" class="hash-link" aria-label="Direct link to Generate an explain plan" title="Direct link to Generate an explain plan"></a></h2><p>In this section you generate an explain plan for a query. An explain plan shows the full query details and all of the operations Druid performs to execute it. </p><p>Druid optimizes queries of certain <a href="/docs/27.0.0/querying/sql-translation#query-types">types</a><!-- -->see <a href="/docs/27.0.0/querying/sql-translation">SQL query translation</a> for information on how to interpret an explain plan and use the details to improve query performance.</p><ol><li><p>Open a new query tab.</p></li><li><p>Click <strong>kttm_simple</strong> in the left pane to display the generated queries.</p></li><li><p>Click <strong>SELECT * FROM kttm_simple</strong> and run the query.</p></li><li><p>Click the ellipsis at the bottom of the query window and select <strong>Explain SQL query</strong>. The query plan opens in a new window:</p><p><img loading="lazy" alt="query plan" src="/assets/images/tutorial-sql-query-plan-f9aae6d8f66389766c16b59aadd1a2f0.png" width="1250" height="640" class="img_ev3q"></p></li><li><p>Click <strong>Open in new tab</strong>. You can review the query details and modify it as required.</p></li><li><p>Change the limit from 1001 to 2001:</p><div class="language-sql codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-sql codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token string" style="color:rgb(195, 232, 141)">&quot;Limit&quot;</span><span class="token plain">: </span><span class="token number" style="color:rgb(247, 140, 108)">2001</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>and run the query to confirm that the updated query returns 2,001 results.</p></li></ol><h2 class="anchor anchorWithStickyNavbar_LWe7" id="try-out-a-few-more-features">Try out a few more features<a href="#try-out-a-few-more-features" class="hash-link" aria-label="Direct link to Try out a few more features" title="Direct link to Try out a few more features"></a></h2><p>In this section you try out a few more useful Query view features.</p><h3 class="anchor anchorWithStickyNavbar_LWe7" id="use-calculator-mode">Use calculator mode<a href="#use-calculator-mode" class="hash-link" aria-label="Direct link to Use calculator mode" title="Direct link to Use calculator mode"></a></h3><p>Queries without a FROM clause run in calculator mode<!-- --><!-- -->this can be useful to help you understand how functions work. See the <a href="/docs/27.0.0/querying/sql-functions">Druid SQL functions</a> reference for more information.</p><ol><li><p>Open a new query tab and enter the following:</p><div class="language-sql codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-sql codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token keyword" style="font-style:italic">SELECT</span><span class="token plain"> SQRT</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token number" style="color:rgb(247, 140, 108)">49</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div></li><li><p>Run the query to produce the result <code>7</code>.</p></li></ol><h3 class="anchor anchorWithStickyNavbar_LWe7" id="download-query-results">Download query results<a href="#download-query-results" class="hash-link" aria-label="Direct link to Download query results" title="Direct link to Download query results"></a></h3><p>You can download query results in CSV, TSV, or newline-delimited JSON format.</p><ol><li><p>Open a new query tab and run a query, for example:</p><div class="language-sql codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-sql codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token keyword" style="font-style:italic">SELECT</span><span class="token plain"> </span><span class="token keyword" style="font-style:italic">DISTINCT</span><span class="token plain"> platform</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">FROM</span><span class="token plain"> kttm_simple</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div></li><li><p>Above the results pane, click the down arrow and select <strong>Download results as… CSV</strong>. </p></li></ol><h3 class="anchor anchorWithStickyNavbar_LWe7" id="view-query-history">View query history<a href="#view-query-history" class="hash-link" aria-label="Direct link to View query history" title="Direct link to View query history"></a></h3><p>In any query tab, click the ellipsis at the bottom of the query window and select <strong>Query history</strong>. </p><p>You can click the links on the left to view queries run at a particular date and time, and open a previously run query in a new query tab.</p><h2 class="anchor anchorWithStickyNavbar_LWe7" id="further-reading">Further reading<a href="#further-reading" class="hash-link" aria-label="Direct link to Further reading" title="Direct link to Further reading"></a></h2><p>For more information on ingestion and querying data, see the following topics:</p><ul><li><a href="/docs/27.0.0/tutorials/">Quickstart</a> for information on getting started with Druid.</li><li><a href="/docs/27.0.0/tutorials/tutorial-query">Tutorial: Querying data</a> for example queries to run on Druid data.</li><li><a href="/docs/27.0.0/ingestion/">Ingestion</a> for an overview of ingestion and the ingestion methods available in Druid.</li><li><a href="/docs/27.0.0/multi-stage-query/">SQL-based ingestion</a> for an overview of SQL-based ingestion.</li><li><a href="/docs/27.0.0/multi-stage-query/examples">SQL-based ingestion query examples</a> for examples of SQL-based ingestion for various use cases.</li></ul></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages"><a class="pagination-nav__link pagination-nav__link--prev" href="/docs/27.0.0/tutorials/tutorial-kerberos-hadoop"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">Kerberized HDFS deep storage</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/docs/27.0.0/tutorials/tutorial-unnest-arrays"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">Unnesting arrays</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#prerequisites" class="table-of-contents__link toc-highlight">Prerequisites</a></li><li><a href="#run-a-demo-query-to-ingest-data" class="table-of-contents__link toc-highlight">Run a demo query to ingest data</a></li><li><a href="#view-and-filter-query-results" class="table-of-contents__link toc-highlight">View and filter query results</a></li><li><a href="#run-aggregate-queries" class="table-of-contents__link toc-highlight">Run aggregate queries</a></li><li><a href="#generate-an-explain-plan" class="table-of-contents__link toc-highlight">Generate an explain plan</a></li><li><a href="#try-out-a-few-more-features" class="table-of-contents__link toc-highlight">Try out a few more features</a><ul><li><a href="#use-calculator-mode" class="table-of-contents__link toc-highlight">Use calculator mode</a></li><li><a href="#download-query-results" class="table-of-contents__link toc-highlight">Download query results</a></li><li><a href="#view-query-history" class="table-of-contents__link toc-highlight">View query history</a></li></ul></li><li><a href="#further-reading" class="table-of-contents__link toc-highlight">Further reading</a></li></ul></div></div></div></div></main></div></div><footer class="footer"><div class="container container-fluid"><div class="footer__bottom text--center"><div class="margin-bottom--sm"><img src="/img/favicon.png" class="themedImage_ToTc themedImage--light_HNdA footer__logo"><img src="/img/favicon.png" class="themedImage_ToTc themedImage--dark_i4oU footer__logo"></div><div class="footer__copyright">Copyright © 2023 Apache Software Foundation. Except where otherwise noted, licensed under CC BY-SA 4.0. Apache Druid, Druid, and the Druid logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.</div></div></div></footer></div>
<script src="/assets/js/runtime~main.5371e784.js"></script>
<script src="/assets/js/main.832012d1.js"></script>
</body>
</html>