{"id":5796,"date":"2021-04-05T13:03:02","date_gmt":"2021-04-05T13:03:02","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=5796"},"modified":"2021-04-05T13:03:02","modified_gmt":"2021-04-05T13:03:02","slug":"sql-queries","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/","title":{"rendered":"SQL Queries"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>\u00a0\ud83d\udc49\u00a0 To give a table, or a column in a table, a temporary name we can use Alias:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: <strong>SELECT \"column_name\" AS \"column_Alias\" FROM \"table_name\";<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example we have a table with column_name as &#8220;<strong>SNo&#8221; <\/strong>, &#8220;<strong>Country<\/strong>&#8221; and &#8220;<strong>Capital<\/strong>&#8221; and we want to update it as &#8220;<strong>Serial Number<\/strong>&#8221; , &#8220;<strong>State<\/strong>&#8221; and &#8220;<strong>Country_Capital <\/strong>then we will run the following command:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Joydeep-cotocus\/f6ede3b49a0364ccf16604490c48a399.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\ud83d\udc49 To count the number of rows in the table:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: <strong>SELECT COUNT(column_name) AS (alias_name) FROM table_name;<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example we want to count number of orders placed by a particular customer from customer_table:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Joydeep-cotocus\/0436d5c8365a64c8e58995edd2419abd.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\">This will show the count of numbers of order places by the customer id &#8220;CG-1234&#8242; and the products ordered.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\ud83d\udc49 To add the values in a columns we use Sum command:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: <strong>SELECT SUM (column_name) FROM table_name;<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example we want to add the profit amount from the sales table:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Joydeep-cotocus\/e9e4a806d7e4275e8f2041fea7b98285.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\ud83d\udc49 To find out an average for a column in the table:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: <strong>SELECT AVG (column_name) FROM table_name;<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example we need to find the average age of the customers from customer table:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Joydeep-cotocus\/0fcdd7adfda8ba89bca71e3066372c93.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\ud83d\udc49To find the minimum and the maximum value in a table:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Minimum<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>SELECT MIN (column_name) FROM table_name;<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example we want to find the minimum order sale made for a product from sales table:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Joydeep-cotocus\/b6e7bf0b41b16fab3cd213d4eab8f634.js\"><\/script>\n\n\n\n<ul class=\"wp-block-list\"><li>Maximum<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>SELECT MAX (column_name) FROM table_name;<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example we want to find the maximum order sale made for a product from sales table:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Joydeep-cotocus\/1015c106530bf6c170620e7d74c34284.js\"><\/script>\n\n\n\n<p class=\"has-text-align-left wp-block-paragraph\"><strong>\ud83d\udc49 To groups rows that have the same values into summary rows, like &#8220;find the number of customers in each region&#8221; we use GROUP BY statement. The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: <strong>SELECT \"column_name\",\"function-type\" (column_name) FROM table_name Group By \"column-name\";<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example we need to find the number orders places in each region in the table:<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Joydeep-cotocus\/4410047ac5955b5e7d7992b07508ca15.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\ud83d\udc49<\/strong> <strong>To make conditions for aggregate functions we use HAVING clause. The difference between Having clause and Where clause is that we cannot make conditions in aggregate functions whereas we can make conditions in aggregate functions using Having clause.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: <strong>SELECT column_name, AGGREGATE FUNCTION column_name FROM tables GROUP BY (column1) HAVING condition;<\/strong><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example we have some set of customers in four regions and we want to see in which region the count of customers is more than 200. In this scenario we will use the HAVING clause and set a condition of count more than 200.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Joydeep-cotocus\/a6e3f81522f57ff8686cdcb7c6534dbb.js\"><\/script>\n\n\n\n<p class=\"wp-block-paragraph\">This command will show the count of customers in the regions where it is more than 200.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\ud83d\udc49<\/strong> <strong>To go through a condition and return a value when the first condition is met (like an if-then-else statement) &#8220;CASE&#8221; statement is used. So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the\u00a0<code>ELSE<\/code>\u00a0clause.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Syntax: <strong>SELECT * CASE WHEN condition THEN result ELSE result END;<\/strong><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0\ud83d\udc49\u00a0 To give a table, or a column in a table, a temporary name we can use Alias: For example [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[72],"tags":[5193,4388,5191,5192,5194,5195,421,284],"class_list":["post-5796","post","type-post","status-publish","format-standard","hentry","category-database","tag-alias","tag-case","tag-count","tag-having","tag-maximum","tag-minimum","tag-sql","tag-update"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Queries - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Queries - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"\u00a0\ud83d\udc49\u00a0 To give a table, or a column in a table, a temporary name we can use Alias: For example [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-05T13:03:02+00:00\" \/>\n<meta name=\"author\" content=\"Joydeep M\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Joydeep M\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/\"},\"author\":{\"name\":\"Joydeep M\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/8a2c0533775223a96dd307662111bac6\"},\"headline\":\"SQL Queries\",\"datePublished\":\"2021-04-05T13:03:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/\"},\"wordCount\":406,\"commentCount\":0,\"keywords\":[\"alias\",\"Case\",\"count\",\"having\",\"maximum\",\"minimum\",\"sql\",\"Update\"],\"articleSection\":[\"Database\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/\",\"name\":\"SQL Queries - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"datePublished\":\"2021-04-05T13:03:02+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/8a2c0533775223a96dd307662111bac6\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/sql-queries\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Queries\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/8a2c0533775223a96dd307662111bac6\",\"name\":\"Joydeep M\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fb490db5d492c960456cc628f9e832fda72c5bc426cd8a13b18cbb95bca7ce99?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fb490db5d492c960456cc628f9e832fda72c5bc426cd8a13b18cbb95bca7ce99?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/fb490db5d492c960456cc628f9e832fda72c5bc426cd8a13b18cbb95bca7ce99?s=96&d=mm&r=g\",\"caption\":\"Joydeep M\"},\"description\":\"An investment in knowledge pays the best interest.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/joydeep-kumar-538412208\\\/\"],\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/joydeep\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Queries - scmGalaxy","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/","og_locale":"en_US","og_type":"article","og_title":"SQL Queries - scmGalaxy","og_description":"\u00a0\ud83d\udc49\u00a0 To give a table, or a column in a table, a temporary name we can use Alias: For example [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/","og_site_name":"scmGalaxy","article_published_time":"2021-04-05T13:03:02+00:00","author":"Joydeep M","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Joydeep M","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/"},"author":{"name":"Joydeep M","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/8a2c0533775223a96dd307662111bac6"},"headline":"SQL Queries","datePublished":"2021-04-05T13:03:02+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/"},"wordCount":406,"commentCount":0,"keywords":["alias","Case","count","having","maximum","minimum","sql","Update"],"articleSection":["Database"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/","name":"SQL Queries - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"datePublished":"2021-04-05T13:03:02+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/8a2c0533775223a96dd307662111bac6"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/sql-queries\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"SQL Queries"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/8a2c0533775223a96dd307662111bac6","name":"Joydeep M","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/fb490db5d492c960456cc628f9e832fda72c5bc426cd8a13b18cbb95bca7ce99?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/fb490db5d492c960456cc628f9e832fda72c5bc426cd8a13b18cbb95bca7ce99?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fb490db5d492c960456cc628f9e832fda72c5bc426cd8a13b18cbb95bca7ce99?s=96&d=mm&r=g","caption":"Joydeep M"},"description":"An investment in knowledge pays the best interest.","sameAs":["https:\/\/www.linkedin.com\/in\/joydeep-kumar-538412208\/"],"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/joydeep\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/5796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=5796"}],"version-history":[{"count":8,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/5796\/revisions"}],"predecessor-version":[{"id":5804,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/5796\/revisions\/5804"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=5796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=5796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=5796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}