<?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: nia</title>
    <description>The latest articles on DEV Community by nia (@niania).</description>
    <link>https://dev.to/niania</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%2F2668595%2Fe12c0764-00a4-425d-b504-d7c07c270804.png</url>
      <title>DEV Community: nia</title>
      <link>https://dev.to/niania</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/niania"/>
    <language>en</language>
    <item>
      <title>[Git] 5 Git Tips I Learned Today</title>
      <dc:creator>nia</dc:creator>
      <pubDate>Sun, 02 Mar 2025 13:36:17 +0000</pubDate>
      <link>https://dev.to/niania/git-5-git-tips-i-learned-today-2502</link>
      <guid>https://dev.to/niania/git-5-git-tips-i-learned-today-2502</guid>
      <description>&lt;h1&gt;
  
  
  How to Stop Tracking Unnecessary Files in Git
&lt;/h1&gt;

&lt;p&gt;While working on a team project, I noticed that the &lt;code&gt;.idea/&lt;/code&gt; folder kept being tracked by Git. This folder contains IDE-generated metadata and configuration files, which don't need to be pushed to the remote repository.&lt;/p&gt;

&lt;p&gt;Even if &lt;code&gt;.idea/&lt;/code&gt; is listed in &lt;code&gt;.gitignore&lt;/code&gt;, Git might still track it if the folder was already added before the &lt;code&gt;.gitignore&lt;/code&gt; rule was applied.&lt;/p&gt;

&lt;p&gt;To stop tracking the folder without deleting the actual files, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git rm --cached -r .idea/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This removes the folder from Git's index without affecting the local files.&lt;/p&gt;

&lt;p&gt;After running the command, you should see output like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;delete '.idea/fileName.xml'
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Useful Things I Learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Hidden folders like &lt;code&gt;.idea/&lt;/code&gt; won't show up with &lt;code&gt;ls&lt;/code&gt;. Use &lt;code&gt;ls -a&lt;/code&gt; to see all hidden files and folders.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;.gitignore&lt;/code&gt; file needs to be in the &lt;strong&gt;root directory&lt;/strong&gt; of the project, not inside subfolders.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;/out&lt;/code&gt; folder, which stores compiled binaries or build artifacts, should also be excluded from the repository since these files can be regenerated from the source code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Discard Local Changes and Sync with Another Branch
&lt;/h1&gt;

&lt;p&gt;If you want to discard all local changes and sync your branch with the latest version of another branch, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git reset --hard origin/&amp;lt;branch_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command completely resets your branch to match the remote branch, deleting any local changes.&lt;/p&gt;

&lt;h1&gt;
  
  
  Setting IntelliJ as the Default Merge Conflict Tool
&lt;/h1&gt;

&lt;p&gt;If you're not comfortable resolving merge conflicts from the command line, IntelliJ can be used to visually resolve conflicts.&lt;/p&gt;

&lt;p&gt;On &lt;strong&gt;Mac&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global merge.tool intellij
git config --global mergetool.intellij.cmd "/Applications/IntelliJ IDEA.app/Contents/MacOS/idea diff $LOCAL $REMOTE $BASE $MERGED"
git config --global mergetool.prompt true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On &lt;strong&gt;Windows&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global merge.tool intellij
git config --global mergetool.intellij.cmd "C:/Program Files/JetBrains/IntelliJ IDEA/bin/idea64.exe" diff $LOCAL $REMOTE $BASE $MERGED
git config --global mergetool.prompt true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To verify the configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global --list | grep merge.tool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a conflict occurs, run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;IntelliJ will display the conflicting files. The &lt;strong&gt;left pane&lt;/strong&gt; shows the local changes (HEAD), and the &lt;strong&gt;right pane&lt;/strong&gt; shows the incoming changes from the remote branch. After resolving conflicts, click &lt;strong&gt;Accept&lt;/strong&gt; and commit the merge result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "Resolved merge conflicts"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Viewing Git Diff in IntelliJ
&lt;/h1&gt;

&lt;p&gt;To set IntelliJ as the default diff tool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global diff.tool intellij
git config --global difftool.intellij.cmd "/Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea diff $LOCAL $REMOTE"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this command to see differences in IntelliJ:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;IntelliJ will open each file one by one, showing the changes side-by-side.&lt;/p&gt;

&lt;h1&gt;
  
  
  Connecting an Existing Local Branch to a Remote Branch
&lt;/h1&gt;

&lt;p&gt;If you create a local branch without pushing it to the remote repository, you'll need to connect it manually.&lt;/p&gt;

&lt;p&gt;First, push the branch to the remote repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push origin &amp;lt;branch_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, set the upstream branch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git branch --set-upstream-to=origin/&amp;lt;branch_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the remote branch doesn't show up immediately, run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;You can verify the remote branches with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git branch -r
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures your local branch is properly tracking the remote branch.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>github</category>
      <category>git</category>
    </item>
    <item>
      <title>[Git] How to Temporarily Save Changes with `git stash` While Switching Branches</title>
      <dc:creator>nia</dc:creator>
      <pubDate>Sat, 01 Mar 2025 13:57:41 +0000</pubDate>
      <link>https://dev.to/niania/git-how-to-temporarily-save-changes-with-git-stash-while-switching-branches-50ej</link>
      <guid>https://dev.to/niania/git-how-to-temporarily-save-changes-with-git-stash-while-switching-branches-50ej</guid>
      <description>&lt;p&gt;1st March 2025 &lt;/p&gt;

&lt;p&gt;While working on a team project, there might be times when you're in the middle of writing code on your &lt;strong&gt;feat&lt;/strong&gt; branch but need to switch to the &lt;strong&gt;dev&lt;/strong&gt; branch to check something. In this case, you can use the &lt;code&gt;git stash&lt;/code&gt; command to temporarily save your work without committing unfinished changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Save your current changes:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git stash push &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Description of your work in progress"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Switch to the &lt;strong&gt;dev&lt;/strong&gt; branch:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git switch dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;After checking what you need on &lt;strong&gt;dev&lt;/strong&gt;, switch back to your &lt;strong&gt;feat&lt;/strong&gt; branch:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git switch feat
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Restore your stashed changes:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git stash pop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;git stash pop&lt;/code&gt; command applies the latest stash and automatically removes it from the stash list. If you want to apply the stash without deleting it, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash apply
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What If You Have Multiple Stashes?
&lt;/h2&gt;

&lt;p&gt;If you've stashed your work multiple times, you can see the list of saved stashes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;stash@&lt;span class="o"&gt;{&lt;/span&gt;0&lt;span class="o"&gt;}&lt;/span&gt;: On branch_name: Work &lt;span class="k"&gt;in &lt;/span&gt;progress 2
stash@&lt;span class="o"&gt;{&lt;/span&gt;1&lt;span class="o"&gt;}&lt;/span&gt;: On branch_name: Work &lt;span class="k"&gt;in &lt;/span&gt;progress 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To apply a specific stash, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git stash pop stash@&lt;span class="o"&gt;{&lt;/span&gt;0&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I haven't used multiple stashes much yet, but if I do, I'll come back and explain the process in more detail.&lt;/p&gt;

</description>
      <category>git</category>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Use Vi Editor - Beginner's Guide</title>
      <dc:creator>nia</dc:creator>
      <pubDate>Sat, 01 Mar 2025 06:39:43 +0000</pubDate>
      <link>https://dev.to/niania/how-to-use-vi-editor-beginners-guide-11k8</link>
      <guid>https://dev.to/niania/how-to-use-vi-editor-beginners-guide-11k8</guid>
      <description>&lt;p&gt;Day 11 - 22th Jan 2025&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick Summary: Create file, edit and save the file
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;vi &amp;lt;filename.txt &amp;gt;&lt;/code&gt; - if the file doesn't exist, this makes file with file name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;i&lt;/code&gt;- enter insert mode. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write your contents in the file. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;esc&lt;/code&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;:wq&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;enter&lt;/code&gt;! &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Most Important Concepts for Using &lt;code&gt;vi&lt;/code&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Modes in &lt;code&gt;vi&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Command Mode&lt;/strong&gt;: Default mode when you open &lt;code&gt;vi&lt;/code&gt;. You can navigate and execute commands here.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Insert Mode&lt;/strong&gt;: Used for editing text. Enter this mode by pressing &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Escape Mode&lt;/strong&gt;: Press &lt;code&gt;Esc&lt;/code&gt; to return to Command mode.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Basic Navigation&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;h&lt;/code&gt;: Move left&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;l&lt;/code&gt;: Move right&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;k&lt;/code&gt;: Move up&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;j&lt;/code&gt;: Move down&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Ctrl + d&lt;/code&gt;: Scroll down&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Ctrl + u&lt;/code&gt;: Scroll up&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Editing&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;i&lt;/code&gt;: Insert before the cursor.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a&lt;/code&gt;: Append after the cursor.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;o&lt;/code&gt;: Open a new line below the cursor.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dd&lt;/code&gt;: Delete the current line.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt;: Delete the character under the cursor.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;u&lt;/code&gt;: Undo the last action.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Ctrl + r&lt;/code&gt;: Redo the last undone action.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Saving and Exiting&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:w&lt;/code&gt;: Save changes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:q&lt;/code&gt;: Quit.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:wq&lt;/code&gt;: Save and quit.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:q!&lt;/code&gt;: Quit without saving.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Search&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;/text&lt;/code&gt;: Search forward for "text".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;?text&lt;/code&gt;: Search backward for "text".&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt;: Repeat the search in the same direction.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;N&lt;/code&gt;: Repeat the search in the opposite direction.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Copy, Paste, and Cut&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;yy&lt;/code&gt;: Copy (yank) the current line.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;p&lt;/code&gt;: Paste the copied text after the cursor.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dd&lt;/code&gt;: Cut the current line.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  7. &lt;strong&gt;Replace Text&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;:s/old/new/&lt;/code&gt;: Replace the first occurrence of "old" with "new" on the current line.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:s/old/new/g&lt;/code&gt;: Replace all occurrences of "old" with "new" on the current line.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:%s/old/new/g&lt;/code&gt;: Replace all occurrences in the entire file.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Quick Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Always press &lt;code&gt;Esc&lt;/code&gt; to ensure you're in Command mode before executing commands.&lt;/li&gt;
&lt;li&gt;Practice basic navigation and saving before diving into advanced features.&lt;/li&gt;
&lt;li&gt;To exit quickly without saving (if you're stuck), press &lt;code&gt;Esc&lt;/code&gt; and type &lt;code&gt;:q!&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>basic</category>
      <category>programming</category>
    </item>
    <item>
      <title>[SQL] Using Session Variables in MySQL</title>
      <dc:creator>nia</dc:creator>
      <pubDate>Fri, 28 Feb 2025 12:13:05 +0000</pubDate>
      <link>https://dev.to/niania/sql-using-session-variables-in-mysql-3noo</link>
      <guid>https://dev.to/niania/sql-using-session-variables-in-mysql-3noo</guid>
      <description>&lt;p&gt;26th Feb 2025 Wednesday &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Session Variable?
&lt;/h2&gt;

&lt;p&gt;Session variables, also known as user-defined variables, are temporary variables that persist for the duration of a database session — the period during which a user remains connected to the database. These variables are only accessible within the current session and are automatically discarded once the session ends. Unlike table data, session variables are not permanently stored in the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Declaring and Assigning Variables
&lt;/h3&gt;

&lt;p&gt;You can declare and assign values to session variables using the &lt;code&gt;SET&lt;/code&gt; statement:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;my_var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;my_var&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- Retrieves the value of the variable&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Alternatively, the &lt;code&gt;SELECT ... INTO&lt;/code&gt; syntax allows you to assign the result of a query directly to a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;total_employees&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;total_employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- Displays the total number of employees&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using Session Variables in Queries
&lt;/h3&gt;

&lt;p&gt;Session variables can be applied in queries to perform dynamic calculations. For example, to calculate salary increases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;increase_rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;increase_rate&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;new_salary&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here, the &lt;code&gt;@increase_rate&lt;/code&gt; variable stores the salary increase rate, which is applied to each employee’s salary in the query result.&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating Data with Session Variables
&lt;/h3&gt;

&lt;p&gt;Session variables can also be used in &lt;code&gt;UPDATE&lt;/code&gt; statements to set dynamic conditions. For instance, to increase the salaries of employees earning below the average salary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;avg_salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;05&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;avg_salary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This query first calculates the average salary and stores it in &lt;code&gt;@avg_salary&lt;/code&gt;, then applies a 5% salary increase to employees earning below that amount.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inserting Data with Session Variables
&lt;/h3&gt;

&lt;p&gt;You can insert data into another table using session variables combined with &lt;code&gt;INSERT INTO ... SELECT&lt;/code&gt; statements. For example, to store information about the highest-paid employees in a separate table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;max_salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;high_paid_employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;max_salary&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This method inserts data into the target table by selecting matching rows from the source table, without needing explicit &lt;code&gt;VALUES&lt;/code&gt; clauses.&lt;/p&gt;

&lt;p&gt;Session variables provide a flexible way to store temporary data and simplify query logic, especially in scenarios involving calculations or conditional updates.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>mysql</category>
      <category>beginners</category>
    </item>
    <item>
      <title>[SQL] Essential Terms in the Relational Database Model and Characteristics of a relation</title>
      <dc:creator>nia</dc:creator>
      <pubDate>Mon, 24 Feb 2025 21:51:31 +0000</pubDate>
      <link>https://dev.to/niania/sql-essential-terms-in-the-relational-database-model-and-characteristics-of-a-relation-1cmj</link>
      <guid>https://dev.to/niania/sql-essential-terms-in-the-relational-database-model-and-characteristics-of-a-relation-1cmj</guid>
      <description>&lt;p&gt;Feb 24rd 2025 - part 1 &lt;/p&gt;

&lt;h2&gt;
  
  
  Before starting the post...
&lt;/h2&gt;

&lt;p&gt;Today I learned about terms used when dealing with relational database models and the key characteristics of a relation. I’m the type of person who thinks using a precise word is important, so  I’m glad that this is a trait that’s considered desirable for a developer. 😄 It’s 19:33 currently and I’m off to the gym after writing this post! &lt;/p&gt;

&lt;h2&gt;
  
  
  Why understanding the correct terms is important
&lt;/h2&gt;

&lt;p&gt;Every field has its vocabulary that defines key concepts, and so does the RDBMS. It is crucial to understand the basic terms to get a deeper understanding of RDBMS. They are the building blocks for grasping complex concepts and applying them correctly. &lt;/p&gt;

&lt;h2&gt;
  
  
  Defining essential terms used in relational database model
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;A relational database model&lt;/strong&gt; is a way of organizing and managing data in a database using a structure based on relations and their relationships.&lt;/p&gt;

&lt;p&gt;A  &lt;strong&gt;relationship&lt;/strong&gt; refers to an association between two or more tables based on common attributes. Relationships are established using &lt;strong&gt;foreign keys&lt;/strong&gt;, which link a column in one table to the primary key of another or the same table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A relation schem&lt;/strong&gt;a is the structure of the relation. It defines the relation's design, including the names of the columns, the data types for each column, and constraints. But it does not include the actual data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Relation instance&lt;/strong&gt; refers to the specific data currently in that relation at a given time. It represents the actual, concrete data that exists in the relation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A relation&lt;/strong&gt; is what we commonly know as a table. Relation includes its structure (schema) and the current data (tuples) stored at a given time. A relation combines the schema (the structure) and the instance (the current set of data).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Intension&lt;/strong&gt; and Schema are essentially the same. It's a structure of the relation, but not the data itself. &lt;br&gt;
&lt;strong&gt;The Extension&lt;/strong&gt;, however, includes the actual tuples in the table. A set of tuples is the extension of a relation. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Attributes&lt;/strong&gt; are columns of a relation.&lt;br&gt;
Constraints of a relation are rules enforced to ensure the data's accuracy, consistency, and integrity. They define what is allowed or not allowed in the relation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Domain&lt;/strong&gt; refers to the set of valid values an attribute in a relation can hold. It defines the type, range, and constraints for the data that can be stored in that column. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data type&lt;/strong&gt;is a specific category of data, like INT or VARCHAR. But domain is a broader concept that includes data type plus any additional constraints (NOT NULL, UNIQUE), and allowed range of values (ENUM sets). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Degree&lt;/strong&gt; refers to the number of attributes in a relation. It measures how many fields each tuple contains.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A tuple&lt;/strong&gt; is a single row in a relation. It's a collection of values that describes a single entity in the relation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cardinality&lt;/strong&gt; refers to the number of tuples in a relation at a given time. If the relation has 50 tuples, the cardinality of that relation is 50. If a relation is empty, its cardinality is 0.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why understanding the characteristics of a relation is important
&lt;/h2&gt;

&lt;p&gt;To ensure consistency, integrity, and efficiency, a relation must follow specific characteristics that define its structure and behavior. These characteristics establish rules for how data is stored, retrieved, and maintained within the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Characteristics of a relation
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Each attribute(column) must have a unique name. No two attributes have the same name. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each tuple is unique. A relation must not have duplicate tuples. The primary key ensures its uniqueness. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Entries in a column are from the same domain. This means that all values in a particular column must follow a consistent set of rules, like data type, constraints and format (an email must contain @). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The order of rows and columns does not matter.&lt;br&gt;
The rows and columns in a relation have no inherent order. You can rearrange them without changing the meaning of the data. A relation is a set, not a list, so the focus is on the data itself, not its position.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Values in each column must be atomic. This means a single cell should only contain one value. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each attribute must have a defined domain (data type and constraints). &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;NULL values are allowed unless restricted. NULL means missing or unknown data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>sql</category>
      <category>database</category>
    </item>
    <item>
      <title>[SQL] - Inner Join, Left Join, Non-Equi Join, Self Join</title>
      <dc:creator>nia</dc:creator>
      <pubDate>Sun, 23 Feb 2025 12:43:03 +0000</pubDate>
      <link>https://dev.to/niania/sql-inner-join-left-join-non-equi-join-self-join-1ee7</link>
      <guid>https://dev.to/niania/sql-inner-join-left-join-non-equi-join-self-join-1ee7</guid>
      <description>&lt;p&gt;&lt;strong&gt;[Day 26] Reviewing Incorrect SQL problems.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before diving in, I’ll start by explaining the purpose of using JOINs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use the JOIN in SQL
&lt;/h2&gt;

&lt;p&gt;The JOIN combines information from different tables into a single dataset to retrieve the desired data.&lt;/p&gt;

&lt;p&gt;For example, imagine one table contains "customer information" (e.g., name, customer ID), and another table holds "order information" (e.g., order ID, customer ID, order date). If I want to know "which customers placed orders and when," how would I approach this?&lt;/p&gt;

&lt;p&gt;I can connect the customer table and the order table using the common key, &lt;code&gt;customer_id&lt;/code&gt;, through a JOIN, allowing me to view the customer’s name and order date together in one result. If the customer table is called the &lt;code&gt;customer&lt;/code&gt; and the order table is &lt;code&gt;order&lt;/code&gt;, use &lt;code&gt;customer.customer_id = order.customer_id&lt;/code&gt; retrieves only the rows where &lt;code&gt;customer_id&lt;/code&gt; matches in both tables. This effectively returns the intersection of the two tables—only the overlapping data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem 01
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You need to generate a report of each employee’s salary grade, which is stored in the &lt;code&gt;Job_Grades&lt;/code&gt; table. The report should include the employee’s job title, department name, hire date, salary, and salary grade.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;employees&lt;/code&gt; table does not contain &lt;code&gt;job_title&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;employees e JOIN jobs j ON e.job_id = j.job_id&lt;/code&gt;: I use the employee’s &lt;code&gt;job_id&lt;/code&gt; to fetch the &lt;code&gt;job_title&lt;/code&gt; from the &lt;code&gt;jobs&lt;/code&gt; table.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;code&gt;employees&lt;/code&gt; table does not include &lt;code&gt;department_name&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- `employees e JOIN departments d ON e.department_id = d.department_id`: I use the employee’s `department_id` to retrieve the `department_name` from the `departments` table.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;I needed to consider how to use the &lt;code&gt;lowest_sal&lt;/code&gt; and &lt;code&gt;highest_sal&lt;/code&gt; fields in the &lt;code&gt;job_grades&lt;/code&gt; table to assign grades based on an employee’s salary. I couldn’t rely on the typical equi join (joining with &lt;code&gt;=&lt;/code&gt;), which I’ve been accustomed to using.&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;- I realized that I need to fetch only the rows where the `salary` falls between `lowest_sal` and `highest_sal`. If I want to include employees whose salaries fall outside any defined range, I should use a `LEFT JOIN`. Using a plain `JOIN` (which defaults to `INNER JOIN`) would exclude employees whose salaries don’t fit any range, as those rows with NULL values would be omitted from the output.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```sql
SELECT e.last_name, j.job_title, d.department_name, e.hire_date, e.salary, jg.grade_level
FROM employees e
JOIN departments d ON e.department_id = d.department_id
JOIN jobs j ON e.job_id = j.job_id
LEFT JOIN job_grades jg ON e.salary BETWEEN jg.lowest_sal AND jg.highest_sal;

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

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;This problem initially confused me because I had primarily used equi joins to solve problems. It turned out to be a valuable opportunity to learn more about non-equi joins.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Problem 02
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Some employees report to other employees as their managers (supervisors). Query the employees’ manager IDs, manager names, employee names, and employee IDs. If an employee does not report to a manager,  you must still include that information in the output, and the manager’s name should be displayed in uppercase.
&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;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;employee_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;UPPER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt;
    &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Since the problem specifies, “If there are employees without a reporting manager, include that information as well,” I need to include employees with &lt;code&gt;manager_id&lt;/code&gt; as NULL in the results. Using a plain &lt;code&gt;JOIN&lt;/code&gt; (which defaults to &lt;code&gt;INNER JOIN&lt;/code&gt;) would exclude employees without managers, as it only returns matching rows. Therefore, I must use &lt;code&gt;LEFT JOIN&lt;/code&gt; to ensure all employees are included.&lt;/p&gt;

&lt;p&gt;Here’s another valid version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;employee_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager_name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Problem 02
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Output the department name and employee count for departments with five or more employees, sorted by employee count in descending order.
&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;departments&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_name&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;- ANSI Standard Version:
&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;SELECT&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;departments&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_name&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;I initially got this wrong because I performed the grouping before joining the tables. I need to join the tables first to establish the connection, then group and filter the results.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem 03
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Query the total number of employees working as managers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A Quick review of primary key and foreign key
&lt;/h3&gt;

&lt;p&gt;Databases store data across multiple tables. To connect data between tables, I use a common value called a “key,” which can be thought of as a column in a table. A &lt;strong&gt;primary key (Primary Key)&lt;/strong&gt; uniquely identifies each row (record) in a table. A &lt;strong&gt;foreign key (Foreign Key)&lt;/strong&gt; is a column in one table that references the primary key in another table (or the same table). “Referencing a key” means that a foreign key in one table points to or connects to a primary key in another (or the same) table. &lt;/p&gt;

&lt;p&gt;[EER Diagram of the database]&lt;/p&gt;

&lt;p&gt;I can use MySQL Workbench’s Reverse Engineer feature to create an EER diagram and examine the relationships between tables. By observing the four lines connected to the &lt;code&gt;employees&lt;/code&gt; table in the diagram, I can identify which columns in the &lt;code&gt;employees&lt;/code&gt; table are linked to columns in other tables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm92uqxdtn6t3pwxuxu2i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm92uqxdtn6t3pwxuxu2i.png" alt="eer diagram of hr database" width="800" height="648"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, the &lt;code&gt;manager_id&lt;/code&gt; I need isn’t connected to any other table’s key. Instead, &lt;code&gt;manager_id&lt;/code&gt; references the &lt;code&gt;employee_id&lt;/code&gt; within the &lt;code&gt;employees&lt;/code&gt; table itself. Therefore, I need to use a self-join.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgojqez2wkd2qwkh7m4y0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgojqez2wkd2qwkh7m4y0.png" alt="self-join relationship between manager_id and employee_id" width="456" height="732"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Every employee has an &lt;code&gt;employee_id&lt;/code&gt;, and managers do too. To query employees working as managers, I can write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager_count&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&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;
&lt;code&gt;e1&lt;/code&gt; is the alias for the manager table, and &lt;code&gt;e2&lt;/code&gt; is the alias for the employee table. When joining the manager table (&lt;code&gt;e1&lt;/code&gt;) with the employee table (&lt;code&gt;e2&lt;/code&gt;), &lt;code&gt;e1.employee_id = e2.manager_id&lt;/code&gt; means it retrieves only the rows where the manager table’s &lt;code&gt;employee_id&lt;/code&gt; matches the employee table’s &lt;code&gt;manager_id&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s another query that produces the same result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager_count&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The difference lies in the &lt;code&gt;FROM&lt;/code&gt; clause and the &lt;code&gt;JOIN employees e1&lt;/code&gt; part. The logical relationship remains the same, so the results are identical. However, conventionally, placing the “subject (manager)” in the &lt;code&gt;FROM&lt;/code&gt; clause and the “dependent (employee)” in the &lt;code&gt;JOIN&lt;/code&gt; clause is more readable and aligns with standard practices.&lt;/p&gt;

&lt;p&gt;Conversely, if the logical relationship changes, the query results will differ. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;num_of_manager&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This query implies that &lt;code&gt;e2&lt;/code&gt; (employee) references &lt;code&gt;e1&lt;/code&gt; (manager). “Reference” means a column in one table points to or connects to a column in another (or the same) table. Here, &lt;code&gt;manager_id&lt;/code&gt; (foreign key) references &lt;code&gt;employee_id&lt;/code&gt; (primary key) in the &lt;code&gt;employees&lt;/code&gt; table, forming a “self-referencing relationship.”&lt;/p&gt;

&lt;p&gt;A primary key uniquely identifies each row in a table and does not reference data in other tables—it is only referenced by foreign keys. A primary key does not reference a foreign key; rather, a foreign key references a primary key. In the EER diagram, the self-referencing relationship between &lt;code&gt;employee_id&lt;/code&gt; and &lt;code&gt;manager_id&lt;/code&gt; confirms this connection.&lt;/p&gt;

&lt;p&gt;I used to interpret the JOIN as simply “fetching rows where they match,” but this review has helped me gain a clearer understanding of 'referencing' in joining tables. &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
