<?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: Daniel Chege</title>
    <description>The latest articles on DEV Community by Daniel Chege (@chege10).</description>
    <link>https://dev.to/chege10</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%2F2155932%2Fd6efeb42-c7b4-47f2-acb8-b839ae0be6bb.png</url>
      <title>DEV Community: Daniel Chege</title>
      <link>https://dev.to/chege10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chege10"/>
    <language>en</language>
    <item>
      <title>SQL 101: Introduction to Structured Query Language(SQL)</title>
      <dc:creator>Daniel Chege</dc:creator>
      <pubDate>Wed, 02 Oct 2024 09:55:10 +0000</pubDate>
      <link>https://dev.to/chege10/sql-101-introduction-to-structured-query-languagesql-4ij9</link>
      <guid>https://dev.to/chege10/sql-101-introduction-to-structured-query-languagesql-4ij9</guid>
      <description>&lt;h2&gt;
  
  
  What is SQL?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;SQL, or Structured Query Language, is a language designed to allow both technical and non-technical users query, manipulate, and transform data from a relational database. And due to its simplicity, SQL databases provide safe and scalable storage for millions of websites and mobile applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are many popular SQL databases including SQLite, MySQL, Postgres, Oracle and Microsoft SQL Server, they all support the common SQL language standard.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is relational database?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A relational database is a type of database that stores and provides access to data points that are related to one another. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if a school has a database, you might find a table containing data of all students. This table might need to store the student name, administration number, age, and his/her level(grade). &lt;/p&gt;

&lt;h3&gt;
  
  
  Key components of SQL
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;SQL consists of various commands, categorized into different groups based on their purpose. Here’s a breakdown of the primary categories:&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Data Defination Language(DDL).
&lt;/h4&gt;

&lt;p&gt;DDL commands are used to define the structure of the database, including creating, altering, and deleting tables.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
CREATE: Used to create a new table or database.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- Create a database&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="n"&gt;schoolDB&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;--Create table&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;Administration_number&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Student_name&lt;/span&gt; &lt;span class="nb"&gt;VARCHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;Grade&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;ALTER: Modifies an existing table, like adding or removing a column.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt; &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;DROP: Deletes a table or database.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;TRUNCATE: Removes all records from a table, including all spaces allocated for the records are removed.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;TRUNCATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;students&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Data Manipulation Language (DML)
&lt;/h4&gt;

&lt;p&gt;DML is used to manage data within tables, focusing on data retrieval, insertion, updates, and deletion.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INSERT: Used to insert data into a table.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO students(Administration_number, Student_name, age, grade)
VALUES (
12, Peter, 11, 2);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;UPDATE: Used to update existing data within a table
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE students
SET age = 13
WHERE administration_number = 1124;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;DELETE: Removes rows of data from a table.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DELETE FROM students WHERE administration_number = 1124;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;LOCK: Table control concurrency.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CALL: Call a PL/SQL &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Data Control Language(DCL)
&lt;/h4&gt;

&lt;p&gt;DCL mainly deal with the rights, permissions, and other controls of the database system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GRANT: Gives users access privileges to the database.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GRANT SELECT, UPDATE ON students TO SOME_USER, ANOTHER_USER;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;REVOKE: Withdraws the user’s access privileges given by using the GRANT commands.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REVOKE SELECT, UPDATE ON students FROM USER1, USER2;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Transaction Control Language(TCL)
&lt;/h4&gt;

&lt;p&gt;TCL manages transactions within a database, ensuring data consistency.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;OPEN: Opens a transaction.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;COMMIT: Commits a transaction.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;ROLLBACK: Rollbacks a transaction in case of any error occurs.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Data Query Language(DQL)
&lt;/h4&gt;

&lt;p&gt;DQL are used for performing queries on the data within schema.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
SELECT: Retrieves data from one or more tables.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM students;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  SQL Syntax and Structure
&lt;/h3&gt;

&lt;p&gt;SQL is designed easy to read and write. Each query follows a structured flow: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;SELECT&lt;/em&gt; Clause – Specifies the columns to retrieve.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT student_name, age
FROM students;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-_ FROM_ Clause: Specifies the table from which data is retrieved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM students;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;WHERE&lt;/em&gt; Clause: Applies conditions to filter the data.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WHERE age &amp;lt; 15;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;ORDER BY&lt;/em&gt; Clause: Sorts the results by one or more columns.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ORDER BY administration_number ASC/DESC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;GROUP BY&lt;/em&gt; Clause: Groups rows with the same values into summary rows.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT
student_name,
COUNT(*)
FROM students
GROUP BY age;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  SQL JOINS
&lt;/h3&gt;

&lt;p&gt;SQL joins combine rows from two or more tables based on a related column between them. The most common types of joins are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;INNER JOIN: Returns rows that have matching values in both tables.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT column, another_table_column
FROM mytable
INNER JOIN another_table 
    ON mytable.id = another_table.id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;LEFT JOIN: Returns all rows from the left table and the matched rows from the right table. If there's no match, NULL values are returned.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;RIGHT JOIN: Similar to the LEFT JOIN but returns all rows from the right table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;FULL OUTER JOIN: Returns rows when there is a match in one of the tables.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT column, another_column, …
FROM mytable
LEFT/RIGHT/FULL JOIN another_table 
    ON mytable.id = another_table.matching_id
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Common SQL Functions
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Aggregate Functions: These include SUM, COUNT, AVG, MIN, and MAX, which are used to perform calculations on sets of rows.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT AGG_FUNC(column_or_expression) AS aggregate_description, …
FROM mytable;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;String Functions: These functions manipulate string values, such as UPPER(), LOWER(), CONCAT(), etc.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT STR_FUNC(column)
FROM mytable;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Date Functions: SQL provides various functions to manipulate and format dates, like NOW(), DATEADD(), and DATE_TRUNC().
SELECT.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Conclusion
&lt;/h4&gt;

&lt;p&gt;SQL is essential for managing and analyzing data, offering powerful tools for efficient database operations.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
