{"id":2620,"date":"2024-05-01T11:36:31","date_gmt":"2024-05-01T15:36:31","guid":{"rendered":"http:\/\/andrewpallant.ca\/wordpress\/?p=2620"},"modified":"2024-05-01T11:46:26","modified_gmt":"2024-05-01T15:46:26","slug":"sql-basics","status":"publish","type":"post","link":"https:\/\/andrewpallant.ca\/wordpress\/sql-basics\/","title":{"rendered":"SQL Basics"},"content":{"rendered":"\n<p>Today, I am writing this blog because I did not have a chance to conduct the SQL training with the support team yesterday before I departed for a new adventure; I feel like I owe it to them to give them something.  This is part one of the training session.   This part is about SQL basics.   Part two is about using patterns to gain efficiencies in your day-to-day work.   Let&#8217;s start with part one: <strong>SQL Basics<\/strong>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>The <strong>SELECT<\/strong> statement is probably the most used in the support team.   It is used whenever looking up a record to verify the data or simply doing ad-hoc reports.   The simplest statement is what I would call the wild card statement.   You would use this when you don&#8217;t know the data table structure or if you want quick, natural results.    The wild card is represented by an asterisk (<strong>*<\/strong>).   You would protect the consumption of data and the memory used by limiting your results using the <strong>TOP<\/strong> clause.   To filter your results, you will use the <strong>WHERE<\/strong> clause.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"color: #C40C0C\">SELECT TOP<\/span> 2 * <span style=\"color: #C40C0C\">FROM<\/span> [TABLE1] <span style=\"color: #C40C0C\">WHERE<\/span> &#91;Name] <span style=\"color: #C40C0C\">LIKE<\/span> '%Andrew%'   <span style=\"color: #0A6847\">\n\n-- This will look filter a result of rows that contain a column &#91;Name] that contains the name Andrew and limit the results to two rows.<\/span><\/code><\/pre>\n<br\/>\n<br\/>\n<pre class=\"wp-block-code\"><code><span style=\"color: #C40C0C\">SELECT TOP<\/span> 3 * <span style=\"color: #C40C0C\">FROM<\/span>  TABLE1 <span style=\"color: #C40C0C\">WHERE<\/span> &#91;Name] <span style=\"color: #C40C0C\">=<\/span> 'Andrew'   <span style=\"color: #0A6847\">\n\n-- This will look filter a result of rows that has a column &#91;Name] that equals the name Andrew and limit it to three rows.<\/span><\/code><\/pre>\n\n\n\n\n<p>Let&#8217;s say we wanted to get the results where one table has results, and we want to show that a second table doesn&#8217;t contain.   This was a recent customer request.  We are going to use two queries.   The inner query or the last query will be the positive in this scenario, and the outer query will be the negative.  We are going to introduce the <strong>NOT IN<\/strong> statement.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"color: red\">SELECT<\/span> * <span style=\"color: #C40C0C\">FROM<\/span>&nbsp;[TABLE1] <span style=\"color: #C40C0C\">WHERE<\/span>&nbsp;&#91;CreatedBy]&nbsp;<span style=\"color: red\">NOT IN<\/span>&nbsp;(<span style=\"color: red\">SELECT<\/span> [CreatedBy] <span style=\"color: #C40C0C\">FROM<\/span> [TABLE2] <span style=\"color: #C40C0C\">WHERE<\/span> &#91;Name] = 'Andrew') \n\n<span style=\"color:#0A6847;\">-- This query will grab all the rows with the name Andrew and give you all the rows in Table 1 that do not have rows created by Andrew<span><\/code><\/pre>\n\n\n\n<p>Now for the second most commonly used SQL statement in the support team: <strong>INSERT<\/strong>.  I would recommend learning the table schema by either viewing it in Microsoft Azure Data Studio or SQL Management Studio.    Find the database and expand it so you can see the Tables node in the tree view of the database.   Find the table or tables and expand that to view the column names.   You can also use the <strong>SELECT<\/strong> clauses from the above and have it output the data with the columns.<\/p>\n\n\n\n<p>The construction of the INSERT statement is as of such<\/p><br\/>\n<p>INSERT ( COLUMN(s) names INTO [TABLE1] VALUES ( value representing the columns in the first set of brackets))<\/p>\n<br\/>\n<pre class=\"wp-block-code\"><code><span style=\"color: #C40C0C\">INSERT <\/span> ([FIRSTNAME],[LASTNAME], [AGE])  <span style=\"color: #C40C0C\">INTO<\/span> [TABLE1] <span style=\"color: #C40C0C\">VALUES<\/span>('Someone', 'SMITH', 42)<span style=\"color: #0A6847\">-- This will insert a record with the first name <i>Someone<\/i>, last name <i>smith<\/i> and age <i>42<\/i><\/span><\/code><\/pre>\n<br\/><br\/>\n<p>Now assume we want to add a record to Table1 and it has an index column that we want to use in Table2 to establish a relationship. We are going to store the created identity from the first insert so that we can use it for subsequent inserts<\/p><br\/>\n\n<pre class=\"wp-block-code\"><code>\n<span style=\"color: #C40C0C\">DECLARE<\/span> @LinkForTableTwoID <span style=\"color: #C40C0C\">AS UniqueIdentifier<span> <span style=\"color:#0A6847;\">-- This Assumes the Generated ID from Table1 will be a UniqueIdentifier<\/span>\n\n<span style=\"color: #C40C0C\">INSERT <\/span> ([FIRSTNAME], [LASTNAME], [AGE])  <span style=\"color: #C40C0C\">INTO<\/span> [TABLE1] <span style=\"color: #C40C0C\">VALUES<\/span>('Someone', 'SMITH', 42)\n\nSET @LinkForTableTwoID = SCOPE_IDENTITY() <span style=\"color: #0A6847\">-- get the freshly generated identity for future use <\/span>\n\n<span style=\"color: #C40C0C\">INSERT <\/span> ([Address1], [Address2], [City], [AddressType], [Table1ID]) <span style=\"color: #C40C0C\">INTO<\/span> [TABLE2] <span style=\"color: #C40C0C\">VALUES<\/span>('22 ThatRoad RD', '', 'Toronto', 'Primary', @LinkForTableTwoID)\n<span style=\"color: #C40C0C\">INSERT <\/span> ([Address1], [Address2], [City], [AddressType],[Table1ID])  <span style=\"color: #C40C0C\">INTO<\/span> [TABLE2] <span style=\"color: #C40C0C\">VALUES<\/span>('1 SomeStreet Cres', 'Box 19712', 'Toronto', 'Mailing', @LinkForTableTwoID)\n<span style=\"color: #0A6847\">-- This will create a record with the name of a person and their age.   Then we will add two record2 to Table 2 linked to the first table by using a scope identity.<\/span>\n<\/code><\/pre>\n\n\n\n\n\n<p><strong>Recap Of Some Basics<\/strong><\/p>\n\n\n\n<p>Reviewing the current page, here are three questions and their answers in an FAQ format:<\/p>\n\n\n\n<p><strong>Q1: What is the purpose of the SELECT statement in SQL?<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>A1:<\/strong>\u00a0The SELECT statement is used to look up records, verify data, or generate ad-hoc reports.\u00a0It\u2019s the most commonly used statement by the support team.<\/li>\n<\/ul>\n\n\n\n<p><strong>Q2: How can you limit the results of a SQL query?<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>A2:<\/strong>\u00a0To limit the results of a query, you can use the TOP clause.\u00a0For example,\u00a0<code>SELECT TOP 2 * FROM [TABLE1]<\/code>\u00a0it will limit the results to two rows.<\/li>\n<\/ul>\n\n\n\n<p><strong>Q3: What is the process for inserting a record into an SQL table and linking it to another table?<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>A3:<\/strong>\u00a0To insert a record and link it to another table, you first insert the record into the first table and store the generated identity using\u00a0SCOPE_IDENTITY().  Then, use this identity to insert related records into the second table.<\/li>\n<\/ul>\n\n\n\n<p>Since this meeting was only going to be an hour long and consisted of two parts, I now pause for Part Two: Looking for Patterns for Work Efficiencies. As always, with any of my training, my door is still open and happy to take questions 1:1 either in the comments of my LinkedIn post or directly from those who know how to get a hold of me.<\/p>\n\n\n\n<p>Best of Luck!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I am writing this blog because I did not have a chance to conduct the SQL training with the support team yesterday before I departed for a new adventure; I feel like I owe it to them to give them something. This is part one of the training session. This part is about SQL &hellip; <a href=\"https:\/\/andrewpallant.ca\/wordpress\/sql-basics\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">SQL Basics<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,36,78,82,87],"tags":[400,283,246,398],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>SQL Basics - LDNDeveloper<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"SQL Basics - LDNDeveloper\" \/>\r\n<meta property=\"og:description\" content=\"Today, I am writing this blog because I did not have a chance to conduct the SQL training with the support team yesterday before I departed for a new adventure; I feel like I owe it to them to give them something. This is part one of the training session. This part is about SQL &hellip; Continue reading SQL Basics\" \/>\r\n<meta property=\"og:url\" content=\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/\" \/>\r\n<meta property=\"og:site_name\" content=\"LDNDeveloper\" \/>\r\n<meta property=\"article:published_time\" content=\"2024-05-01T15:36:31+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2024-05-01T15:46:26+00:00\" \/>\r\n<meta name=\"author\" content=\"ldnDeveloper\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:creator\" content=\"@LdnDeveloper\" \/>\r\n<meta name=\"twitter:site\" content=\"@LdnDeveloper\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ldnDeveloper\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/\"},\"author\":{\"name\":\"ldnDeveloper\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"headline\":\"SQL Basics\",\"datePublished\":\"2024-05-01T15:36:31+00:00\",\"dateModified\":\"2024-05-01T15:46:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/\"},\"wordCount\":628,\"publisher\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"keywords\":[\"Customer Support\",\"Software Development\",\"SQL\",\"Support Teams\"],\"articleSection\":[\"Deployment\",\"How To\",\"SQL\",\"Team\",\"Tips\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/\",\"url\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/\",\"name\":\"SQL Basics - LDNDeveloper\",\"isPartOf\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#website\"},\"datePublished\":\"2024-05-01T15:36:31+00:00\",\"dateModified\":\"2024-05-01T15:46:26+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/andrewpallant.ca\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Basics\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#website\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/\",\"name\":\"LDNDeveloper\",\"description\":\"Learning, Growing and Sharing.\",\"publisher\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/andrewpallant.ca\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\",\"name\":\"ldnDeveloper\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg\",\"contentUrl\":\"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg\",\"width\":512,\"height\":512,\"caption\":\"ldnDeveloper\"},\"logo\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\"},\"description\":\"Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http:\/\/www.unlatched.com\/#sthash.8DiTkpKy.dpuf\",\"sameAs\":[\"http:\/\/www.andrewpallant.ca\",\"https:\/\/x.com\/LdnDeveloper\"],\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/author\/ldndeveloper\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Basics - LDNDeveloper","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":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/","og_locale":"en_US","og_type":"article","og_title":"SQL Basics - LDNDeveloper","og_description":"Today, I am writing this blog because I did not have a chance to conduct the SQL training with the support team yesterday before I departed for a new adventure; I feel like I owe it to them to give them something. This is part one of the training session. This part is about SQL &hellip; Continue reading SQL Basics","og_url":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/","og_site_name":"LDNDeveloper","article_published_time":"2024-05-01T15:36:31+00:00","article_modified_time":"2024-05-01T15:46:26+00:00","author":"ldnDeveloper","twitter_card":"summary_large_image","twitter_creator":"@LdnDeveloper","twitter_site":"@LdnDeveloper","twitter_misc":{"Written by":"ldnDeveloper","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/#article","isPartOf":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/"},"author":{"name":"ldnDeveloper","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"headline":"SQL Basics","datePublished":"2024-05-01T15:36:31+00:00","dateModified":"2024-05-01T15:46:26+00:00","mainEntityOfPage":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/"},"wordCount":628,"publisher":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"keywords":["Customer Support","Software Development","SQL","Support Teams"],"articleSection":["Deployment","How To","SQL","Team","Tips"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/","url":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/","name":"SQL Basics - LDNDeveloper","isPartOf":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#website"},"datePublished":"2024-05-01T15:36:31+00:00","dateModified":"2024-05-01T15:46:26+00:00","breadcrumb":{"@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/andrewpallant.ca\/wordpress\/sql-basics\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/andrewpallant.ca\/wordpress\/"},{"@type":"ListItem","position":2,"name":"SQL Basics"}]},{"@type":"WebSite","@id":"https:\/\/andrewpallant.ca\/wordpress\/#website","url":"https:\/\/andrewpallant.ca\/wordpress\/","name":"LDNDeveloper","description":"Learning, Growing and Sharing.","publisher":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/andrewpallant.ca\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84","name":"ldnDeveloper","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg","contentUrl":"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg","width":512,"height":512,"caption":"ldnDeveloper"},"logo":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/"},"description":"Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http:\/\/www.unlatched.com\/#sthash.8DiTkpKy.dpuf","sameAs":["http:\/\/www.andrewpallant.ca","https:\/\/x.com\/LdnDeveloper"],"url":"https:\/\/andrewpallant.ca\/wordpress\/author\/ldndeveloper\/"}]}},"_links":{"self":[{"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/2620"}],"collection":[{"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=2620"}],"version-history":[{"count":6,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/2620\/revisions"}],"predecessor-version":[{"id":2627,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/2620\/revisions\/2627"}],"wp:attachment":[{"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=2620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=2620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=2620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}