<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: nimrodmasini</title>
    <description>The latest articles on DEV Community by nimrodmasini (@nimrodmasini).</description>
    <link>https://dev.to/nimrodmasini</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F724607%2F037e8d51-c57c-4324-a275-648d6dc0f78f.png</url>
      <title>DEV Community: nimrodmasini</title>
      <link>https://dev.to/nimrodmasini</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nimrodmasini"/>
    <language>en</language>
    <item>
      <title>INTRODUCTION TO SQL FOR DATA ANALYSIS</title>
      <dc:creator>nimrodmasini</dc:creator>
      <pubDate>Sat, 18 Feb 2023 07:19:53 +0000</pubDate>
      <link>https://dev.to/nimrodmasini/introduction-to-sql-for-data-analysis-4id7</link>
      <guid>https://dev.to/nimrodmasini/introduction-to-sql-for-data-analysis-4id7</guid>
      <description>&lt;h1&gt;
  
  
  INTRODUCTION TO SQL
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;SQL&lt;/strong&gt; is a short form for &lt;strong&gt;Structured Query Language&lt;/strong&gt;. It is the most popular query language used by major relational database management systems such as MySQL, ORACLE and SQL Server. SQL is easy to learn as the statements comprise of descriptive English words and are not case sensitive.&lt;br&gt;
We can create and interact with a database using SQL in an efficient and easy way. The benefit with SQL is that we don’t have to specify how to get the data from the database. Rather, we simply specify what is to be retrieved, and SQL does the rest. Although called a query language, SQL can do much more besides querying. SQL provides statements for defining the structure of&lt;br&gt;
the data, manipulating data in the database, declare constraints and retrieve data from the database in various ways, depending on our requirements. &lt;/p&gt;
&lt;h2&gt;
  
  
  Relational Databases
&lt;/h2&gt;

&lt;p&gt;Here we'll be discussing on Relational Databases, which is a collection of pre-defined relationships between them. It collects different types of data sets that use tables, rows(records) and columns(fields). A &lt;strong&gt;table&lt;/strong&gt; is used to store information about an entity, for example, a student entity, whereas &lt;strong&gt;fields&lt;/strong&gt; are used to hold the various characteristics of an entity such as the student registration number, age and course, while &lt;strong&gt;records&lt;/strong&gt; are the stored data about a particular entity.&lt;/p&gt;
&lt;h2&gt;
  
  
  Basic SQL Syntax
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;- CREATE DATABASE&lt;/strong&gt;&lt;br&gt;
The create database statement is used in creating a database, for example, creating a database called Student, we create as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE DatabaseName; 
USE DatabaseName;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; A Semi-colon is used at the end of each statement to mark the end of a single SQL statement.&lt;br&gt;
&lt;strong&gt;NB:&lt;/strong&gt; USE statement is used for making the specified&lt;br&gt;
database as active database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- CREATE Table&lt;/strong&gt;&lt;br&gt;
After creating a database, we create tables which are used to hold the various database entities, their records and fields, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE Student{
StudentID PRIMARY KEY INT NOT NULL,
StudentName VARCHAR(20) NOT NULL,
Course VARCHAR(20) NOT NULL
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NB:&lt;/strong&gt; A primary key is used to uniquely identify each record in a table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- DESCRIBE Table&lt;/strong&gt;&lt;br&gt;
The DESCRIBE statement is used to create the structure of an already created table, as below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DESCRIBE tablename;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- DROP Statement&lt;/strong&gt;&lt;br&gt;
The DROP statement is used to remove a table or a database permanently from a system. However, one should be very cautious while using this statement because it cannot be undone.&lt;br&gt;
&lt;em&gt;Syntax to drop a table:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP TABLE tableName;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Syntax to drop a database:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP DATABASE databaseName;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SQL For Data Manipulation
&lt;/h2&gt;

&lt;p&gt;Data Manipulation using SQL means either retrieval of existing data, insertion of new data, removal of existing data or modification of existing data in the database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- Insertion of Records&lt;/strong&gt;&lt;br&gt;
The INSERT INTO statement is used to insert new records in a table. For example, in a student table with, registration number, name, age, and gender as fields, records can be inserted as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO Student(reg_no, name, age, gender) VALUES(123, "James",23,"Male"),(234,"Mary",34,"Female");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Data Updation&lt;/strong&gt;&lt;br&gt;
The UPDATE statement is used to make changes in the value(s) of one or more columns of existing records in a table. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE table_name
SET attribute1 = value1, attribute2 = value2
WHERE condition 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- Data Deletion&lt;/strong&gt;&lt;br&gt;
The DELETE statement is used to delete one or more&lt;br&gt;
records from a table.Synax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM table_name
WHERE condition;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SQL For Data Queries
&lt;/h2&gt;

&lt;p&gt;SQL has efficient mechanisms to retrieve data stored in multiple tables. The user enters the SQL commands called queries where&lt;br&gt;
the specific requirements for data to be retrieved are&lt;br&gt;
provided.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- SELECT Statement&lt;/strong&gt;&lt;br&gt;
Is used to retrieve data from the tables in a database.&lt;br&gt;
&lt;em&gt;Syntax:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT attribute1, attribute2, ...
FROM table_name
WHERE condition;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;For example;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT name, age, course
FROM Students
WHERE age&amp;gt;20;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- WHERE Clause&lt;/strong&gt;&lt;br&gt;
Is used to retrieve data that meet&lt;br&gt;
some specified conditions. For example, the below example is used to select students whose age is greater than 20 years old;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT name, age, course
FROM Students
WHERE age&amp;gt;20;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;- ORDER BY Clause&lt;/strong&gt;&lt;br&gt;
The ORDER BY clause is used to display data in an ordered form with respect to a specified column.For example;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT *
FROM employee
ORDER BY salary;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SQL Joins
&lt;/h2&gt;

&lt;p&gt;They are used to query data from two or more tables, based on a relationship between certain columns in these tables.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Different SQL Joins&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JOIN&lt;/strong&gt;-Returns rows when there is at least one match in both tables.&lt;br&gt;
&lt;strong&gt;LEFT JOIN&lt;/strong&gt;-Returns all rows from the left table, even if there are no matches in the right table.&lt;br&gt;
&lt;strong&gt;RIGHT JOIN&lt;/strong&gt;-Returns all rows from the right table even if there are no matches in the left table. &lt;br&gt;
&lt;strong&gt;FULL JOIN&lt;/strong&gt;-Returns rows when there is a match in one of the tables.&lt;/p&gt;
&lt;h2&gt;
  
  
  Different SQL Joins
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Inner Join
&lt;/h3&gt;

&lt;p&gt;The INNER JOIN keyword returns rows when there is at least one match in both. For example the persons and orders taable below:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Persons Table:&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;P_ID&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Address&lt;/th&gt;
&lt;th&gt;City&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;Kisumu&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Nimrod&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Nyeri&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Muranga&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Orders table:&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Order_ID&lt;/th&gt;
&lt;th&gt;Order_No&lt;/th&gt;
&lt;th&gt;Order_PID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;1245&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;1256&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;1367&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;4398&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;4392&lt;/td&gt;
&lt;td&gt;15&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To perform INNER JOIN, we use the following SELECT STATEMENT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT P_ID, Name, Order_ID
FROM person
INNER JOIN P_ID ON P_ID=Order_PID;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The results set will look lie this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Order_ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1245&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1256&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;1367&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;4398&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Left Join
&lt;/h3&gt;

&lt;p&gt;The LEFT JOIN keyword returns all the rows from the left table(Persons), even if there are no matches in the right table(Orders).&lt;br&gt;
&lt;em&gt;Syntax:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT Name, Order_ID
FROM Persons
LEFT JOIN Orders ON P_ID=Order_PID;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This yields the following set:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Order_ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1245&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1256&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;1367&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;4398&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nimrod&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Right Join
&lt;/h3&gt;

&lt;p&gt;The RIGHT JOIN keyword returns all the rows from the right table(Orders), even if there are no matches in the left table(Persons).&lt;br&gt;
&lt;em&gt;Syntax:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT Name, Order_ID
FROM Persons
RIGHT JOIN Orders ON P_ID=Order_PID;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This returns the following result:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Order_ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1245&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1256&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;1367&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;4398&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;4392&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Full Join
&lt;/h3&gt;

&lt;p&gt;Returns rows when there is a match in one of the tables.&lt;br&gt;
&lt;em&gt;Syntax:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT Names, Order_ID
FROM Persons
FULL JOIN Orders ON P_ID=Orders_PID;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would yield the following:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Order_ID&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1245&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Elvis&lt;/td&gt;
&lt;td&gt;1256&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;1367&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;John&lt;/td&gt;
&lt;td&gt;4398&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nimrod&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;4392&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>datascience</category>
      <category>sql</category>
      <category>database</category>
    </item>
  </channel>
</rss>
