{"id":32,"date":"2015-09-14T20:04:11","date_gmt":"2015-09-14T19:04:11","guid":{"rendered":"https:\/\/sqldoubleg.live-website.com\/?p=32"},"modified":"2015-11-02T10:16:19","modified_gmt":"2015-11-02T10:16:19","slug":"day-1-adventureworks-some-basics-about-select-joins-and-more","status":"publish","type":"post","link":"https:\/\/www.sqldoubleg.com\/es\/2015\/09\/14\/day-1-adventureworks-some-basics-about-select-joins-and-more\/","title":{"rendered":"Day 1 @ AdventureWorks, some basics about SELECT, JOINS and more"},"content":{"rendered":"<p>Day 1 of exercises using the AdventureWorks sample database, the SELECT statement&nbsp;The first day of this journey we&#8217;ll work on the most basic statement\u00a0I can think of, the SELECT statement.<\/p>\n<p>Exercises are growing in difficulty, from the most basic to others\u00a0a bit more complicated, where you&#8217;d get data from more than on table in the database or different clauses might need to be applied to obtain the desired results.<\/p>\n<p>The exercises are as follows, in the post with the answers I&#8217;ll try to provide a reasonable explanation for each query along with some useful resources for a better understanding.<\/p>\n<p>You can copy this into your SSMS (SQL Server Management Studio) using the sample database AdventureWorks, I&#8217;ve used the current version (2014), but most likely you can try in earlier versions without problem.<\/p>\n<pre class=\"brush: tsql; title: ; notranslate\" title=\"\">\r\n\r\n--==================================================\r\n-- SELECT Statements\r\n--==================================================\r\n\r\n--01============================================================================================================\r\n-- Retrieve a list of the different types of contacts that can exist in the database\r\n-- Output:\r\n-- - ContactTypeID\r\n-- - Name\r\n\r\n--02============================================================================================================\r\n-- Retrieve (ContactTypeID, Name) for the contact type 'Marketing Manager'\r\n-- Output:\r\n-- - ContactTypeID\r\n-- - Name\r\n\r\n\r\n--03============================================================================================================\r\n-- Retrieve a list of the different types of contacts which are managers\r\n-- Output:\r\n-- - ContactTypeID\r\n-- - Name\r\n\r\n\r\n--04============================================================================================================\r\n-- Retrieve a list of the different types of contacts which are managers\r\n-- Output:\r\n-- - ContactTypeID\r\n-- - Name\r\n-- Sorted by\r\n-- - Alphabetically Descending\r\n\r\n\r\n--05============================================================================================================\r\n-- Retrieve a list of all contacts which are 'Purchasing Manager'\r\n-- Output:\r\n-- - BusinessEntityID\r\n-- - LastName\r\n-- - FirstName\r\n-- - MiddleName\r\n--\r\n-- Sorted by:\r\n-- - LastName ascending\r\n-- - FirstName ascending\r\n-- - MiddleName ascending\r\n\r\n\r\n--06============================================================================================================\r\n-- Retrieve a list of all contacts which are 'Purchasing Manager'\r\n-- Output:\r\n-- - BusinessEntityID\r\n-- - LastName\r\n-- - FirstName\r\n-- - MiddleName, if there is no MiddleName, to display '' (empty string) instead of NULL\r\n--\r\n-- Sorted by:\r\n-- - LastName ascending\r\n-- - FirstName ascending\r\n-- - MiddleName ascending\r\n\r\n\r\n--07============================================================================================================\r\n-- Retrieve a list of the different types of contacts and how many of them exist in the database\r\n-- Output:\r\n-- - ContactTypeID\r\n-- - ContactTypeName\r\n-- - N_contacts\r\n--\r\n\r\n\r\n--08============================================================================================================\r\n-- Retrieve a list of the different types of contacts and how many of them exist in the database\r\n-- Output:\r\n-- - ContactTypeID\r\n-- - ContactTypeName\r\n-- - N_contacts\r\n--\r\n-- Sorted by:\r\n-- - N_contacts descending\r\n\r\n\r\n--09============================================================================================================\r\n-- Retrieve a list of the different types of contacts and how many of them exist in the database\r\n-- Output:\r\n-- - ContactTypeID\r\n-- - ContactTypeName\r\n-- - N_contacts\r\n--\r\n-- Sorted by:\r\n-- - N_contacts descending\r\n-- Filter:\r\n-- - Only interested in ContactTypes that have 100 contacts or more.\r\n\r\n\r\n--10============================================================================================================\r\n--\r\n-- The table [HumanResources].[EmployeePayHistory] holds employees' current salary along with historical data\r\n--\r\n--\r\n-- List of employees and their Historical weekly salary (based on 40h a week)\r\n-- Output:\r\n-- - [RateChangeDate] from historical table aliased to DateFrom, in the format 'dd\/mm\/yyyy'\r\n-- - 1 column which includes 'LastName, FirstName MiddleName' called FullNamne\r\n-- - WeeklySalary which must be calculated too\r\n--\r\n-- Sorted by\r\n-- - FullName\r\n-- - DateFrom\r\n\r\n\r\n--============================================================================================================\r\n-- BONUS\r\n--============================================================================================================\r\n\r\n\r\n--============================================================================================================\r\n-- List of employees and their current weekly salary (based on 40h a week)\r\n-- Output:\r\n-- - [RateChangeDate] from historical table aliased to DateFrom, in the format 'dd\/mm\/yyyy'\r\n-- - 1 column which includes 'LastName, FirstName MiddleName' called FullNamne\r\n-- - WeeklySalary which must be calculated too\r\n--\r\n-- Sorted by\r\n-- - FullName\r\n-- - DateFrom\r\n--\r\n-- ONLY 1 line per Employee, the current one to be precise.\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Day 1 of exercises using the AdventureWorks sample database, the SELECT statement&nbsp;The first day of this journey we&#8217;ll work on the most basic statement\u00a0I can think of, the SELECT statement. Exercises are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[7,8,6,5],"_links":{"self":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts\/32"}],"collection":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":0,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqldoubleg.com\/es\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}