{"id":10105,"date":"2024-04-06T16:38:46","date_gmt":"2024-04-06T13:38:46","guid":{"rendered":"https:\/\/sunucun.com.tr\/bilgi\/?post_type=dt_articles&#038;p=10105"},"modified":"2026-11-11T23:37:55","modified_gmt":"2026-11-11T20:37:55","slug":"mysql-select-query","status":"publish","type":"post","link":"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/","title":{"rendered":"MySQL SELECT Query: Retrieving Data from the Database"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_90 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#Basic_Structure_of_a_SELECT_Query\" >Basic Structure of a SELECT Query<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#Example_Table\" >Example Table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#SELECT_Query_Examples\" >SELECT Query Examples<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#1_Selecting_All_Columns\" >1. Selecting All Columns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#2_Selecting_Specific_Columns\" >2. Selecting Specific Columns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#3_Filtering_by_a_Condition_WHERE\" >3. Filtering by a Condition (WHERE)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#4_Multiple_Conditions_AND_OR\" >4. Multiple Conditions (AND \/ OR)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#5_Sorting_ORDER_BY\" >5. Sorting (ORDER BY)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#6_Limiting_the_Number_of_Results_LIMIT\" >6. Limiting the Number of Results (LIMIT)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#7_Unique_Values_DISTINCT\" >7. Unique Values (DISTINCT)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#8_Grouping_and_Summarizing_GROUP_BY\" >8. Grouping and Summarizing (GROUP BY)<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#Clause_Order\" >Clause Order<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/sunucun.com.tr\/blog\/mysql-select-query\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">MySQL is an open-source relational database management system (RDBMS) used by applications all over the world. The basic way to read information from a database is the <strong>SELECT<\/strong> query. In this article we explain the structure of SELECT and its filtering, sorting, limiting and grouping options using a single example table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Basic_Structure_of_a_SELECT_Query\"><\/span>Basic Structure of a SELECT Query<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ...\nFROM table_name;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SELECT:<\/strong> Specifies which columns to return. Use <code>*<\/code> (asterisk) for all columns.<\/li>\n<li><strong>FROM:<\/strong> Specifies the table to read the data from.<\/li>\n<li>The query ends with a semicolon (<code>;<\/code>). SQL keywords are not case-sensitive; they are usually written in capitals for readability.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"998\" height=\"692\" class=\"wp-image-10102\" style=\"width: 485px; height: auto;\" src=\"https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2024\/04\/logo-mysql.png\" alt=\"MySQL logo\" srcset=\"https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2024\/04\/logo-mysql.png 998w, https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2024\/04\/logo-mysql-300x208.png 300w, https:\/\/sunucun.com.tr\/blog\/wp-content\/uploads\/2024\/04\/logo-mysql-768x533.png 768w\" sizes=\"(max-width: 998px) 100vw, 998px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example_Table\"><\/span>Example Table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The examples below use the following table named <code>customers<\/code>:<\/p>\n\n\n\n<div class=\"snc-table-wrap\"><table>\n<thead><tr><th>id<\/th><th>name<\/th><th>city<\/th><th>balance<\/th><\/tr><\/thead>\n<tbody>\n<tr><td>1<\/td><td>Ay\u015fe<\/td><td>Istanbul<\/td><td>1500<\/td><\/tr>\n<tr><td>2<\/td><td>Mehmet<\/td><td>Ankara<\/td><td>300<\/td><\/tr>\n<tr><td>3<\/td><td>Zeynep<\/td><td>Istanbul<\/td><td>820<\/td><\/tr>\n<tr><td>4<\/td><td>Can<\/td><td>Izmir<\/td><td>0<\/td><\/tr>\n<\/tbody><\/table><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"SELECT_Query_Examples\"><\/span>SELECT Query Examples<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Selecting_All_Columns\"><\/span>1. Selecting All Columns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM customers;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Returns every row and column in the table. It is handy for a quick look at a table, but in application code selecting only the columns you need avoids unnecessary data transfer and makes the query faster.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Selecting_Specific_Columns\"><\/span>2. Selecting Specific Columns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, city FROM customers;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Returns only the <code>name<\/code> and <code>city<\/code> columns. You can also give a column an alias with <code>AS<\/code>: <code>SELECT name AS customer_name FROM customers;<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Filtering_by_a_Condition_WHERE\"><\/span>3. Filtering by a Condition (WHERE)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, balance FROM customers\nWHERE city = 'Istanbul';<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Returns only the customers in Istanbul (Ay\u015fe and Zeynep). Operators such as <code>=<\/code>, <code>&lt;&gt;<\/code>, <code>&gt;<\/code>, <code>&lt;<\/code>, <code>BETWEEN<\/code>, <code>IN<\/code> and <code>LIKE<\/code> can be used in WHERE.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Multiple_Conditions_AND_OR\"><\/span>4. Multiple Conditions (AND \/ OR)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name FROM customers\nWHERE city = 'Istanbul' AND balance &gt; 1000;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>AND<\/code> requires both conditions to be true, <code>OR<\/code> requires at least one. This example returns only Ay\u015fe. Use parentheses to control precedence in complex conditions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Sorting_ORDER_BY\"><\/span>5. Sorting (ORDER BY)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, balance FROM customers\nORDER BY balance DESC;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Sorts the results by balance from highest to lowest. <code>ASC<\/code> means ascending (the default) and <code>DESC<\/code> means descending.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Limiting_the_Number_of_Results_LIMIT\"><\/span>6. Limiting the Number of Results (LIMIT)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT name, balance FROM customers\nORDER BY balance DESC\nLIMIT 2;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Returns the two customers with the highest balance. For pagination you can add an offset, for example <code>LIMIT 10 OFFSET 20<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Unique_Values_DISTINCT\"><\/span>7. Unique Values (DISTINCT)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DISTINCT city FROM customers;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Lists each city once: Istanbul, Ankara, Izmir.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8_Grouping_and_Summarizing_GROUP_BY\"><\/span>8. Grouping and Summarizing (GROUP BY)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT city, COUNT(*) AS customer_count, SUM(balance) AS total_balance\nFROM customers\nGROUP BY city;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Returns the number of customers and the total balance for each city; for Istanbul, 2 customers and a total balance of 2320. To filter grouped results, use <code>HAVING<\/code> instead of WHERE: <code>... GROUP BY city HAVING COUNT(*) &gt; 1;<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Clause_Order\"><\/span>Clause Order<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The clauses of a SELECT query are written in this order: <code>SELECT<\/code> \u2192 <code>FROM<\/code> \u2192 <code>WHERE<\/code> \u2192 <code>GROUP BY<\/code> \u2192 <code>HAVING<\/code> \u2192 <code>ORDER BY<\/code> \u2192 <code>LIMIT<\/code>. If the order is wrong, MySQL returns a syntax error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">SELECT is the foundation of reading data in MySQL. By combining column selection, filtering with WHERE, sorting with ORDER BY, limiting with LIMIT and summarizing with GROUP BY, you can retrieve exactly the data you need. On large tables, adding indexes to the columns used in WHERE and ORDER BY makes queries noticeably faster.<\/p>\n\n\n\n<p class=\"snc-related wp-block-paragraph\" data-snc=\"rel20261105\"><strong>Related:<\/strong> <a href=\"https:\/\/sunucun.com.tr\/blog\/creating-a-mysql-database\/\">Creating a MySQL Database<\/a> \u00b7 <a href=\"https:\/\/sunucun.com.tr\/blog\/creating-mysql-users-and-granting-permissions-managing-database-users-and-privileges\/\">Creating MySQL Users and Granting Permissions: Managing Database Users and Privileges<\/a> \u00b7 <a href=\"https:\/\/sunucun.com.tr\/blog\/connecting-to-mysql-database-establishing-connection-with-mysql-workbench-command-line-or-web-application\/\">Connecting to MySQL Database: Establishing Connection with MySQL Workbench, Command Line, or Web Application<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL is an open-source relational database management system (RDBMS) used by applications all over the world. The basic way to read information from a database is the SELECT query. In this article we explain the structure of SELECT and its filtering, sorting, limiting and grouping options using a single example table. Basic Structure of a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":10103,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1470],"tags":[],"class_list":["post-10105","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/10105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=10105"}],"version-history":[{"count":4,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/10105\/revisions"}],"predecessor-version":[{"id":19949,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/10105\/revisions\/19949"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media\/10103"}],"wp:attachment":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=10105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=10105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=10105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}