<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Lee Njogu on Medium]]></title>
        <description><![CDATA[Stories by Lee Njogu on Medium]]></description>
        <link>https://medium.com/@leenjogu?source=rss-4daac5a9d49b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-eXbn6zYE6NoTbdT</url>
            <title>Stories by Lee Njogu on Medium</title>
            <link>https://medium.com/@leenjogu?source=rss-4daac5a9d49b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 17:16:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leenjogu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Working with timestamps in Postgresql — Speaking the language of databases.]]></title>
            <link>https://medium.com/@leenjogu/working-with-timestamps-in-postgresql-speaking-the-language-of-databases-1d931d8a6906?source=rss-4daac5a9d49b------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d931d8a6906</guid>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Lee Njogu]]></dc:creator>
            <pubDate>Tue, 07 Feb 2023 19:53:25 GMT</pubDate>
            <atom:updated>2023-02-07T19:53:25.645Z</atom:updated>
            <content:encoded><![CDATA[<h3>Working with timestamps in Postgresql — Speaking the language of databases.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*-v1CLyXEOTyFgjZaKTm83g.jpeg" /><figcaption>Image by Tyrita White</figcaption></figure><p>Timestamps are an important data type that we need to pay close attention while working with database management systems. In this article, you get to learn the following date-time-related concepts:</p><ul><li><strong>How to order data by a timestamp.</strong></li><li><strong>How to calculate the interval between two timestamps.</strong></li><li><strong>How to extract text from parts of timestamp.</strong></li></ul><p>For this tutorial, I used a dataset <em>EmployeesData</em> that contains data on employees working at a Records Studio.</p><p><a href="https://drive.google.com/file/d/10OGqUpjM-FfGlA5EZj7bSaGWqhuS5Liy/view?usp=drivesdk">EmployeesData.csv</a></p><p>To upload the dataset to Postgresql local server on your machine, you will need to first copy and paste this dataset to the C:\User\Public folder. This allows your dataset to be in a file location where the Postgresql server can access it.</p><p>Let’s now get to it. We will now write a query to create a table to which we copy the dataset.</p><pre>CREATE TABLE IF NOT EXISTS Employees(<br>EmployeeId INTEGER PRIMARY KEY,<br>LastName VARCHAR(20),<br>FirstName VARCHAR(20),<br>Title VARCHAR(30),<br>ReportsTo INTEGER,<br>BirthDate DATE,<br>HIreDate DATE,<br>Address VARCHAR(70),<br>City VARCHAR(40),<br>Statte VARCHAR(40),<br>Country VARCHAR(40),<br>PostalCode VARCHAR(10),<br>Phone VARCHAR(24),<br>Fax VARCHAR(24),<br>Email VARCHAR(60));</pre><p>Now let’s write a query to copy the dataset from a directory that we will specify to our <em>Employees</em> table.</p><pre>COPY Employees<br>FROM &#39;C:\Users\Public\SQL outputs\EmployeesData.csv&#39;<br>DELIMITER &#39;,&#39; CSV HEADER;</pre><p>With the dataset already copied to the <em>Employees</em> table, let’s take a look at it.</p><pre>SELECT * FROM Employees;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*S-R8ttxxlRZPrv6fyHdGhA.png" /></figure><p>For this tutorial, our columns of interest will be <strong>birthdate</strong> and <strong>hiredate</strong> as they are date-time data types.</p><h3>Who was the first employee to be hired?</h3><pre>SELECT <br>hiredate, <br>firstname, <br>lastname<br>FROM Employees <br>ORDER BY hiredate ASC LIMIT 1;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/929/1*MCRrSvKZSiOUAHRhou0Kbw.png" /></figure><p>The <em>ORDER BY</em> clause is used to sort the data by the <strong>hiredate</strong> column. To get the row with the oldest hiredate, then we need to use the <em>ASC</em> and <em>LIMIT</em> clauses to specify that our result should be in ascending order and the number of top rows we need to return.</p><h3>How old is each one of the employees?</h3><pre>SELECT <br>firstname, <br>lastname,<br>AGE(birthdate) AS age<br>FROM Employees <br>ORDER BY age DESC;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*eF9HEhwfXcT5u1UDbs6YCg.png" /></figure><p>To get the interval between two timestamps in years, months, days, hours, minutes and seconds, we use the <strong>AGE()</strong> function. In this case, it calculates the interval between the current date and each employee’s <strong>birthdate</strong>.</p><h3>How long has each employee been working at the Record Studio?</h3><pre><br>SELECT<br>firstname, <br>lastname, <br>AGE(now(), hiredate) AS years_employed<br>FROM Employees <br>ORDER BY years_employed DESC;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*9-TR9tCg353LcsJnSiDO-w.png" /></figure><p>Here we calculate the interval between the current date and each employee’s date of hire.</p><h3>Extract the day of the week and month from the hiring date of each employee.</h3><pre>SELECT <br>firstname,<br>lastname,<br>EXTRACT(YEAR FROM hiredate) AS year_of_hire,<br>EXTRACT(MONTH FROM hiredate) AS month_of_hire,<br>to_char(hiredate, &#39;Day&#39;) AS weekday_of_hire<br>FROM Employees;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*RlHz4zLTDs6YaIpdIv2VXg.png" /></figure><p>The <strong>EXTRACT()</strong> method in the query above makes it possible to retrieve year, month, or day from the <strong>hiredate</strong> timestamp. The <strong>to_char()</strong> function retrieves the day of the week as a string from the <strong>hiredate</strong> timestamp.</p><p>In conclusion, note that the above operations are just a drop in the ocean, considering the many ways you can manipulate timestamps in database management systems. Stay tuned for more tips on working with <strong>Sequel</strong> in the <em>Speaking the language of databases</em> article series. That’s all until next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d931d8a6906" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All About Joins]]></title>
            <link>https://medium.com/@leenjogu/all-about-joins-91b0b1b5fdce?source=rss-4daac5a9d49b------2</link>
            <guid isPermaLink="false">https://medium.com/p/91b0b1b5fdce</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Lee Njogu]]></dc:creator>
            <pubDate>Mon, 23 Jan 2023 21:48:20 GMT</pubDate>
            <atom:updated>2023-01-23T21:48:20.116Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Barbed wire" src="https://cdn-images-1.medium.com/max/385/1*6zSYcjbLotIijlULy6k8bQ.jpeg" /><figcaption>Photo by TtotheStreet</figcaption></figure><p>In the real world, data is scattered across different tables on a database. As a data analyst, there will always arise a need to combine data from two different tables into one dataset. The SQL <em>JOIN </em>clause helps make this possible. There are a few approaches in performing a ‘join’, namely <strong><em>inner</em></strong> join, <strong><em>left</em></strong> join, <strong><em>right</em></strong> join, and <strong><em>full</em> </strong>join.</p><p><strong>Types of Joins.</strong></p><p><strong>Inner join</strong></p><p>An <strong><em>inner</em></strong> join matches rows from the 1st and 2nd table with the same key column.</p><pre>SELECT column, another_column<br>FROM first_table<br>INNER JOIN second_table<br>ON first_table.Id = second_table.matching_Id<br>WHERE condition(s)</pre><p><strong>Left Join</strong></p><p>A <strong><em>left</em> </strong>join includes all rows from the 1st table regardless of whether a matching row exists in the 2nd table.</p><pre>SELECT column, another_column<br>FROM first_table<br>LEFT JOIN second_table<br>ON first_table.Id = second_table.matching_Id<br>WHERE condition(s)</pre><p><strong>Right Join</strong></p><p>A <strong><em>right</em></strong> join keeps all rows from the 2nd table regardless of any matches found in the 1st table.</p><pre>SELECT column, another_column<br>FROM first_table<br>RIGHT JOIN second_table<br>ON first_table.Id = second_table.matching_Id<br>WHERE condition(s)</pre><p><strong>Full outer join</strong></p><p>A <strong><em>full outer </em></strong>join keeps all rows from both tables(including both matching and unmatched rows).</p><pre>SELECT column, another_column<br>FROM first_table<br>FULL JOIN second_table<br>ON first_table.Id = second_table.matching_Id<br>WHERE condition(s)</pre><p>Below is an illustration to help you better understand joins visually.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*TzNnSgR66tI71BHUNWre_A.png" /></figure><p>I hope this write up gave you a better understanding of SQL joins. Till next time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91b0b1b5fdce" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>