{"id":174185,"date":"2026-02-19T20:40:13","date_gmt":"2026-02-19T19:40:13","guid":{"rendered":"https:\/\/liora.io\/en\/?p=174185"},"modified":"2026-02-19T20:40:14","modified_gmt":"2026-02-19T19:40:14","slug":"sql-distinct-how-do-i-use-this-query","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/sql-distinct-how-do-i-use-this-query","title":{"rendered":"SQL DISTINCT: How do I use this query?"},"content":{"rendered":"<p><strong>While the SELECT query is one of the most commonly used to read data from a table, it has some limitations if used on its own. That&#8217;s why other commands are associated with it. These combinations allow you to refine the results. One of these commands is SQL DISTINCT, which removes duplicates within a MySQL database.<\/strong><\/p>\t\t\n\t\t\t<h2>What is SQL DISTINCT?<\/h2>\t\t\n\t\t<p>The<strong> SQL DISTINCT query<\/strong> is used to eliminate duplicates. When you use the SELECT <a href=\"https:\/\/liora.io\/en\/sql-queries-the-5-most-important-commands-to-know\">SQL query,<\/a> you can choose one or more columns from a table. However, within these columns, there may be duplicate rows. To avoid this, you can add DISTINCT to the SELECT query. By doing so, all redundancies are removed.<\/p><p>In practice, this command looks like this:<\/p><p>SELECT DISTINCT column_name<\/p><p>FROM table_name<\/p><p>You can use this command on a single column or on a set of related columns.<\/p><p><strong>?Good to know:<\/strong> If you are using an Oracle database management system, you should not use the SQL DISTINCT command but rather the UNIQUE command.<\/p>\t\t\n\t\t\t<h2>3 examples of using SQL DISTINCT<\/h2><figure class=\"wp-block-image size-large\" style=\"margin-top:var(--wp--preset--spacing--columns);margin-bottom:var(--wp--preset--spacing--columns)\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-1024x572.jpg\" alt=\"Three colleagues working together on a project, with a computer and sticky notes on the desk.\" class=\"wp-image-207725\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-56x56.jpg 56w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-115x64.jpg 115w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-150x150.jpg 150w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-210x117.jpg 210w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-300x167.jpg 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-410x270.jpg 410w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-440x246.jpg 440w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-448x448.jpg 448w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-587x510.jpg 587w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-768x429.jpg 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-785x438.jpg 785w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-1024x572.jpg 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-1250x590.jpg 1250w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-1440x680.jpg 1440w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-1536x857.jpg 1536w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis-2048x1143.jpg 2048w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/teamwork-office-project-analysis.jpg 2560w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\t\t\n\t\t<p>To help you better understand how to use the SQL DISTINCT query, here are 3 concrete application examples.<\/p>\t\t\n\t\t\t<h3>The SELECT DISTINCT query for a single column<\/h3>\t\t\n\t\t<p>This is the basic use of the SQL DISTINCT query.<\/p><p>In our example, you have a table with all your customers, some of whom belong to the same family (same last name).<\/p><p>Here is your table:<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-baqh{text-align:center;vertical-align:top}\n.tg .tg-9ix1{background-color:#6434fc;color:#efefef;font-size:20px;font-weight:bold;text-align:center;vertical-align:top}\n\n<table style=\"undefined;width: 300px\">\n<colgroup>\n<col style=\"width: 50px\">\n<col style=\"width: 125px\">\n<col style=\"width: 125px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>ID<\/th>\n    <th>First Name<\/th>\n    <th>Last Name<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>1<\/td>\n    <td>Pierre<\/td>\n    <td>Dupont<\/td>\n  <\/tr>\n  <tr>\n    <td>2<\/td>\n    <td>Jeanne<\/td>\n    <td>Martin<\/td>\n  <\/tr>\n  <tr>\n    <td>3<\/td>\n    <td>Henri<\/td>\n    <td>Durand<\/td>\n  <\/tr>\n  <tr>\n    <td>4<\/td>\n    <td>Bertrand<\/td>\n    <td>Petit<\/td>\n  <\/tr>\n  <tr>\n    <td>5<\/td>\n    <td>Martine<\/td>\n    <td>Dupont<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\n\t\t<p>If you want to display only unique last names (families), here is the query to use: <code>SELECT DISTINCT last_name FROM client<\/code>.<\/p><p>And here is the result:<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-5t56{background-color:#6434fc;color:#efefef;font-size:20px;text-align:center;vertical-align:top}\n.tg .tg-lap0{font-size:100%;text-align:center;vertical-align:top}\n\n<table style=\"undefined;width: 74px\">\n<colgroup>\n<col style=\"width: 100px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Name<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>Dupont<\/td>\n  <\/tr>\n  <tr>\n    <td>Martin<\/td>\n  <\/tr>\n  <tr>\n    <td>Durand<\/td>\n  <\/tr>\n  <tr>\n    <td>Petit<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\t\t\n\t\t<p>Here, the name Smith is displayed only once (as opposed to twice in the first table).<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/formation\/data-ia\/sql\">Learn SQL for Data Science<\/a><\/div><\/div>\n\n\t\t\t<h3>The SELECT DISTINCT query for multiple columns<\/h3>\t\t\n\t\t<p>As mentioned above, the DISTINCT command can also be used for multiple columns.<\/p><p>Let&#8217;s take an example of a similar Customer table:<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-5t56{background-color:#6434fc;color:#efefef;font-size:20px;text-align:center;vertical-align:top}\n.tg .tg-baqh{text-align:center;vertical-align:top}\n.tg .tg-mw77{background-color:#6434fc;color:#efefef;text-align:center;vertical-align:top}\n\n<table style=\"undefined;width: 300px\">\n<colgroup>\n<col style=\"width: 50px\">\n<col style=\"width: 125px\">\n<col style=\"width: 125px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>ID<\/th>\n    <th>First Name<\/th>\n    <th>Last Name<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>1<\/td>\n    <td>Martine<\/td>\n    <td>Dupont<\/td>\n  <\/tr>\n  <tr>\n    <td>2<\/td>\n    <td>Jeanne<\/td>\n    <td>Martin<\/td>\n  <\/tr>\n  <tr>\n    <td>3<\/td>\n    <td>Henry<\/td>\n    <td>Durand<\/td>\n  <\/tr>\n  <tr>\n    <td>4<\/td>\n    <td>Bertrand<\/td>\n    <td>Petit<\/td>\n  <\/tr>\n  <tr>\n    <td>5<\/td>\n    <td>Martine<\/td>\n    <td>Dupont<\/td>\n  <\/tr>\n  <tr>\n    <td>6<\/td>\n    <td>Antoine<\/td>\n    <td>Martin<\/td>\n  <\/tr>\n  <tr>\n    <td>7<\/td>\n    <td>Fabienne<\/td>\n    <td>Bernard<\/td>\n  <\/tr>\n  <tr>\n    <td>8<\/td>\n    <td>Christelle<\/td>\n    <td>Robert<\/td>\n  <\/tr>\n  <tr>\n    <td>9<\/td>\n    <td>Henri<\/td>\n    <td>Durand<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\n\t\t<p>If you want to display only unique families, here&#8217;s the query to use: SELECT DISTINCT first name last name FROM customer.<\/p><p>And here&#8217;s the result:<\/p>\t\t\n\t\t\t\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-5t56{background-color:#6434fc;color:#efefef;font-size:20px;text-align:center;vertical-align:top}\n.tg .tg-baqh{text-align:center;vertical-align:top}\n.tg .tg-lap0{font-size:100%;text-align:center;vertical-align:top}\n\n<table style=\"undefined;width: 250px\">\n<colgroup>\n<col style=\"width: 125px\">\n<col style=\"width: 125px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Pr\u00e9nom<\/th>\n    <th>Nom<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>Martine<\/td>\n    <td>Dupon<\/td>\n  <\/tr>\n  <tr>\n    <td>Jeanne<\/td>\n    <td>Martin<\/td>\n  <\/tr>\n  <tr>\n    <td>Henri<\/td>\n    <td>Durand<\/td>\n  <\/tr>\n  <tr>\n    <td>Bertrand<\/td>\n    <td>Petit<\/td>\n  <\/tr>\n  <tr>\n    <td>Antoine<\/td>\n    <td>Martin<\/td>\n  <\/tr>\n  <tr>\n    <td>Fabienne<\/td>\n    <td>Bernard<\/td>\n  <\/tr>\n  <tr>\n    <td>Christelle<\/td>\n    <td>Robert<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\t\t\n\t\t<p>In this case, the Martine Dupont and Henry Durand lines have been eliminated.<\/p>\t\t\n\t\t\t<h3>The number of unique values<\/h3>\t\t\n\t\t<p>If you want to know how many unique values your table contains, simply perform the following operation:<\/p><p>SELECT COUNT(DISTINCT name) AS Number FROM Customer<\/p><p>Most often, the SQL query DISTINCT is used with the COUNT command to count values. But you can add other aggregations to suit your needs. This will enable you to perform various mathematical or statistical operations, without taking duplicates into account.<\/p>\t\t\n\t\t\t<h2>Alternatives to SQL DISTINCT<\/h2><figure class=\"wp-block-image size-large\" style=\"margin-top:var(--wp--preset--spacing--columns);margin-bottom:var(--wp--preset--spacing--columns)\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"572\" src=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-1024x572.jpg\" alt=\"Computer screen showing a Python script running with results displayed in a table.\" class=\"wp-image-207600\" srcset=\"https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-56x56.jpg 56w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-115x64.jpg 115w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-150x150.jpg 150w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-210x117.jpg 210w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-300x167.jpg 300w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-410x270.jpg 410w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-440x246.jpg 440w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-448x448.jpg 448w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-587x510.jpg 587w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-768x429.jpg 768w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-785x438.jpg 785w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-1024x572.jpg 1024w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-1250x590.jpg 1250w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-1440x680.jpg 1440w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-1536x857.jpg 1536w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic-2048x1143.jpg 2048w, https:\/\/liora.io\/app\/uploads\/sites\/9\/2026\/02\/script-python-execution-logic.jpg 2560w\" sizes=\"(max-width: 1024px) 100vw, 1024px\"><\/figure>\t\t\n\t\t<p>Other commands can be used to eliminate duplicates. In particular, GROUP BY \/ ORDER BY.<\/p><p>In this context, <a href=\"https:\/\/liora.io\/en\/sql-tutorial-top-5-most-useful-methods\">the SQL query is as follows:<\/a><\/p><p>SELECT lastname, firstname FROM `Customer` GROUP BY lastname, firstname ORDER BY lastname, firstname<\/p><p>The result is exactly the same as with the query: SELECT DISTINCT firstname lastname FROM customer.<\/p>\t\t\n\t\t\t<h2>Master SQL with Liora<\/h2>\t\t\n\t\t<p>The SQL programming language is packed with essential queries for manipulating data. If you&#8217;d like to deepen your knowledge in this area, join our training courses at Liora. These business-oriented courses will have you 100% operational by the end of the course.<\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-scientist\">Become an SQL expert<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>While the SELECT query is one of the most commonly used to read data from a table, it has some limitations if used on its own. That&#8217;s why other commands are associated with it. These combinations allow you to refine the results. One of these commands is SQL DISTINCT, which removes duplicates within a MySQL [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":207727,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2433],"class_list":["post-174185","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=174185"}],"version-history":[{"count":3,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174185\/revisions"}],"predecessor-version":[{"id":207728,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/174185\/revisions\/207728"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/207727"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=174185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=174185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}