{"id":16086,"date":"2020-05-19T09:18:58","date_gmt":"2020-05-19T09:18:58","guid":{"rendered":"https:\/\/ittutorial.org\/?p=16086"},"modified":"2020-05-21T22:53:43","modified_gmt":"2020-05-21T22:53:43","slug":"select-distinct-statement-in-oracle-sql-oracle-sql-tutorials-3","status":"publish","type":"post","link":"https:\/\/ittutorial.org\/select-distinct-statement-in-oracle-sql-oracle-sql-tutorials-3\/","title":{"rendered":"SELECT DISTINCT Statement in Oracle SQL | Oracle SQL Tutorials -3"},"content":{"rendered":"<p align=\"left\">Hi,<\/p>\n<p align=\"left\">I will explain SELECT DISTINCT Statement in Oracle SQL in this post of Oracle SQL Tutorial series.<\/p>\n<p align=\"left\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-16079\" src=\"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/05\/oracle-sql-tutorial.png\" alt=\"\" width=\"387\" height=\"311\" srcset=\"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/05\/oracle-sql-tutorial.png 387w, https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/05\/oracle-sql-tutorial-300x241.png 300w\" sizes=\"auto, (max-width: 387px) 100vw, 387px\" \/><\/p>\n<p align=\"left\"><!--more--><\/p>\n<p align=\"left\">\n<p align=\"left\">\n<p align=\"left\"><span style=\"color: #ff0000;\"><strong>Read the previous post of this tutorial series before this.<\/strong><\/span><\/p>\n<p>https:\/\/ittutorial.org\/sql-select-statement-and-desc-command-in-oracle-sql-oracle-sql-tutorials-1\/<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h5><span style=\"color: #ff0000;\">SELECT DISTINCT Statement<\/span><\/h5>\n<p>SELECT DISTINCT statement is used to query only different or unique values.<\/p>\n<p>If there are lots of duplicate values in a table, when you want to display only different (distinct) values, then you should use DISTINCT statement.<\/p>\n<p>&nbsp;<\/p>\n<h5><span style=\"color: #ff0000;\">SELECT DISTINCT Syntax<\/span><\/h5>\n<div class=\"w3-example\">\n<pre class=\"w3-code notranslate sqlHigh\"><span class=\"sqlcolor\"><span class=\"sqlkeywordcolor\">SELECT<\/span>\u00a0<span class=\"sqlkeywordcolor\">DISTINCT<\/span>\u00a0<em>column1<\/em>,<em>\u00a0column2, column3... <\/em><span class=\"sqlkeywordcolor\">FROM<\/span>\u00a0<em>table_name<\/em>;<\/span><\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>When you query the location id of hr.departmens table, you can list the multiple duplicate values in this table without using DISTINCT.<\/p>\n<p>&nbsp;<\/p>\n<pre>SQL&gt; SELECT LOCATION_ID FROM HR.DEPARTMENTS;\r\n\r\nLOCATION_ID\r\n-----------\r\n1700\r\n1800\r\n1700\r\n2400\r\n1500\r\n1400\r\n2700\r\n2500\r\n1700\r\n1700\r\n1700\r\n\r\nLOCATION_ID\r\n-----------\r\n1700\r\n1700\r\n1700\r\n1700\r\n1700\r\n1700\r\n1700\r\n1700\r\n1700\r\n1700\r\n1700\r\n\r\nLOCATION_ID\r\n-----------\r\n1700\r\n1700\r\n1700\r\n1700\r\n1700\r\n\r\n27 rows selected.\r\n\r\nSQL&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>But If you use DISTINCT, then you can display only unique or distinct values as follows.<\/p>\n<pre>SQL&gt; SELECT DISTINCT LOCATION_ID FROM HR.DEPARTMENTS;\r\n\r\nLOCATION_ID\r\n-----------\r\n1800\r\n2400\r\n1400\r\n2500\r\n1700\r\n2700\r\n1500\r\n\r\n7 rows selected.\r\n\r\nSQL&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>You can use DISTINCT for multiple columns as follows.<\/p>\n<p>&nbsp;<\/p>\n<pre>SQL&gt; SELECT DISTINCT DEPARTMENT_NAME,LOCATION_ID FROM HR.DEPARTMENTS;\r\n\r\nDEPARTMENT_NAME                LOCATION_ID\r\n------------------------------ -----------\r\nAdministration                        1700\r\nMarketing                             1800\r\nShareholder Services                  1700\r\nOperations                            1700\r\nSales                                 2500\r\nControl And Credit                    1700\r\nIT Helpdesk                           1700\r\nPurchasing                            1700\r\nFinance                               1700\r\nCorporate Tax                         1700\r\nManufacturing                         1700\r\n\r\nDEPARTMENT_NAME                LOCATION_ID\r\n------------------------------ -----------\r\nContracting                           1700\r\nNOC                                   1700\r\nIT                                    1400\r\nTreasury                              1700\r\nGovernment Sales                      1700\r\nExecutive                             1700\r\nConstruction                          1700\r\nPayroll                               1700\r\nShipping                              1500\r\nAccounting                            1700\r\nBenefits                              1700\r\n\r\nDEPARTMENT_NAME                LOCATION_ID\r\n------------------------------ -----------\r\nRetail Sales                          1700\r\nHuman Resources                       2400\r\nPublic Relations                      2700\r\nIT Support                            1700\r\nRecruiting                            1700\r\n\r\n27 rows selected.\r\n\r\nSQL&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #ff0000;\">DISTINCT operator causes sorting which slows down the SQL execution time.<\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>If it is possible, it is better that remove Distinct clause in Select for the SQL Query performance.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h4><span style=\"color: #ff0000;\">Do you want to learn Oracle Database for Beginners, then read the following articles.<\/span><\/h4>\n<blockquote class=\"wp-embedded-content\" data-secret=\"TszfDpaBmd\"><p><a href=\"https:\/\/ittutorial.org\/oracle-tutorial-oracle-database-tutorials-for-beginners-junior-oracle-dba\/\">Oracle Tutorial | Oracle Database Tutorials for Beginners  ( Junior Oracle DBA )<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Oracle Tutorial | Oracle Database Tutorials for Beginners  ( Junior Oracle DBA )&#8221; &#8212; IT Tutorial\" src=\"https:\/\/ittutorial.org\/oracle-tutorial-oracle-database-tutorials-for-beginners-junior-oracle-dba\/embed\/#?secret=epQmoHZIe5#?secret=TszfDpaBmd\" data-secret=\"TszfDpaBmd\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi, I will explain SELECT DISTINCT Statement in Oracle SQL in this post of Oracle SQL Tutorial series.<\/p>\n","protected":false},"author":1,"featured_media":16079,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3019],"tags":[11376,3013,11370,11371,11372,11373,11374,11378,11375,11377],"class_list":["post-16086","post","type-post","status-publish","format-standard","has-post-thumbnail","","category-oracle-sql","tag-mysql-select-distinct-on-one-column","tag-oracle-sql-tutorials","tag-select-distinct-count","tag-select-distinct-multiple-columns","tag-select-distinct-on-one-column","tag-select-distinct-on-one-column-with-multiple-columns-returned","tag-select-distinct-oracle","tag-select-distinct-statement-in-oracle-sql","tag-sql-select-distinct-values-and-count-of-each","tag-sql-select-unique-values-from-multiple-columns"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/ittutorial.org\/wp-content\/uploads\/2020\/05\/oracle-sql-tutorial.png","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/16086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/comments?post=16086"}],"version-history":[{"count":2,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/16086\/revisions"}],"predecessor-version":[{"id":16091,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/posts\/16086\/revisions\/16091"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/media\/16079"}],"wp:attachment":[{"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/media?parent=16086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/categories?post=16086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ittutorial.org\/wp-json\/wp\/v2\/tags?post=16086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}