{"id":9128,"date":"2022-09-16T01:08:10","date_gmt":"2022-09-16T08:08:10","guid":{"rendered":"https:\/\/www.pingcap.com\/?p=9128"},"modified":"2024-12-20T04:04:43","modified_gmt":"2024-12-20T12:04:43","slug":"how-to-query-data-from-tidb-with-sql-or-java","status":"publish","type":"post","link":"https:\/\/www.pingcap.com\/ko\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/","title":{"rendered":"How to Query Data from TiDB with SQL or Java\u00a0"},"content":{"rendered":"<p>Authors: <a href=\"http:\/\/github.com\/Mini256\">Zhiyuan Liang<\/a>, <a href=\"http:\/\/github.com\/Icemap\">Qizhi Wang<\/a> (Senior Software Engineers at PingCAP)<br>Editors: <a href=\"http:\/\/github.com\/septemberfd\">Fendy Feng<\/a>, Tom Dewan<\/p>\n\n\n\n<p>The database is a crucial component for building applications. It stores and analyzes the huge amounts of data coming in from outside tools or generated when people use the application. By retrieving data that fits certain criteria, queries can help you make the best use of that data.&nbsp;<\/p>\n\n\n\n<p>TiDB is an open source, distributed, and highly-scalable NewSQL database. It has an established record in supporting many data-intensive applications. In this tutorial, we\u2019ll use TiDB as an example and show you how to query data from a relational SQL database with SQL or Java.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Before_you_get_started\"><\/span>Before you get started<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before getting started to query data, you need to build a TiDB cluster, import data into this cluster, and then connect to TiDB.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><ol>\n<li><strong>Build a TiDB cluster.<\/strong> You can either create a local TiDB cluster or a <a href=\"https:\/\/www.pingcap.com\/ko\/tidb-cloud\/\">TiDB Cloud<\/a> free cluster, which is a fully-managed Database-as-a-Service (DBaaS).\n<ul>\n<li>To create a local TiDB cluster, see <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/production-deployment-using-tiup\">Deploy a TiDB Cluster Using TiUP<\/a>.<\/li>\n<li>To create a TiDB Cloud free cluster, see <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/dev-guide-build-cluster-in-cloud#step-1-create-a-free-cluster\">Build a TiDB Cluster in TiDB Cloud<\/a>.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Import data into TiDB<\/strong>. We\u2019ll use the sample data from the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bookshop_(company)\">Bookshop<\/a> application, a popular virtual bookstore. If you\u2019ve built a local TiDB cluster, use <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/ecosystem-tool-user-guide\">TiUP<\/a> to import this data; if you\u2019ve built a TiDB Cloud cluster, just click the <strong>Import<\/strong> button on the <strong>TiDB Cloud<\/strong> page and follow the instructions that followed to finish the data importing process. For a detailed guide, see <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/dev-guide-bookshop-schema-design#import-table-structures-and-data\">Import Table Structures and Data from Bookshop to TiDB<\/a>.<\/li>\n<li><strong>Connect to TiDB.<\/strong> TiDB is fully MySQL compatible, so you can choose any SQL client drivers or object relational mapping frameworks to connect to TiDB. For a detailed guide on TiDB connection, see <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/dev-guide-connect-to-tidb\">Connect to TiDB<\/a>.<\/li>\n<\/ol>\n<\/div>\n\n\n\n<p>After you&#8217;ve fully prepared, you can start to make queries. In the following sections, we&#8217;ll elaborate on how to query data from TiDB via SQL or Java.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Execute_a_simple_query\"><\/span>Execute a simple query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The Bookshop application dataset includes an <code>authors<\/code> table, which stores basic information about their writers. To query author information from TiDB, use the <code>SELECT ... FROM \u2026<\/code> SQL statement.<\/p>\n\n\n\n<p>For example, by executing the following SQL statement in a MySQL client, you can get the <code>id<\/code> \uadf8\ub9ac\uace0 <code>name<\/code> of all the authors stored in TiDB.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT id, name FROM authors;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>The results TiDB returns are:&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>+------------+--------------------------+\n| id         | name                     |\n+------------+--------------------------+\n|       6357 | Adelle Bosco             |\n|     345397 | Chanelle Koepp           |\n|     807584 | Clementina Ryan          |\n|     839921 | Gage Huel                |\n|     850070 | Ray Armstrong            |\n|     850362 | Ford Waelchi             |\n|     881210 | Jayme Gutkowski          |\n|    1165261 | Allison Kuvalis          |\n|    1282036 | Adela Funk               |\n...\n| 4294957408 | Lyla Nitzsche            |\n+------------+--------------------------+\n20000 rows in set (0.05 sec)\n<\/code><\/pre>\n\n\n\n<div class=\"wp-block-jetpack-markdown\"><p>In Java, to get the same results:<\/p>\n<ol>\n<li>You should first define how to store the author&#8217;s basic information by declaring an <code>Author<\/code> class.<\/li>\n<li>Select the appropriate data type from the Java language according to the <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/data-type-overview\">data types<\/a> \uadf8\ub9ac\uace0 <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/data-type-numeric\">value range<\/a> TiDB supports to store the corresponding data. For example, you can use a variable of:\n<ul>\n<li>Type <code>Int<\/code> to store data of type <code>int<\/code>.<\/li>\n<li>Type <code>Long<\/code> to store data of type <code>bigint<\/code>.<\/li>\n<li>Type <code>Short<\/code> to store data of type <code>tinyint<\/code>.<\/li>\n<li>Type <code>String<\/code> to store data of type <code>varchar<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre><code>public class Author {\n    private Long id;\n    private String name;\n    private Short gender;\n    private Short birthYear;\n    private Short deathYear;\n    public Author() {}\n     \/\/ Skip the getters and setters.\n}\n<\/code><\/pre>\n<p>3.After <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/dev-guide-connect-to-tidb#jdbc\">connecting to TiDB using the JDBC driver<\/a>, create a <code>Statement<\/code> object with <code>conn.createStatus()<\/code>.<\/p>\n<p>4.Call <code>stmt.executeQuery(\"query_sql\")<\/code> to initiate a database query request to TiDB.<\/p>\n<p>5.The query results will be stored in a <code>ResultSet<\/code> object. With the<code>ResultSet<\/code> traversed, the returned results can be mapped to the <code>Author<\/code> object.<\/p>\n<pre><code>public class AuthorDAO {\n    \/\/ Omit initialization of instance variables...\n    public List&lt;Author&gt; getAuthors() throws SQLException {\n        List&lt;Author&gt; authors = new ArrayList&lt;&gt;();\n        try (Connection conn = ds.getConnection()) {\n            Statement stmt = conn.createStatement();\n            ResultSet rs = stmt.executeQuery(\"SELECT id, name FROM authors\");\n            while (rs.next()) {\n                Author author = new Author();\n                author.setId( rs.getLong(\"id\"));\n                author.setName(rs.getString(\"name\"));\n                authors.add(author);\n            }\n        }\n        return authors;\n    }\n}\n<\/code><\/pre>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"trackable-btns\">\n  <a href=\"\/ko\/download\/\" onclick=\"trackViews('How to Query Data from TiDB with SQL or Java', 'download-tidb-btn-middle')\"><button>Download TiDB<\/button><\/a>\n  <a href=\"https:\/\/share.hsforms.com\/1e2W03wLJQQKPd1d9rCbj_Q2npzm\" onclick=\"trackViews('How to Query Data from TiDB with SQL or Java', 'subscribe-blog-btn-middle')\"><button>Subscribe to Blog<\/button><\/a>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Filter_results\"><\/span>Filter results<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To filter query results, use the <code>WHERE<\/code> statement.&nbsp;<\/p>\n\n\n\n<p>For example, by executing the following SQL statement, you can filter the authors born in 1998.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM authors WHERE birth_year = 1998;<\/code><\/pre>\n\n\n\n<p>In Java, you can use the same SQL statement to handle data query requests with dynamic parameters. To deal with such queries, we recommend that you use a <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/dev-guide-prepared-statement\">prepared statement<\/a> as shown below instead of a normal statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public List&lt;Author&gt; getAuthorsByBirthYear(Short birthYear) throws SQLException {\n    List&lt;Author&gt; authors = new ArrayList&lt;&gt;();\n    try (Connection conn = ds.getConnection()) {\n        PreparedStatement stmt = conn.prepareStatement(\"\"\"\n        SELECT * FROM authors WHERE birth_year = ?;\n        \"\"\");\n        stmt.setShort(1, birthYear);\n        ResultSet rs = stmt.executeQuery();\n        while (rs.next()) {\n            Author author = new Author();\n            author.setId( rs.getLong(\"id\"));\n            author.setName(rs.getString(\"name\"));\n            authors.add(author);\n        }\n    }\n    return authors;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sort_results\"><\/span>Sort results<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To sort results from tables stored in TiDB, you can use the <code>ORDER BY<\/code> statement.&nbsp;<\/p>\n\n\n\n<p>For example, by executing the SQL statement below, you can sort all authors in the <code>authors<\/code> table in descending order (<code>DESC<\/code>) according to their birth year.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT id, name, birth_year\nFROM authors\nORDER BY birth_year DESC;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>You can also use the <code>ORDER BY<\/code> statement in Java:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public List&lt;Author&gt; getAuthorsSortByBirthYear() throws SQLException {\n    List&lt;Author&gt; authors = new ArrayList&lt;&gt;();\n    try (Connection conn = ds.getConnection()) {\n        Statement stmt = conn.createStatement();\n        ResultSet rs = stmt.executeQuery(\"\"\"\n            SELECT id, name, birth_year\n            FROM authors\n            ORDER BY birth_year DESC;\n            \"\"\");\n\n        while (rs.next()) {\n            Author author = new Author();\n            author.setId(rs.getLong(\"id\"));\n            author.setName(rs.getString(\"name\"));\n            authors.add(author);\n        }\n    }\n    return authors;\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>In both cases, TiDB returns:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>+-----------+------------------------+------------+\n| id        | name                   | birth_year |\n+-----------+------------------------+------------+\n| 83420726  | Terrance Dach          | 2000       |\n| 57938667  | Margarita Christiansen | 2000       |\n| 77441404  | Otto Dibbert           | 2000       |\n| 61338414  | Danial Cormier         | 2000       |\n| 49680887  | Alivia Lemke           | 2000       |\n| 45460101  | Itzel Cummings         | 2000       |\n| 38009380  | Percy Hodkiewicz       | 2000       |\n| 12943560  | Hulda Hackett          | 2000       |\n| 1294029   | Stanford Herman        | 2000       |\n| 111453184 | Jeffrey Brekke         | 2000       |\n...\n300000 rows in set (0.23 sec)<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Limit_the_number_of_query_results\"><\/span>Limit the number of query results<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To limit the number of query results, you can use the <code>LIMIT<\/code> statement.&nbsp;<\/p>\n\n\n\n<p>For example, to get the <code>id<\/code> \uadf8\ub9ac\uace0 <code>name<\/code> of the 10 youngest authors and display them in descending order of their birth year, execute the following SQL statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT id, name, birth_year\nFROM authors\nORDER BY birth_year DESC\nLIMIT 10;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>You can also use the <code>LIMIT<\/code> statement in Java:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public List&lt;Author&gt; getAuthorsWithLimit(Integer limit) throws SQLException {\n    List&lt;Author&gt; authors = new ArrayList&lt;&gt;();\n    try (Connection conn = ds.getConnection()) {\n        PreparedStatement stmt = conn.prepareStatement(\"\"\"\n            SELECT id, name, birth_year\n            FROM authors\n            ORDER BY birth_year DESC\n            LIMIT ?;\n            \"\"\");\n        stmt.setInt(1, limit);\n        ResultSet rs = stmt.executeQuery();\n        while (rs.next()) {\n            Author author = new Author();\n            author.setId(rs.getLong(\"id\"));\n            author.setName(rs.getString(\"name\"));\n            authors.add(author);\n        }\n    }\n    return authors;\n}\n<\/code><\/pre>\n\n\n\n<p>In both cases, TiDB returns:&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>+-----------+------------------------+------------+\n| id        | name                   | birth_year |\n+-----------+------------------------+------------+\n| 83420726  | Terrance Dach          | 2000       |\n| 57938667  | Margarita Christiansen | 2000       |\n| 77441404  | Otto Dibbert           | 2000       |\n| 61338414  | Danial Cormier         | 2000       |\n| 49680887  | Alivia Lemke           | 2000       |\n| 45460101  | Itzel Cummings         | 2000       |\n| 38009380  | Percy Hodkiewicz       | 2000       |\n| 12943560  | Hulda Hackett          | 2000       |\n| 1294029   | Stanford Herman        | 2000       |\n| 111453184 | Jeffrey Brekke         | 2000       |\n+-----------+------------------------+------------+\n10 rows in set (0.11 sec)<\/code><\/pre>\n\n\n\n<p>Using the <code>LIMIT<\/code> statement cut the query time by more than 50%\u2014from 0.23 s to 0.11 s.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Aggregate_queries\"><\/span>Aggregate queries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To better understand the overall data situation, you can combine the <code>GROUP BY<\/code> statement with an aggregate function like the <code>COUNT<\/code> function to aggregate query results.<\/p>\n\n\n\n<p>For example, to know how many authors were born in each year and which year has the most authors, execute the following statement:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT birth_year, COUNT (DISTINCT id) AS author_count\nFROM authors\nGROUP BY birth_year\nORDER BY author_count DESC;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>You can also use the Java <code>Group BY<\/code> statement and <code>COUNT<\/code> function:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class AuthorCount {\n    private Short birthYear;\n    private Integer authorCount;\n\n    public AuthorCount() {}\n\n     \/\/ Skip the getters and setters.\n}\n\npublic List&lt;AuthorCount&gt; getAuthorCountsByBirthYear() throws SQLException {\n    List&lt;AuthorCount&gt; authorCounts = new ArrayList&lt;&gt;();\n    try (Connection conn = ds.getConnection()) {\n        Statement stmt = conn.createStatement();\n        ResultSet rs = stmt.executeQuery(\"\"\"\n            SELECT birth_year, COUNT(DISTINCT id) AS author_count\n            FROM authors\n            GROUP BY birth_year\n            ORDER BY author_count DESC;\n            \"\"\");\n\n        while (rs.next()) {\n            AuthorCount authorCount = new AuthorCount();\n            authorCount.setBirthYear(rs.getShort(\"birth_year\"));\n            authorCount.setAuthorCount(rs.getInt(\"author_count\"));\n            authorCounts.add(authorCount);\n        }\n    }\n    return authorCount;\n}<\/code><\/pre>\n\n\n\n<p>In both cases, TiDB returns:&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>+------------+--------------+\n| birth_year | author_count |\n+------------+--------------+\n|       1932 |          317 |\n|       1947 |          290 |\n|       1939 |          282 |\n|       1935 |          289 |\n|       1968 |          291 |\n|       1962 |          261 |\n|       1961 |          283 |\n|       1986 |          289 |\n|       1994 |          280 |\n...\n|       1972 |          306 |\n+------------+--------------+\n71 rows in set (0.00 sec)\n<\/code><\/pre>\n\n\n\n<p>In addition to the <code>COUNT<\/code> function, TiDB also supports other aggregate functions. For more information, see <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/aggregate-group-by-functions\">Aggregate (GROUP BY) Functions<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Wrapping_Up\"><\/span>Wrapping Up<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this tutorial, you\u2019ve learned how to query data from TiDB with SQL statements or Java commands. I hope you find this guide helpful. If you have any questions, contact us through <a href=\"https:\/\/twitter.com\/PingCAP\">Twitter<\/a> or our <a href=\"https:\/\/slack.tidb.io\/invite?team=tidb-community&amp;channel=everyone&amp;ref=pingcap-blog\">Slack channel<\/a>. You can also join TiDB Internals to share your thoughts and feedback.<\/p>\n\n\n\n<p>If you want to know more developer guides related to TiDB or TiDB Cloud, see <a href=\"https:\/\/docs.pingcap.com\/tidb\/stable\/dev-guide-overview\">TiDB Developer Guide<\/a> \ub610\ub294 <a href=\"https:\/\/docs.pingcap.com\/tidbcloud\/dev-guide-overview\">TiDB Cloud Developer Guide<\/a>.&nbsp;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>Keep reading:<\/strong><br><a href=\"https:\/\/www.pingcap.com\/ko\/blog\/building-a-web-application-with-spring-boot-and-tidb\/\">Building a Web Application with Spring Boot and TiDB<\/a><br><a href=\"https:\/\/www.pingcap.com\/ko\/blog\/using-retool-and-tidb-cloud-to-build-a-real-time-kanban-in-30-minutes\/\">Using Retool and TiDB Cloud to Build a Real-Time Kanban in 30 Minutes<\/a><br><a href=\"https:\/\/www.pingcap.com\/ko\/blog\/analytics-on-tidb-cloud-with-databricks\/\">Analytics on TiDB Cloud with Databricks<\/a><\/p>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>You\u2019ll learn how to query data from a SQL database with SQL and Java respectively. <\/p>","protected":false},"author":196,"featured_media":9131,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[18],"tags":[163,157,111,29],"class_list":["post-9128","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-community","tag-app-developer","tag-sql","tag-tidb","tag-tutorial"],"acf":[],"featured_image_src":"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg","author_info":{"display_name":"Zhiyuan Liang","author_link":"https:\/\/www.pingcap.com\/ko\/blog\/author\/zhiyuan-liang\/"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Query Data from TiDB with SQL or Java\u00a0 | TiDB<\/title>\n<meta name=\"description\" content=\"This post shows how to query data from TiDB, an open source distributed SQL database with SQL and Java respectively.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pingcap.com\/ko\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Query Data from TiDB with SQL or Java\u00a0 | TiDB\" \/>\n<meta property=\"og:description\" content=\"This post shows how to query data from TiDB, an open source distributed SQL database with SQL and Java respectively.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pingcap.com\/ko\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/\" \/>\n<meta property=\"og:site_name\" content=\"TiDB\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/pingcap2015\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-16T08:08:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-12-20T12:04:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-tidb-social-scaled.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"2560\" \/>\n\t<meta property=\"og:image:height\" content=\"1340\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Zhiyuan Liang\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-tidb-social-scaled.jpeg\" \/>\n<meta name=\"twitter:creator\" content=\"@PingCAP\" \/>\n<meta name=\"twitter:site\" content=\"@PingCAP\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Zhiyuan Liang\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/\"},\"author\":{\"name\":\"Zhiyuan Liang\",\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/person\/eb40ba298bc36a0f833ac79b7f473b0e\"},\"headline\":\"How to Query Data from TiDB with SQL or Java\u00a0\",\"datePublished\":\"2022-09-16T08:08:10+00:00\",\"dateModified\":\"2024-12-20T12:04:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/\"},\"wordCount\":904,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pingcap.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg\",\"keywords\":[\"App Developer\",\"SQL\",\"TiDB\",\"Tutorial\"],\"articleSection\":[\"Community\"],\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/\",\"url\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/\",\"name\":\"How to Query Data from TiDB with SQL or Java\u00a0 | TiDB\",\"isPartOf\":{\"@id\":\"https:\/\/www.pingcap.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg\",\"datePublished\":\"2022-09-16T08:08:10+00:00\",\"dateModified\":\"2024-12-20T12:04:43+00:00\",\"description\":\"This post shows how to query data from TiDB, an open source distributed SQL database with SQL and Java respectively.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#primaryimage\",\"url\":\"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg\",\"contentUrl\":\"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg\",\"width\":2560,\"height\":853},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pingcap.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Query Data from TiDB with SQL or Java\u00a0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pingcap.com\/#website\",\"url\":\"https:\/\/www.pingcap.com\/\",\"name\":\"TiDB\",\"description\":\"TiDB | SQL at Scale\",\"publisher\":{\"@id\":\"https:\/\/www.pingcap.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pingcap.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pingcap.com\/#organization\",\"name\":\"PingCAP\",\"url\":\"https:\/\/www.pingcap.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"contentUrl\":\"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png\",\"width\":811,\"height\":232,\"caption\":\"PingCAP\"},\"image\":{\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/pingcap2015\",\"https:\/\/x.com\/PingCAP\",\"https:\/\/linkedin.com\/company\/pingcap\",\"https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/person\/eb40ba298bc36a0f833ac79b7f473b0e\",\"name\":\"Zhiyuan Liang\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/www.pingcap.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/static.pingcap.com\/files\/2022\/10\/17234942\/avatar.jpg\",\"contentUrl\":\"https:\/\/static.pingcap.com\/files\/2022\/10\/17234942\/avatar.jpg\",\"caption\":\"Zhiyuan Liang\"},\"description\":\"Senior Software Engineer\",\"url\":\"https:\/\/www.pingcap.com\/ko\/blog\/author\/zhiyuan-liang\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Query Data from TiDB with SQL or Java\u00a0 | TiDB","description":"This post shows how to query data from TiDB, an open source distributed SQL database with SQL and Java respectively.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pingcap.com\/ko\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/","og_locale":"ko_KR","og_type":"article","og_title":"How to Query Data from TiDB with SQL or Java\u00a0 | TiDB","og_description":"This post shows how to query data from TiDB, an open source distributed SQL database with SQL and Java respectively.","og_url":"https:\/\/www.pingcap.com\/ko\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/","og_site_name":"TiDB","article_publisher":"https:\/\/facebook.com\/pingcap2015","article_published_time":"2022-09-16T08:08:10+00:00","article_modified_time":"2024-12-20T12:04:43+00:00","og_image":[{"width":2560,"height":1340,"url":"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-tidb-social-scaled.jpeg","type":"image\/jpeg"}],"author":"Zhiyuan Liang","twitter_card":"summary_large_image","twitter_image":"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-tidb-social-scaled.jpeg","twitter_creator":"@PingCAP","twitter_site":"@PingCAP","twitter_misc":{"Written by":"Zhiyuan Liang","Est. reading time":"7\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#article","isPartOf":{"@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/"},"author":{"name":"Zhiyuan Liang","@id":"https:\/\/www.pingcap.com\/#\/schema\/person\/eb40ba298bc36a0f833ac79b7f473b0e"},"headline":"How to Query Data from TiDB with SQL or Java\u00a0","datePublished":"2022-09-16T08:08:10+00:00","dateModified":"2024-12-20T12:04:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/"},"wordCount":904,"commentCount":0,"publisher":{"@id":"https:\/\/www.pingcap.com\/#organization"},"image":{"@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#primaryimage"},"thumbnailUrl":"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg","keywords":["App Developer","SQL","TiDB","Tutorial"],"articleSection":["Community"],"inLanguage":"ko-KR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/","url":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/","name":"How to Query Data from TiDB with SQL or Java\u00a0 | TiDB","isPartOf":{"@id":"https:\/\/www.pingcap.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#primaryimage"},"image":{"@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#primaryimage"},"thumbnailUrl":"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg","datePublished":"2022-09-16T08:08:10+00:00","dateModified":"2024-12-20T12:04:43+00:00","description":"This post shows how to query data from TiDB, an open source distributed SQL database with SQL and Java respectively.","breadcrumb":{"@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/"]}]},{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#primaryimage","url":"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg","contentUrl":"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg","width":2560,"height":853},{"@type":"BreadcrumbList","@id":"https:\/\/www.pingcap.com\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pingcap.com\/"},{"@type":"ListItem","position":2,"name":"How to Query Data from TiDB with SQL or Java\u00a0"}]},{"@type":"WebSite","@id":"https:\/\/www.pingcap.com\/#website","url":"https:\/\/www.pingcap.com\/","name":"\ud2f0DB","description":"TiDB | SQL at Scale","publisher":{"@id":"https:\/\/www.pingcap.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pingcap.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/www.pingcap.com\/#organization","name":"PingCAP","url":"https:\/\/www.pingcap.com\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/","url":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","contentUrl":"https:\/\/static.pingcap.com\/files\/2021\/11\/pingcap-logo.png","width":811,"height":232,"caption":"PingCAP"},"image":{"@id":"https:\/\/www.pingcap.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/pingcap2015","https:\/\/x.com\/PingCAP","https:\/\/linkedin.com\/company\/pingcap","https:\/\/youtube.com\/channel\/UCuq4puT32DzHKT5rU1IZpIA"]},{"@type":"Person","@id":"https:\/\/www.pingcap.com\/#\/schema\/person\/eb40ba298bc36a0f833ac79b7f473b0e","name":"Zhiyuan Liang","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/www.pingcap.com\/#\/schema\/person\/image\/","url":"https:\/\/static.pingcap.com\/files\/2022\/10\/17234942\/avatar.jpg","contentUrl":"https:\/\/static.pingcap.com\/files\/2022\/10\/17234942\/avatar.jpg","caption":"Zhiyuan Liang"},"description":"Senior Software Engineer","url":"https:\/\/www.pingcap.com\/ko\/blog\/author\/zhiyuan-liang\/"}]}},"grav_blocks":false,"card_markup":"<a class=\"card-resource bg-white\" href=\"https:\/\/www.pingcap.com\/ko\/blog\/how-to-query-data-from-tidb-with-sql-or-java\/\"><div class=\"card-resource__image-container\"><img class=\"card-resource__image\" alt=\"query data from Tidb\" src=\"https:\/\/static.pingcap.com\/files\/2022\/09\/query-data-from-Tidb-scaled.jpeg\" loading=\"lazy\" width=2560 height=853 \/><\/div><div class=\"card-resource__content-container\"><div class=\"card-resource__content-head\"><div class=\"card-resource__category\">Community<\/div><\/div><h5 class=\"card-resource__title\">How to Query Data from TiDB with SQL or Java\u00a0<\/h5><\/div><\/a>","_links":{"self":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/posts\/9128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/users\/196"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/comments?post=9128"}],"version-history":[{"count":26,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/posts\/9128\/revisions"}],"predecessor-version":[{"id":24441,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/posts\/9128\/revisions\/24441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/media\/9131"}],"wp:attachment":[{"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/media?parent=9128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/categories?post=9128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pingcap.com\/ko\/wp-json\/wp\/v2\/tags?post=9128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}