{"id":3906,"date":"2025-06-06T20:01:31","date_gmt":"2025-06-06T20:01:31","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3906"},"modified":"2025-06-07T09:55:17","modified_gmt":"2025-06-07T09:55:17","slug":"sql-select-distinct","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-select-distinct\/","title":{"rendered":"SQL SELECT DISTINCT"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Sometimes, your query results have duplicates. To get <strong>only unique (different) values<\/strong>, we use <code>DISTINCT<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 How to Use DISTINCT<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Get unique departments from employees\nSELECT DISTINCT department FROM employees;\n\n-- Multiple columns: unique combinations of those columns\nSELECT DISTINCT department, job_title FROM employees;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 What DISTINCT Does<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Removes duplicate rows from the result<\/li>\n\n\n\n<li>Works on all selected columns combined (if multiple columns selected)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u26a0\ufe0f Notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Using <code>DISTINCT<\/code> can slow down queries on large data sets \u2014 use only when needed<\/li>\n\n\n\n<li>To remove duplicates on a single column, just select that column with <code>DISTINCT<\/code><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde9 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Example<\/th><th>What it does<\/th><\/tr><\/thead><tbody><tr><td><code>SELECT DISTINCT department FROM employees;<\/code><\/td><td>List of unique departments<\/td><\/tr><tr><td><code>SELECT DISTINCT department, job_title FROM employees;<\/code><\/td><td>Unique department &amp; job_title combos<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Simple and useful to clean up your data output!<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, your query results have duplicates. To get only unique (different) values, we use DISTINCT. \ud83d\udd39 How to Use DISTINCT \ud83e\udde0 What DISTINCT Does \u26a0\ufe0f Notes \ud83e\udde9 Quick Recap Example What it does SELECT DISTINCT department FROM employees; List of unique departments SELECT DISTINCT department, job_title FROM employees; Unique department &amp; job_title combos \u2705 Simple [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3906","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3906","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3906"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3906"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}