{"id":3089,"date":"2024-06-19T16:09:08","date_gmt":"2024-06-19T08:09:08","guid":{"rendered":"https:\/\/five.co\/?p=3089"},"modified":"2024-06-19T16:12:06","modified_gmt":"2024-06-19T08:12:06","slug":"select-distinct-sql","status":"publish","type":"post","link":"https:\/\/preview-php85.dzine.org\/five\/blog\/select-distinct-sql\/","title":{"rendered":"Select Distinct Rows in SQL"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Learn How to Select Distinct Rows in SQL<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Selecting distinct records in an SQL database through the <code>SELECT DISTINCT<\/code> statement is a helpful and frequently used database query. The <code>SELECT DISTINCT <\/code>statement returns only distinct values, which enables database administrators to get a quick overview of database records and generate insights from data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s learn how the <code>SELECT DISTINCT<\/code> statement works, and how to combine it with other frequently used statements such as <code>COUNT <\/code>or a <code>WHERE <\/code>statement. We will illustrate the<code> SELECT DISTINCT<\/code> statement with an easy-to-understand example.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<div class=\"root-eb-toc-zcadp wp-block-essential-blocks-table-of-contents\"><div class=\"eb-parent-wrapper eb-parent-eb-toc-zcadp \"><div class=\"eb-toc-container eb-toc-zcadp  eb-toc-is-not-sticky eb-toc-not-collapsible eb-toc-initially-not-collapsed eb-toc-scrollToTop style-1 list-style-none\" data-scroll-top=\"false\" data-scroll-top-icon=\"fas fa-angle-up\" data-collapsible=\"false\" data-sticky-hide-mobile=\"false\" data-sticky=\"false\" data-scroll-target=\"scroll_to_toc\" data-copy-link=\"false\" data-editor-type=\"\" data-hide-desktop=\"false\" data-hide-tab=\"false\" data-hide-mobile=\"false\" data-itemcollapsed=\"false\" data-highlight-scroll=\"false\"><div class=\"eb-toc-header\"><h2 class=\"eb-toc-title\">Table of Contents<\/h2><\/div><div class=\"eb-toc-wrapper \" data-headers=\"[{&quot;level&quot;:2,&quot;content&quot;:&quot;Learn How to Select Distinct Rows in SQL&quot;,&quot;text&quot;:&quot;Learn How to Select Distinct Rows in SQL&quot;,&quot;link&quot;:&quot;learn-how-to-select-distinct-rows-in-sql&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;SELECT DISTINCT Rows in SQL: Here&apos;s How&quot;,&quot;text&quot;:&quot;SELECT DISTINCT Rows in SQL: Here&apos;s How&quot;,&quot;link&quot;:&quot;select-distinct-rows-in-sql-heres-how&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;DISTINCT and ORDER BY&quot;,&quot;text&quot;:&quot;DISTINCT and ORDER BY&quot;,&quot;link&quot;:&quot;distinct-and-order-by&quot;},{&quot;level&quot;:3,&quot;content&quot;:&quot;Select Distinct: Adding a WHERE clause&quot;,&quot;text&quot;:&quot;Select Distinct: Adding a WHERE clause&quot;,&quot;link&quot;:&quot;select-distinct-adding-a-where-clause&quot;},{&quot;level&quot;:2,&quot;content&quot;:&quot;Working with SQL in Five&quot;,&quot;text&quot;:&quot;Working with SQL in Five&quot;,&quot;link&quot;:&quot;working-with-sql-in-five&quot;}]\" data-visible=\"[true,true,true,true,true,true]\" data-delete-headers=\"[{&quot;label&quot;:&quot;Learn How to Select Distinct Rows in SQL&quot;,&quot;value&quot;:&quot;learn-how-to-select-distinct-rows-in-sql&quot;,&quot;isDelete&quot;:true},{&quot;label&quot;:&quot;SELECT DISTINCT Rows in SQL: Here&apos;s How&quot;,&quot;value&quot;:&quot;select-distinct-rows-in-sql-heres-how&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;DISTINCT and ORDER BY&quot;,&quot;value&quot;:&quot;distinct-and-order-by&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Select Distinct: Adding a WHERE clause&quot;,&quot;value&quot;:&quot;select-distinct-adding-a-where-clause&quot;,&quot;isDelete&quot;:false},{&quot;label&quot;:&quot;Working with SQL in Five&quot;,&quot;value&quot;:&quot;working-with-sql-in-five&quot;,&quot;isDelete&quot;:false}]\" data-smooth=\"true\" data-top-offset=\"\"><div class=\"eb-toc__list-wrap\"><ul class=\"eb-toc__list\"><li><a href=\"#select-distinct-rows-in-sql-heres-how\">SELECT DISTINCT Rows in SQL: Here&#8217;s How<\/a><ul class=\"eb-toc__list\"><li><a href=\"#distinct-and-order-by\">DISTINCT and ORDER BY<\/a><li><a href=\"#select-distinct-adding-a-where-clause\">Select Distinct: Adding a WHERE clause<\/a><\/li><\/ul><li><a href=\"#working-with-sql-in-five\">Working with SQL in Five<\/a><\/ul><\/div><\/div><\/div><\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">SELECT DISTINCT Rows in SQL: Here&#8217;s How<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s design a database for global smartphone sales to understand better how selecting distinct records from a table can help us extract insights from our dataset.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Our database has five fields (and many more in reality):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GUID, which serves as the table\u2019s Primary Key. For the sake of simplicity, let\u2019s use simple numbers as the primary key.<\/li>\n\n\n\n<li>Brand<\/li>\n\n\n\n<li>Manufacturer<\/li>\n\n\n\n<li>Model<\/li>\n\n\n\n<li>Month<\/li>\n\n\n\n<li>Units Sold<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here is what our database table would look like with five records in it:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>GUID<\/strong><\/td><td><strong>Brand<\/strong><\/td><td><strong>Manufacturer<\/strong><\/td><td><strong>Model<\/strong><\/td><td><strong>Month<\/strong><\/td><td><strong>Units Sold<\/strong><\/td><\/tr><tr><td>1<\/td><td>Apple<\/td><td>Foxconn<\/td><td>iPhone X<\/td><td>January<\/td><td>500<\/td><\/tr><tr><td>2<\/td><td>Samsung<\/td><td>Samsung Electronics<\/td><td>S22<\/td><td>January<\/td><td>2000<\/td><\/tr><tr><td>3<\/td><td>Apple<\/td><td>Foxconn<\/td><td>iPhone 13 Pro<\/td><td>January<\/td><td>800<\/td><\/tr><tr><td>4<\/td><td>Xiaomi<\/td><td>Xiaomi<\/td><td>Mi 11<\/td><td>December<\/td><td>4500<\/td><\/tr><tr><td>5<\/td><td>Apple<\/td><td>Foxconn<\/td><td>iPhone 14 Plus<\/td><td>July<\/td><td>5000<\/td><\/tr><tr><td>6<\/td><td>Samsung<\/td><td>Samsung Electronics<\/td><td>A55<\/td><td>December<\/td><td>4500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say we want to find out which distinct brands are in this dataset. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By writing a <code>SELECT DISTINCT <\/code>statement, we can use SQL to return only distinct brands from the &#8220;Brand&#8221; column.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DISTINCT\n  (`SmartphoneSale`.`Brand`)\nFROM\n  `SmartphoneSale`\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT DISTINCT<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  (<\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #98C379\">`Brand`<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what our query returns:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img width=\"1022\" height=\"1004\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-7.png\" alt=\"Five.Co - SQL Select Distinct Statement written in Five\" class=\"wp-image-3091\" style=\"width:510px;height:auto\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-7.png 1022w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-7-300x295.png 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-7-768x754.png 768w\" sizes=\"(max-width: 1022px) 100vw, 1022px\" \/><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Samsung, Xiaomi, and Apple are the distinct brands in our dataset. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The order of our results seems rather random: we would like these to be ordered alphabetically, so let&#8217;s add a few more conditions to our SQL statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">DISTINCT and ORDER BY<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By adding an <code>ORDER BY<\/code> to our SQL statement, we can easily adjust the order of our results.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT DISTINCT\n  (`SmartphoneSale`.`Brand`)\nFROM\n  `SmartphoneSale`\nORDER BY\n  (`SmartphoneSale`.`Brand`) ASC\n\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT DISTINCT<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  (<\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #98C379\">`Brand`<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">ORDER BY<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  (<\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #98C379\">`Brand`<\/span><span style=\"color: #ABB2BF\">) <\/span><span style=\"color: #C678DD\">ASC<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Now our results look like this: the three brands are listed alphabetically. Much better!<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img width=\"848\" height=\"772\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-8.png\" alt=\"Five.Co - SELECT DISTINCT and ORDER BY in SQL\" class=\"wp-image-3092\" style=\"width:431px;height:auto\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-8.png 848w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-8-300x273.png 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-8-768x699.png 768w\" sizes=\"(max-width: 848px) 100vw, 848px\" \/><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s take this one step further. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s say we only want to see those brands that sold smartphones in January. If you look at our dataset, you will see that Xiaomi (mysteriously) did not sell any smartphones in January, but Apple and Samsung did. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Select Distinct: Adding a WHERE clause<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To return the distinct brands that sold smartphones in January, we adjusted our query like so:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#abb2bf;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282c34\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"SELECT\n  `SmartphoneSale`.`Month`,\n  `SmartphoneSale`.`Brand`\nFROM\n  `SmartphoneSale`\nWHERE\n  (`SmartphoneSale`.`Month`=&quot;January&quot;)\nORDER BY\n  `SmartphoneSale`.`Brand` ASC\" style=\"color:#abb2bf;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki one-dark-pro\" style=\"background-color: #282c34\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #C678DD\">SELECT<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #98C379\">`Month`<\/span><span style=\"color: #ABB2BF\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #98C379\">`Brand`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">FROM<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">WHERE<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  (<\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #98C379\">`Month`<\/span><span style=\"color: #56B6C2\">=<\/span><span style=\"color: #98C379\">&quot;January&quot;<\/span><span style=\"color: #ABB2BF\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #C678DD\">ORDER BY<\/span><\/span>\n<span class=\"line\"><span style=\"color: #ABB2BF\">  <\/span><span style=\"color: #98C379\">`SmartphoneSale`<\/span><span style=\"color: #ABB2BF\">.<\/span><span style=\"color: #98C379\">`Brand`<\/span><span style=\"color: #ABB2BF\"> <\/span><span style=\"color: #C678DD\">ASC<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what the query returns:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img width=\"1024\" height=\"519\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-9-1024x519.png\" alt=\"Five.Co - SELECT DISTINCT in SQL With a WHERE and ORDER BY \" class=\"wp-image-3093\" style=\"width:722px;height:auto\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-9-1024x519.png 1024w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-9-300x152.png 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-9-768x389.png 768w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-9.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Perfect: the query says that only Apple and Samsung sold smartphones in January. By looking at the original dataset, we can confirm that this is true (this is a luxury you won&#8217;t have once you deal with larger datasets).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Working with SQL in Five<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Five is a rapid application development environment that lets you create and query a MySQL database from scratch. In addition, you can use Five to build a responsive, login-protected web application on top of your database. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Five gives you point-and-click tools for creating forms, charts, data views, or <a href=\"https:\/\/preview-php85.dzine.org\/five\/blog\/generate-mysql-pdf-report\/\">PDF reports<\/a> that visualize data stored in your SQL database. Five is the fastest way to go from database to web app.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img width=\"1024\" height=\"441\" src=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-10-1024x441.png\" alt=\"\" class=\"wp-image-3095\" srcset=\"https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-10-1024x441.png 1024w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-10-300x129.png 300w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-10-768x330.png 768w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-10-1536x661.png 1536w, https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/image-10.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To get started with Five, <a href=\"https:\/\/preview-php85.dzine.org\/five\/get-started\">sign up for a free trial<\/a> and follow one of our code-along tutorials.<\/p>\n\n\n\n<div style=\"background-color: #001524;\"><hr style=\"height: 5px;\">\n<pre style=\"text-align: center; overflow: hidden; white-space: pre-line;\"><span style=\"color: #f1ebda; background-color: #4588d8; font-size: calc(18px + 0.390625vw);\"><strong>Go From SQL Database to Web App<\/strong><br><span style=\"font-size: 14pt;\">Start Developing<\/span><\/span><\/pre>\n<p style=\"text-align: center;\"><a href=\"https:\/\/preview-php85.dzine.org\/five\/get-started\" target=\"_blank\" rel=\"noopener\"><button style=\"background-color: #f8b92b; border: none; color: black; padding: 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 18px; cursor: pointer; margin: 4px 2px; border-radius: 5px;\"><strong>Get Instant Access<\/strong><\/button><br><\/a><\/p>\n<hr style=\"height: 5px;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Learn How to Select Distinct Rows in SQL Selecting distinct records in an SQL database through the SELECT DISTINCT statement is a helpful and frequently used database query. The SELECT DISTINCT statement returns only distinct values, which enables database administrators to get a quick overview of database records and generate insights from data. Let&#8217;s learn [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3094,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_eb_attr":"","_uag_custom_page_level_css":"","footnotes":""},"categories":[21],"tags":[],"class_list":["post-3089","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog"],"acf":[],"uagb_featured_image_src":{"full":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/Five.Co-SELECT-DISTINCT-SQL.png",960,540,false],"thumbnail":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/Five.Co-SELECT-DISTINCT-SQL-150x150.png",150,150,true],"medium":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/Five.Co-SELECT-DISTINCT-SQL-300x169.png",300,169,true],"medium_large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/Five.Co-SELECT-DISTINCT-SQL-768x432.png",768,432,true],"large":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/Five.Co-SELECT-DISTINCT-SQL.png",960,540,false],"1536x1536":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/Five.Co-SELECT-DISTINCT-SQL.png",960,540,false],"2048x2048":["https:\/\/preview-php85.dzine.org\/five\/wp-content\/uploads\/2024\/06\/Five.Co-SELECT-DISTINCT-SQL.png",960,540,false]},"uagb_author_info":{"display_name":"Dominik Keller","author_link":"https:\/\/preview-php85.dzine.org\/five\/author\/f1v5_ed3_my\/"},"uagb_comment_info":0,"uagb_excerpt":"Learn How to Select Distinct Rows in SQL Selecting distinct records in an SQL database through the SELECT DISTINCT statement is a helpful and frequently used database query. The SELECT DISTINCT statement returns only distinct values, which enables database administrators to get a quick overview of database records and generate insights from data. Let&#8217;s learn&hellip;","_links":{"self":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/3089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/comments?post=3089"}],"version-history":[{"count":0,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/posts\/3089\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media\/3094"}],"wp:attachment":[{"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/media?parent=3089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/categories?post=3089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/preview-php85.dzine.org\/five\/wp-json\/wp\/v2\/tags?post=3089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}