{"id":718,"date":"2018-06-06T03:24:44","date_gmt":"2018-06-06T00:24:44","guid":{"rendered":"http:\/\/kusuaks7\/?p=323"},"modified":"2021-05-11T13:55:51","modified_gmt":"2021-05-11T13:55:51","slug":"know-sql-in-2018","status":"publish","type":"post","link":"https:\/\/www.experfy.com\/blog\/bigdata-cloud\/know-sql-in-2018\/","title":{"rendered":"know-sql-in-2018"},"content":{"rendered":"<p><strong><em>Ready to learn SQL? <a href=\"https:\/\/www.experfy.com\/training\/courses\">Browse courses<\/a> like &nbsp;<a href=\"https:\/\/www.experfy.com\/training\/courses\/apache-spark-sql\">Apache Spark SQL<\/a> developed by industry thought leaders and Experfy in Harvard Innovation Lab<\/em><\/strong><\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*P4nj9fJjSeJ9-c0rwSZqlg.png\" style=\"width: 650px; height: 200px;\" \/><\/p>\n<p style=\"text-align: center;\">Credit <a href=\"https:\/\/xkcd.com\/327\/\" rel=\"noopener\">xkcd<\/a><\/p>\n<section name=\"edc1\">\n<p id=\"f223\" name=\"f223\">As a technical recruiter I came across many positions where SQL was a critical skill. To be a Business Analyst, Data Scientist, Backend Developer, Marketing professional or UX Design Researcher understanding SQL queries can be fundamental to your work.<\/p>\n<p id=\"b913\" name=\"b913\">Let me back up.<strong>&nbsp;What is SQL anyway?&nbsp;<\/strong>SQL is a query language for talking to structured databases. Pretty much all databases that have tables and rows will accept SQL based queries. SQL has many flavors but the fundamentals stay the same. Professionals and amateurs alike use SQL to find, create, update and delete information from their sources of record. It can be used with a ton of different databases like MySQL, Postgres, SQL Server and Oracle. It powers the logic behind popular server-side frameworks like&nbsp;<a data-href=\"https:\/\/medium.com\/@connorleech\/php-laravel-ruby-on-rails-and-web-frameworks-32c1e50cea2d\" href=\"https:\/\/medium.com\/@connorleech\/php-laravel-ruby-on-rails-and-web-frameworks-32c1e50cea2d\" target=\"_blank\" rel=\"noopener noreferrer\">Ruby On Rails and Laravel<\/a>. If you want to find information associated with a particular account or query what buttons users click in your app there is a good chance SQL can help you out!<\/p>\n<blockquote id=\"c575\" name=\"c575\"><p><span style=\"font-size:16px;\"><strong>SQL: Pronounced &ldquo;S-Q-L&rdquo; or &ldquo;Sequel&rdquo;\u200a&mdash;\u200ayou&nbsp;choose.<\/strong><\/span><\/p><\/blockquote>\n<h4 id=\"0fde\" name=\"0fde\"><strong>SQL alternatives<\/strong><\/h4>\n<p id=\"0fa0\" name=\"0fa0\">Before we hop on the SQL train to Database Town I&rsquo;d like to acknowledge some alternatives. You can use ORMs to query databases. ORM stands for&nbsp;<strong>Object Relational Mapper<\/strong>, which is a fancy way of saying that you can write code in a programming language like PHP or Node.js that translates to SQL queries. Popular ORMs are Active Record for&nbsp;<strong>Ruby On Rails<\/strong>, Eloquent for&nbsp;<strong>Laravel<\/strong>&nbsp;and Sequelize for&nbsp;<strong>Node.js<\/strong>. All of these services allow you to write code that translates to SQL under the hood. SQL is important for building applications with these technologies.<\/p>\n<p id=\"950f\" name=\"950f\">There are many databases that do not use SQL, such as MongoDB and GraphQL. These are newer technologies and not as widely adopted as relational databases. Relational databases have been around a very long time and power the majority of data storage on the internet. To fully appreciate&nbsp;<strong>NoSQL technologies<\/strong>&nbsp;and the reasons they came about it&rsquo;s helpful to know how relational databases and SQL work.<\/p>\n<h4 id=\"2b16\" name=\"2b16\"><strong>Create a&nbsp;table<\/strong><\/h4>\n<p id=\"1071\" name=\"1071\">The first thing to know is that relational databases (such as MySQL, SQLite or PostgreSQL) are made up of tables. One database can hold many tables and each table consists of a particular category of record. For example, in an eCommerce site we might have tables for orders, users, transactions and products. In many web applications you&rsquo;ll create tables through&nbsp;<a data-href=\"https:\/\/laravel.com\/docs\/5.5\/migrations\" href=\"https:\/\/laravel.com\/docs\/5.5\/migrations\" rel=\"noopener nofollow noreferrer\" target=\"_blank\">migrations<\/a>, but it&rsquo;s still helpful to be able to read and write SQL create statements.<\/p>\n<div id=\"19de\" name=\"19de\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#E6E6FA;\">create table exercise_logs ( <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; id integer primary key autoincrement,&nbsp; <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; type text, <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; minutes integer,&nbsp; <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; calories integer,&nbsp; <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; heart_rate integer<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">);<\/span><\/span><\/div>\n<p name=\"7409\">&nbsp;<\/p>\n<p id=\"7409\" name=\"7409\">The above SQL statement creates a table called exercise_logs with five columns (id, type, minutes, calories and heart_rate). Each column has a specific&nbsp;<a data-href=\"http:\/\/www.cs.toronto.edu\/~nn\/csc309-20085\/guide\/pointbase\/docs\/html\/htmlfiles\/dev_datatypesandconversionsFIN.html\" href=\"http:\/\/www.cs.toronto.edu\/~nn\/csc309-20085\/guide\/pointbase\/docs\/html\/htmlfiles\/dev_datatypesandconversionsFIN.html\" rel=\"noopener nofollow noreferrer\" target=\"_blank\">data type<\/a>, such as integer or text.<\/p>\n<p id=\"a057\" name=\"a057\">On the second line of our create statement we specify that each new record will have a unique id, known as the table&rsquo;s primary key.<\/p>\n<p id=\"e0aa\" name=\"e0aa\">The structure and definition of a database&rsquo;s tables is known as the database&rsquo;s&nbsp;<strong>schema<\/strong>.<\/p>\n<h4 id=\"bc27\" name=\"bc27\"><strong>Insert data into a&nbsp;table<\/strong><\/h4>\n<p id=\"8821\" name=\"8821\">To add an exercise log to our table we can write a SQL insert statement.<\/p>\n<div id=\"43e3\" name=\"43e3\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#E6E6FA;\">insert into exercise_logs(type, minutes, calories, heart_rate) <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">values (&#39;biking&#39;, 30, 100, 110);<\/span><\/span><\/div>\n<p name=\"802e\">&nbsp;<\/p>\n<p id=\"802e\" name=\"802e\">In this example we specify the table name, exercise_logs and the columns for which we&rsquo;d like to insert data. After the values keyword we include the data to be inserted into the database.<\/p>\n<h3 id=\"65d4\" name=\"65d4\"><strong>Select statements<\/strong><\/h3>\n<p id=\"305c\" name=\"305c\">For many professions, querying data using SQL select statements is their primary&nbsp;<strong>bread and butter.<\/strong><\/p>\n<p id=\"ae4d\" name=\"ae4d\">To select&nbsp;<em>all records<\/em>&nbsp;from a database:<\/p>\n<div id=\"416b\" name=\"416b\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#E6E6FA;\">select * from exercise_logs;<\/span><\/span><\/div>\n<p id=\"e542\" name=\"e542\">To find all the activities a user engaged in and the total amount of calories they burned doing that activity you could write:<\/p>\n<div id=\"a709\" name=\"a709\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#E6E6FA;\">select type, sum(calories) as total_calories <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">from exercise_logs <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">group by type;<\/span><\/span><\/div>\n<p name=\"1ed3\">&nbsp;<\/p>\n<p id=\"1ed3\" name=\"1ed3\">You can do all kinds of funky stuff like determine the number of students had which letter grade.<\/p>\n<p id=\"e244\" name=\"e244\">You could group each of the exercises by heart rate zones.<\/p>\n<div id=\"1475\" name=\"1475\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#E6E6FA;\">\/* Group the count of exercises by each heart rate zone *\/<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">select count(*),&nbsp;&nbsp;&nbsp; <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; case&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp;&nbsp;&nbsp; when heart_rate &gt; 220 &#8211; 30 then &#39;above max&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp;&nbsp;&nbsp; when heart_rate &gt; round(.9 * (220 &#8211; 30)) then &#39;above target&#39;<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp;&nbsp;&nbsp; when heart_rate &gt; round(.5 * (220 &#8211; 30)) then &#39;within target&#39;<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp;&nbsp;&nbsp; else &#39;below target&#39;&nbsp;&nbsp;&nbsp; <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">end as &#39;heart_rate_zone&#39;<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">from exercise_logs<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">group by heart_rate_zone;<\/span><\/span><\/div>\n<p name=\"7627\">&nbsp;<\/p>\n<p id=\"7627\" name=\"7627\">SQL is very helpful for reporting purposes. It can be used to filter marketing results, find customer information, search server logs or create reporting dashboards.<\/p>\n<h4 id=\"0243\" name=\"0243\"><strong>Subqueries and&nbsp;Like<\/strong><\/h4>\n<p id=\"15de\" name=\"15de\">Sometimes you&rsquo;d like to dynamically grab data with a query and use that result in another query. For this case we have subqueries. For example, we could have a table called&nbsp;<strong>drs_favorites&nbsp;<\/strong>that holds doctor recommended activities:<\/p>\n<div id=\"656a\" name=\"656a\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#E6E6FA;\">\/* This is a comment *\/<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">create table drs_favorites ( <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; id integer primary key,&nbsp;&nbsp;&nbsp; \/* Unique identifier *\/<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; type text,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/* Type of activity *\/<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; reason text&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/* Why the doctor recommends it *\/<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">);<\/span><\/span><\/div>\n<div name=\"ceaa\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#E6E6FA;\">insert into drs_favorites<\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; (type, reason) <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">&nbsp; values (&#39;running&#39;, &#39;improves cardiovascular health.&#39;);<\/span><\/span><\/div>\n<p name=\"0c7a\">&nbsp;<\/p>\n<p id=\"0c7a\" name=\"0c7a\">For our main query we would like to find all the activities in the activity_log table that doctors recommended for improving cardiovascular health.<\/p>\n<div id=\"15f9\" name=\"15f9\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#E6E6FA;\">SELECT * FROM exercise_logs WHERE type IN (SELECT type FROM <\/span><br \/>\n<span style=\"background-color:#E6E6FA;\">drs_favorites WHERE reason LIKE &quot;%cardiovascular%&quot;);<\/span><\/span><\/div>\n<p name=\"979b\">&nbsp;<\/p>\n<p id=\"979b\" name=\"979b\">In between the parenthesis we have a subquery that selects all the records where the word &ldquo;cardiovascular&rdquo; is included somewhere in the reason the doctor recommends it. Read more about the&nbsp;<a data-href=\"https:\/\/www.w3schools.com\/sql\/sql_like.asp\" href=\"https:\/\/www.w3schools.com\/sql\/sql_like.asp\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">LIKE operator<\/a>. Note that SQL can use capital or lowercase letters.<\/p>\n<h4 id=\"5fa8\" name=\"5fa8\"><strong>Conclusion<\/strong><\/h4>\n<p id=\"16d1\" name=\"16d1\">That&rsquo;s the tip of the iceberg! The exercise code, including joins and SQL statements to talk to multiple databases are available on&nbsp;<a data-href=\"https:\/\/github.com\/connor11528\/coding-interviews\/tree\/master\/sql\" href=\"https:\/\/github.com\/connor11528\/coding-interviews\/tree\/master\/sql\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">this Github repository<\/a>.<\/p>\n<p id=\"7a2a\" name=\"7a2a\">If you&rsquo;d like to play around with a test database and write some queries, W3schools has a SQL editor available here:<\/p>\n<p><a data-href=\"https:\/\/www.w3schools.com\/sql\/trysql.asp?filename=trysql_op_in\" href=\"https:\/\/www.w3schools.com\/sql\/trysql.asp?filename=trysql_op_in\" rel=\"nofollow noopener\" title=\"https:\/\/www.w3schools.com\/sql\/trysql.asp?filename=trysql_op_in\"><strong>SQL Tryit Editor v1.5<\/strong><br \/>\n<em>Edit description<\/em>www.w3schools.com<\/a><\/p>\n<p id=\"61ee\" name=\"61ee\">Happy coding!<\/p>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>SQL is a critical skill. What is SQL anyway?&nbsp;SQL is a query language for talking to structured databases. Pretty much all databases that have tables and rows will accept SQL based queries. SQL has many flavors but the fundamentals stay the same. Professionals and amateurs alike use SQL to find, create, update and delete information from their sources of record. Understanding SQL queries can be fundamental to your work. Professionals and amateurs alike use SQL to find, create, update and delete information from their sources of record.<\/p>\n","protected":false},"author":288,"featured_media":3840,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[187],"tags":[94],"ppma_author":[1899],"class_list":["post-718","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bigdata-cloud","tag-data-science"],"authors":[{"term_id":1899,"user_id":288,"is_guest":0,"slug":"connor-leech","display_name":"Connor Leech","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","user_url":"","last_name":"Leech","first_name":"Connor","job_title":"","description":"Connor Leech is a Technical Master Stitcher at Stitch Labs, developing with Laravel, Javascript, and PHP. He is passionate about enabling developer productivity."}],"_links":{"self":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/users\/288"}],"replies":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/comments?post=718"}],"version-history":[{"count":1,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/718\/revisions"}],"predecessor-version":[{"id":6306,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/718\/revisions\/6306"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media\/3840"}],"wp:attachment":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media?parent=718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/categories?post=718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/tags?post=718"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}