{"id":178,"date":"2018-12-06T11:13:28","date_gmt":"2018-12-06T11:13:28","guid":{"rendered":"https:\/\/barkhane.com\/sql\/2018\/12\/06\/sql-select-distinct\/"},"modified":"2024-03-03T17:58:59","modified_gmt":"2024-03-03T17:58:59","slug":"sql-select-distinct","status":"publish","type":"post","link":"https:\/\/barkhane.com\/sql\/sql-select-distinct\/","title":{"rendered":"SQL SELECT DISTINCT"},"content":{"rendered":"<p>The\u00a0<b>SQL DISTINCT command<\/b>\u00a0is used with SELECT key word to retrieve only distinct or unique data.<\/p>\n<p>In a <a href=\"https:\/\/barkhane.com\/sql\/sql-table\/\">table<\/a>, there may be a chance to exist a duplicate value and sometimes we want to retrieve only unique values. In such scenarios, SQL SELECT DISTINCT statement is used.<\/p>\n<h4 class=\"n\">Note: SQL SELECT UNIQUE and SQL SELECT DISTINCT statements are same.<\/h4>\n<p>Let\\&#8217;s see the syntax of select distinct statement.<\/p>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\">\n<div class=\"tools\"><\/div>\n<\/div>\n<ol class=\"dp-sql\" start=\"1\">\n<li class=\"alt\"><span class=\"keyword\">SELECT<\/span>\u00a0<span class=\"keyword\">DISTINCT<\/span>\u00a0column_name\u00a0,column_name<\/li>\n<li class=\"\"><span class=\"keyword\">FROM<\/span>\u00a0\u00a0table_name;<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>Let\\&#8217;s try to understand it by the table given below:<\/p>\n<table class=\"alt\">\n<tbody>\n<tr>\n<th>Student_Name<\/th>\n<th>Gender<\/th>\n<th>Mobile_Number<\/th>\n<th>HOME_TOWN<\/th>\n<\/tr>\n<tr>\n<td>Raju<\/td>\n<td>Male<\/td>\n<td>9865231470<\/td>\n<td>Indore<\/td>\n<\/tr>\n<tr>\n<td>Deepti<\/td>\n<td>Female<\/td>\n<td>8893927056<\/td>\n<td>Mumbai<\/td>\n<\/tr>\n<tr>\n<td>Pravesh<\/td>\n<td>Male<\/td>\n<td>4993599904<\/td>\n<td>Indore<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Here is a table of students from where we want to retrieve distinct information For example: distinct home-town.<\/p>\n<div class=\"codeblock\">\n<div class=\"dp-highlighter\">\n<div class=\"bar\">\n<div class=\"tools\"><\/div>\n<\/div>\n<ol class=\"dp-sql\" start=\"1\">\n<li class=\"alt\"><span class=\"keyword\">SELECT<\/span>\u00a0<span class=\"keyword\">DISTINCT<\/span>\u00a0home_town<\/li>\n<li class=\"\"><span class=\"keyword\">FROM<\/span>\u00a0students<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<p>Now, it will return two rows.<\/p>\n<table class=\"alt\">\n<tbody>\n<tr>\n<th>HOME_TOWN<\/th>\n<\/tr>\n<tr>\n<td>Indore<\/td>\n<\/tr>\n<tr>\n<td>Mumbai<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0SQL DISTINCT command\u00a0is used with SELECT key word to retrieve only distinct or unique data. In a table, there may [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}