<?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>Forem: Lokeshwaran S</title>
    <description>The latest articles on Forem by Lokeshwaran S (@lokeshwaran_s_db77da73b51).</description>
    <link>https://forem.com/lokeshwaran_s_db77da73b51</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%2F3838360%2F230e4b57-7531-4ac0-8a67-4082678ecdf4.png</url>
      <title>Forem: Lokeshwaran S</title>
      <link>https://forem.com/lokeshwaran_s_db77da73b51</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lokeshwaran_s_db77da73b51"/>
    <language>en</language>
    <item>
      <title>Users, Roles and Groups in SQL - CA33</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 29 Mar 2026 13:21:43 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/users-roles-and-groups-in-sql-ca33-38pd</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/users-roles-and-groups-in-sql-ca33-38pd</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this task, I worked with roles and permissions in SQL using the dvdrental database. The goal was to control access to different tables by creating roles, assigning privileges, and managing user groups.&lt;/p&gt;

&lt;p&gt;This helped me understand how database security works in real-world applications.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create roles with limited access&lt;/li&gt;
&lt;li&gt;Grant and revoke permissions&lt;/li&gt;
&lt;li&gt;Restrict access to specific columns&lt;/li&gt;
&lt;li&gt;Manage users using groups&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I thought:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Roles are just users&lt;/li&gt;
&lt;li&gt;Permissions are simple&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I realized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Roles control access at different levels&lt;/li&gt;
&lt;li&gt;Permissions can be very specific&lt;/li&gt;
&lt;li&gt;Security is very important in databases&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;GRANT is used to give permissions&lt;/li&gt;
&lt;li&gt;REVOKE is used to remove permissions&lt;/li&gt;
&lt;li&gt;Roles can be grouped for easier management&lt;/li&gt;
&lt;li&gt;Column-level access is possible&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Task 1: Create report_user with access to film table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;report_user&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;LOGIN&lt;/span&gt; &lt;span class="n"&gt;PASSWORD&lt;/span&gt; &lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;report_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates a login role&lt;/li&gt;
&lt;li&gt;Allows only read access to film table&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Task 2: Fix access to customer table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;report_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Grants permission to read customer table&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Task 3: Restrict columns in customer table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;REVOKE&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;report_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;customer_id&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="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;report_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Removes full access&lt;/li&gt;
&lt;li&gt;Grants access only to specific columns&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Task 4: Create support_user with limited permissions
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;support_user&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;LOGIN&lt;/span&gt; &lt;span class="n"&gt;PASSWORD&lt;/span&gt; &lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;support_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;support_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can view customer data&lt;/li&gt;
&lt;li&gt;Can update only email column&lt;/li&gt;
&lt;li&gt;No DELETE permission given&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Task 5: Remove film access from report_user
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;REVOKE&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;report_user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Removes access to film table&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Task 6: Create readonly_group
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;readonly_group&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt; &lt;span class="n"&gt;TABLES&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="k"&gt;SCHEMA&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;readonly_group&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Group with read-only access&lt;/li&gt;
&lt;li&gt;Applies to all tables&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Task 7: Create users and assign to group
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;analyst1&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;LOGIN&lt;/span&gt; &lt;span class="n"&gt;PASSWORD&lt;/span&gt; &lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;ROLE&lt;/span&gt; &lt;span class="n"&gt;analyst2&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;LOGIN&lt;/span&gt; &lt;span class="n"&gt;PASSWORD&lt;/span&gt; &lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="n"&gt;readonly_group&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;analyst1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;GRANT&lt;/span&gt; &lt;span class="n"&gt;readonly_group&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;analyst2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates two users&lt;/li&gt;
&lt;li&gt;Adds them to readonly group&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Understanding
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Roles help in managing permissions efficiently&lt;/li&gt;
&lt;li&gt;Access can be controlled at table and column level&lt;/li&gt;
&lt;li&gt;Groups simplify permission management for multiple users&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This task helped me understand how to manage users, roles, and permissions in a database. It showed how important access control is for maintaining data security and integrity.&lt;/p&gt;




</description>
      <category>database</category>
      <category>security</category>
      <category>sql</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Filter Assignments - CA32</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 29 Mar 2026 13:19:20 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/filter-assignments-ca32-49l0</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/filter-assignments-ca32-49l0</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this bonus task, I explored advanced filtering techniques in SQL using the dvdrental database. The focus was on using conditions like NULL checks, pattern matching, ranges, and logical operators.&lt;/p&gt;

&lt;p&gt;This helped me understand how to extract specific and meaningful data from a database.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Retrieve data using conditions&lt;/li&gt;
&lt;li&gt;Use operators like AND, OR, BETWEEN, IN&lt;/li&gt;
&lt;li&gt;Apply pattern matching using LIKE&lt;/li&gt;
&lt;li&gt;Work with LIMIT and OFFSET&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I thought:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Filtering is just using WHERE clause&lt;/li&gt;
&lt;li&gt;Conditions are simple&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I realized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SQL provides many ways to filter data&lt;/li&gt;
&lt;li&gt;Pattern matching is very powerful&lt;/li&gt;
&lt;li&gt;Combining conditions gives precise results&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;NULL values require special handling&lt;/li&gt;
&lt;li&gt;LIKE is used for pattern matching&lt;/li&gt;
&lt;li&gt;BETWEEN is useful for ranges&lt;/li&gt;
&lt;li&gt;LIMIT and OFFSET control output size&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Movies with NULL special features
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;special_features&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  2. Movies with rental duration more than 7
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;rental_duration&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  3. Movies with rental rate 4.99 and replacement cost &amp;gt; 20
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;replacement_cost&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  4. Movies with rental rate 0.99 or rating PG-13
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'PG-13'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  5. First 5 movies sorted by title
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&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;h3&gt;
  
  
  6. Skip 10 rows and fetch next 3 by replacement cost
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;replacement_cost&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;OFFSET&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  7. Movies with rating G, PG, or PG-13
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'G'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'PG'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'PG-13'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  8. Movies with rental rate between 2 and 4
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  9. Movies starting with The
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'The%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  10. First 10 movies with multiple conditions
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rental_rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'R'&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%Love%'&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  11. Movies containing % symbol
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%&lt;/span&gt;&lt;span class="se"&gt;\%&lt;/span&gt;&lt;span class="s1"&gt;%'&lt;/span&gt; &lt;span class="k"&gt;ESCAPE&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s1"&gt;;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  12. Movies containing underscore
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%&lt;/span&gt;&lt;span class="se"&gt;\_&lt;/span&gt;&lt;span class="s1"&gt;%'&lt;/span&gt; &lt;span class="k"&gt;ESCAPE&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="se"&gt;\'&lt;/span&gt;&lt;span class="s1"&gt;;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  13. Titles starting with A or B and ending with s
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'A%s'&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'B%s'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  14. Movies containing Man, Men, or Woman
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%Man%'&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%Men%'&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%Woman%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  15. Movies containing digits
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt; &lt;span class="s1"&gt;'[0-9]'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  16. Movies containing backslash
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s1"&gt;%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  17. Movies containing Love or Hate
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%Love%'&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%Hate%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  18. First 5 movies ending with er, or, ar
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%er'&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%or'&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="s1"&gt;'%ar'&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&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;h2&gt;
  
  
  Final Understanding
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;SQL filtering can be very precise using conditions&lt;/li&gt;
&lt;li&gt;Pattern matching helps in text-based searches&lt;/li&gt;
&lt;li&gt;Combining operators gives better control over results&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This task helped me understand advanced SQL filtering techniques. It improved my ability to work with real-world data and extract meaningful information using different conditions.&lt;/p&gt;




</description>
      <category>beginners</category>
      <category>database</category>
      <category>sql</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Select Queries from DVD Rental Database - CA31</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 29 Mar 2026 13:12:35 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/select-queries-from-dvd-rental-database-ca31-46pp</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/select-queries-from-dvd-rental-database-ca31-46pp</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this task, I worked with the dvdrental database to practice SQL queries. The focus was on retrieving, sorting, and formatting data using different SQL clauses.&lt;/p&gt;

&lt;p&gt;This helped me improve my understanding of SELECT, ORDER BY, DISTINCT, and column aliasing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Retrieve specific data from tables&lt;/li&gt;
&lt;li&gt;Use aliases for better readability&lt;/li&gt;
&lt;li&gt;Apply sorting using ORDER BY&lt;/li&gt;
&lt;li&gt;Extract unique values using DISTINCT&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I thought:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Queries are just simple SELECT statements&lt;/li&gt;
&lt;li&gt;Sorting is straightforward&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I realized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Proper formatting improves readability&lt;/li&gt;
&lt;li&gt;Sorting can involve multiple conditions&lt;/li&gt;
&lt;li&gt;DISTINCT is useful for unique data&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Aliases help make output user-friendly&lt;/li&gt;
&lt;li&gt;ORDER BY can sort using multiple columns&lt;/li&gt;
&lt;li&gt;DISTINCT removes duplicate values&lt;/li&gt;
&lt;li&gt;Combining clauses makes queries powerful&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Film titles and rental rates
&lt;/h3&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;title&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="nv"&gt;"Movie Title"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="nv"&gt;"Rate"&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  2. Customer names and email
&lt;/h3&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;first_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="nv"&gt;"First Name"&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="nv"&gt;"Last Name"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  3. Films sorted by rental rate and title
&lt;/h3&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  4. Actor names sorted
&lt;/h3&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;first_name&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;FROM&lt;/span&gt; &lt;span class="n"&gt;actor&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="k"&gt;ASC&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;ASC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  5. Unique replacement costs
&lt;/h3&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;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;replacement_cost&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  6. Film title and duration
&lt;/h3&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;length&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="nv"&gt;"Duration (min)"&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  7. Customer active status
&lt;/h3&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;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="nv"&gt;"Is Active"&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  8. Film categories sorted
&lt;/h3&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;name&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  9. Films by length descending
&lt;/h3&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;length&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;length&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  10. Actors sorted by first name descending
&lt;/h3&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;first_name&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;FROM&lt;/span&gt; &lt;span class="n"&gt;actor&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  11. Unique ratings
&lt;/h3&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;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  12. Unique rental durations
&lt;/h3&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;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;rental_duration&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  13. First unique customer by active status
&lt;/h3&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;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;active&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  14. Earliest rental date per customer
&lt;/h3&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;customer_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;MIN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rental_date&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;rental_date&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;rental&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;customer_id&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  15. 10 shortest films
&lt;/h3&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;length&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;length&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  16. Top 5 customers with highest ID
&lt;/h3&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;first_name&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;FROM&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&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;h3&gt;
  
  
  17. Unique store IDs
&lt;/h3&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;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;store_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;inventory&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  18. Unique replacement cost sorted
&lt;/h3&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;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;replacement_cost&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;replacement_cost&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  19. First rental date per store
&lt;/h3&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;store_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;MIN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rental_date&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;rental_date&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;rental&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;store_id&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;store_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  20. Unique film ratings sorted
&lt;/h3&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;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  21. Films sorted by rating and length
&lt;/h3&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;length&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;length&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  22. Actor names with mixed sorting
&lt;/h3&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;first_name&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;FROM&lt;/span&gt; &lt;span class="n"&gt;actor&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="k"&gt;ASC&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;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  23. Films by replacement cost and rental rate
&lt;/h3&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;replacement_cost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;replacement_cost&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rental_rate&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  24. Customer names sorted
&lt;/h3&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;first_name&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;FROM&lt;/span&gt; &lt;span class="n"&gt;customer&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt; &lt;span class="k"&gt;ASC&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;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  25. Rentals sorted
&lt;/h3&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="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;rental&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rental_date&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  26. Films by rental duration and title
&lt;/h3&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;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rental_duration&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;film&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;rental_duration&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Final Understanding
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;SQL queries can be combined to perform powerful operations&lt;/li&gt;
&lt;li&gt;Sorting and filtering improve data usability&lt;/li&gt;
&lt;li&gt;Aliasing makes output more readable&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This task helped me strengthen my SQL fundamentals by working with real-world queries on the dvdrental database. It improved my ability to retrieve, organize, and present data effectively.&lt;/p&gt;




</description>
    </item>
    <item>
      <title>Create Table in SQL - CA39</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 29 Mar 2026 13:03:51 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/create-table-in-sql-ca39-4bp</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/create-table-in-sql-ca39-4bp</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this task, I focused on creating database tables using SQL. Instead of modifying existing tables, the goal here was to define new tables with proper structure and constraints.&lt;/p&gt;

&lt;p&gt;This helped me understand how to design tables in a way that ensures data consistency and integrity.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create tables with appropriate columns&lt;/li&gt;
&lt;li&gt;Apply constraints like PRIMARY KEY, NOT NULL, UNIQUE, CHECK&lt;/li&gt;
&lt;li&gt;Define default values&lt;/li&gt;
&lt;li&gt;Establish relationships using foreign keys&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I thought:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creating tables is just defining columns&lt;/li&gt;
&lt;li&gt;Constraints are optional&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I realized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Constraints are important for maintaining correct data&lt;/li&gt;
&lt;li&gt;Table design plays a major role in database performance&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;PRIMARY KEY uniquely identifies each record&lt;/li&gt;
&lt;li&gt;NOT NULL ensures required fields are filled&lt;/li&gt;
&lt;li&gt;UNIQUE prevents duplicate values&lt;/li&gt;
&lt;li&gt;CHECK enforces conditions&lt;/li&gt;
&lt;li&gt;FOREIGN KEY maintains relationships&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Create students table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&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;id&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;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;100&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;id uniquely identifies each student&lt;/li&gt;
&lt;li&gt;PRIMARY KEY ensures uniqueness&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. Create employees table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;phone_number&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;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;name and email cannot be null&lt;/li&gt;
&lt;li&gt;phone_number is optional&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3. Create users table with unique fields
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;username&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="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;email&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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;username and email must be unique&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  4. Create products table with conditions
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;products&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stock&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;price must be greater than 0&lt;/li&gt;
&lt;li&gt;stock cannot be negative&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  5. Create orders table with defaults
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;status&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;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="nb"&gt;TIMESTAMP&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="k"&gt;CURRENT_TIMESTAMP&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;status defaults to pending&lt;/li&gt;
&lt;li&gt;created_at stores current time automatically&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  6. Create accounts table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;accounts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;account_number&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;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="nb"&gt;DECIMAL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;account_number must be unique and not null&lt;/li&gt;
&lt;li&gt;balance must be non-negative&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  7. Create enrollments table with composite key
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;enrollments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;student_id&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;course_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;course_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;combination of student_id and course_id must be unique&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  8. Create departments and employees with foreign key
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;departments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&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;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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&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;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&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;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;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;FOREIGN&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;department_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;departments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;department_id must exist in departments table&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  9. Foreign key with cascade actions
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;departments&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&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;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;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&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;employees&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&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;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;100&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;FOREIGN&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;department_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;departments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;deleting a department removes related employees&lt;/li&gt;
&lt;li&gt;updating department id updates employee records&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Understanding
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Table creation is not just structure, it includes rules&lt;/li&gt;
&lt;li&gt;Constraints ensure valid and consistent data&lt;/li&gt;
&lt;li&gt;Relationships connect multiple tables effectively&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This task helped me understand how to design tables properly with constraints and relationships. It also showed how important planning is while creating a database structure.&lt;/p&gt;




</description>
      <category>beginners</category>
      <category>database</category>
      <category>sql</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Alter Tables in SQL - CA40</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 29 Mar 2026 13:01:05 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/alter-tables-in-sql-ca40-2d79</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/alter-tables-in-sql-ca40-2d79</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this task, I worked on modifying existing database tables using ALTER TABLE statements. Instead of creating new tables, the goal was to update the structure of already existing tables by adding constraints, modifying columns, and enforcing rules.&lt;/p&gt;

&lt;p&gt;This helped me understand how databases can be controlled and maintained properly.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Modify existing tables using ALTER statements&lt;/li&gt;
&lt;li&gt;Apply constraints like NOT NULL, UNIQUE, CHECK&lt;/li&gt;
&lt;li&gt;Set default values&lt;/li&gt;
&lt;li&gt;Manage foreign key behavior&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I thought:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ALTER TABLE is only used to add columns&lt;/li&gt;
&lt;li&gt;Changes might be complicated&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I realized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can modify constraints as well&lt;/li&gt;
&lt;li&gt;It is very useful for maintaining data integrity&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Constraints help in controlling data&lt;/li&gt;
&lt;li&gt;ALTER TABLE allows updating structure without deleting data&lt;/li&gt;
&lt;li&gt;Each modification must be carefully applied&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Make email NOT NULL in customers table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&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;customers&lt;/span&gt;
&lt;span class="k"&gt;MODIFY&lt;/span&gt; &lt;span class="n"&gt;email&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;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures email cannot be empty in future records&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. Make username UNIQUE in users table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&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;users&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;unique_username&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prevents duplicate usernames&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3. Ensure price is greater than 0 in products table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&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;products&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;check_price&lt;/span&gt; &lt;span class="k"&gt;CHECK&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures valid product pricing&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  4. Set default value for status in orders table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&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;orders&lt;/span&gt;
&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="s1"&gt;'pending'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatically assigns pending if no value is given&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  5. Add salary column with constraints in employees table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&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;employees&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;CHECK&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;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adds salary column&lt;/li&gt;
&lt;li&gt;Ensures it is not null&lt;/li&gt;
&lt;li&gt;Ensures salary is greater than 10000&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  6. Modify foreign key with cascade delete
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&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;employees&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;fk_department&lt;/span&gt;
&lt;span class="k"&gt;FOREIGN&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;department_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;REFERENCES&lt;/span&gt; &lt;span class="n"&gt;departments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Deletes employees automatically when department is deleted&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  7. Remove CHECK constraint from accounts table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&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;accounts&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;check_balance&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Removes restriction on balance&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  8. Make user_id and transaction_id unique in payments table
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Code (SQL)
&lt;/h3&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;payments&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;unique_payment&lt;/span&gt; &lt;span class="k"&gt;UNIQUE&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;transaction_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures no duplicate transactions per user&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Final Understanding
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;ALTER TABLE is powerful for modifying structure&lt;/li&gt;
&lt;li&gt;Constraints help maintain clean and valid data&lt;/li&gt;
&lt;li&gt;Proper use of constraints improves database reliability&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This task helped me understand how to control and update database tables efficiently without recreating them. It also improved my understanding of constraints and their importance in real-world applications.&lt;/p&gt;




</description>
    </item>
    <item>
      <title>Number Guessing Game - CA03</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 22 Mar 2026 15:36:30 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/number-guessing-game-ca03-30ba</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/number-guessing-game-ca03-30ba</guid>
      <description>&lt;h2&gt;
  
  
  Objective
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Retrieve leaderboard records from the database&lt;/li&gt;
&lt;li&gt;Arrange records based on difficulty level and number of attempts&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Data Structure
&lt;/h2&gt;

&lt;p&gt;Each record in the leaderboard contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name&lt;/li&gt;
&lt;li&gt;Difficulty level&lt;/li&gt;
&lt;li&gt;Attempts count&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Collect all leaderboard entries&lt;/li&gt;
&lt;li&gt;Convert difficulty levels into comparable values&lt;/li&gt;
&lt;li&gt;Apply sorting logic using both difficulty and attempts&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Difficulty Priority
&lt;/h2&gt;

&lt;p&gt;To ensure proper ordering:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Easy is considered lowest&lt;/li&gt;
&lt;li&gt;Medium is next&lt;/li&gt;
&lt;li&gt;Hard is highest&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This helps in comparing difficulty levels during sorting.&lt;/p&gt;




&lt;h2&gt;
  
  
  Sorting Logic
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Compare two entries&lt;/li&gt;
&lt;li&gt;First check difficulty level&lt;/li&gt;
&lt;li&gt;If difficulty is same, compare attempts&lt;/li&gt;
&lt;li&gt;Swap positions if required&lt;/li&gt;
&lt;li&gt;Repeat until the list is sorted&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Code (Python)
&lt;/h2&gt;



&lt;p&gt;```python id="lb32x1"&lt;br&gt;
leaderboard = [&lt;br&gt;
    {"name": "Alice", "difficulty": "Medium", "attempts": 5},&lt;br&gt;
    {"name": "Bob", "difficulty": "Easy", "attempts": 3},&lt;br&gt;
    {"name": "Charlie", "difficulty": "Hard", "attempts": 7},&lt;br&gt;
    {"name": "David", "difficulty": "Easy", "attempts": 6}&lt;br&gt;
]&lt;/p&gt;

&lt;p&gt;difficulty_rank = {"Easy": 1, "Medium": 2, "Hard": 3}&lt;/p&gt;

&lt;p&gt;for i in range(len(leaderboard)):&lt;br&gt;
    for j in range(len(leaderboard) - i - 1):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    current = leaderboard[j]
    next_item = leaderboard[j + 1]

    if (difficulty_rank[current["difficulty"]] &amp;gt; difficulty_rank[next_item["difficulty"]]) or \
       (difficulty_rank[current["difficulty"]] == difficulty_rank[next_item["difficulty"]] and
        current["attempts"] &amp;gt; next_item["attempts"]):

        leaderboard[j], leaderboard[j + 1] = leaderboard[j + 1], leaderboard[j]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;for item in leaderboard:&lt;br&gt;
    print(item)&lt;/p&gt;

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


---

## Explanation

* Leaderboard data is stored using dictionaries
* Difficulty values are mapped to numbers for comparison
* Sorting is done using two conditions

  * First by difficulty
  * Then by attempts if difficulty is same
* Bubble sort ensures correct ordering step by step

---

## Example

Input:

* Alice, Medium, 5
* Bob, Easy, 3
* Charlie, Hard, 7
* David, Easy, 6

Output:

* Bob, Easy, 3
* David, Easy, 6
* Alice, Medium, 5
* Charlie, Hard, 7

---

## Conclusion

This implementation ensures that leaderboard data is organized clearly based on difficulty and performance. It improves readability and helps users understand rankings effectively.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>gamedev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Different Sorting Methodologies - CA17</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 22 Mar 2026 15:31:32 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/different-sorting-methodologies-ca17-4ga2</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/different-sorting-methodologies-ca17-4ga2</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this session, I learned different sorting algorithms that are used to arrange data in a specific order.&lt;/p&gt;

&lt;p&gt;Sorting is important because it helps in organizing data and improving efficiency in searching.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Understand different sorting techniques&lt;/li&gt;
&lt;li&gt;Learn how each algorithm works&lt;/li&gt;
&lt;li&gt;Analyze their performance&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I thought:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sorting is simple arrangement&lt;/li&gt;
&lt;li&gt;All methods work similarly&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But I realized:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each algorithm has a different approach&lt;/li&gt;
&lt;li&gt;Efficiency varies&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Some algorithms are easy but slow&lt;/li&gt;
&lt;li&gt;Some are complex but efficient&lt;/li&gt;
&lt;li&gt;Choice depends on problem size&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Sorting Methodologies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Bubble Sort
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Compare adjacent elements&lt;/li&gt;
&lt;li&gt;Swap if they are in wrong order&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code (Python)
&lt;/h3&gt;



&lt;p&gt;```python id="bub12"&lt;br&gt;
def bubble_sort(arr):&lt;br&gt;
    n = len(arr)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in range(n):
    for j in range(0, n - i - 1):
        if arr[j] &amp;gt; arr[j + 1]:
            arr[j], arr[j + 1] = arr[j + 1], arr[j]

return arr
&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;


Explanation:

* Repeatedly compares adjacent elements
* After each pass, largest element moves to the end

---

### 2. Selection Sort

### Logic:

* Find minimum element
* Place it at correct position

### Code (Python)



```python id="sel34"
def selection_sort(arr):
    n = len(arr)

    for i in range(n):
        min_index = i

        for j in range(i + 1, n):
            if arr[j] &amp;lt; arr[min_index]:
                min_index = j

        arr[i], arr[min_index] = arr[min_index], arr[i]

    return arr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Selects smallest element&lt;/li&gt;
&lt;li&gt;Swaps with current index&lt;/li&gt;
&lt;li&gt;Builds sorted part step by step&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  3. Insertion Sort
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Insert each element in correct position&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code (Python)
&lt;/h3&gt;



&lt;p&gt;```python id="ins56"&lt;br&gt;
def insertion_sort(arr):&lt;br&gt;
    for i in range(1, len(arr)):&lt;br&gt;
        key = arr[i]&lt;br&gt;
        j = i - 1&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    while j &amp;gt;= 0 and arr[j] &amp;gt; key:
        arr[j + 1] = arr[j]
        j -= 1

    arr[j + 1] = key

return arr
&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;


Explanation:

* Takes one element at a time
* Places it in correct position in sorted part

---

### 4. Merge Sort

### Logic:

* Divide array
* Sort halves
* Merge

### Code (Python)



```python id="mer78"
def merge_sort(arr):
    if len(arr) &amp;lt;= 1:
        return arr

    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])

    return merge(left, right)

def merge(left, right):
    result = []
    i = j = 0

    while i &amp;lt; len(left) and j &amp;lt; len(right):
        if left[i] &amp;lt; right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1

    result.extend(left[i:])
    result.extend(right[j:])

    return result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Divides array recursively&lt;/li&gt;
&lt;li&gt;Merges sorted halves&lt;/li&gt;
&lt;li&gt;Efficient for large data&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  5. Quick Sort
&lt;/h3&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Choose pivot&lt;/li&gt;
&lt;li&gt;Partition array&lt;/li&gt;
&lt;li&gt;Recursively sort&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Code (Python)
&lt;/h3&gt;



&lt;p&gt;```python id="qui90"&lt;br&gt;
def quick_sort(arr):&lt;br&gt;
    if len(arr) &amp;lt;= 1:&lt;br&gt;
        return arr&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pivot = arr[len(arr) // 2]

left = [x for x in arr if x &amp;lt; pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x &amp;gt; pivot]

return quick_sort(left) + middle + quick_sort(right)
&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;


Explanation:

* Divides array based on pivot
* Recursively sorts parts
* Faster in practice

---

## Complexity Analysis

| Algorithm      | Time Complexity |
| -------------- | --------------- |
| Bubble Sort    | O(n²)           |
| Selection Sort | O(n²)           |
| Insertion Sort | O(n²)           |
| Merge Sort     | O(n log n)      |
| Quick Sort     | O(n log n)      |

---

## Key Takeaways

* Sorting algorithms differ in efficiency
* Simple methods are easier but slower
* Advanced methods improve performance
* Choosing correct algorithm is important

---

## Conclusion

This session helped me understand different sorting methodologies and how to choose the right one based on the problem.

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

&lt;/div&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>learning</category>
    </item>
    <item>
      <title>Merge Two Sorted Lists - CA15</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 22 Mar 2026 15:24:19 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/merge-two-sorted-lists-ca15-1g19</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/merge-two-sorted-lists-ca15-1g19</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this problem, I was given two sorted linked lists and asked to merge them into a single sorted linked list.&lt;/p&gt;

&lt;p&gt;The final list should also be sorted.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Given two sorted linked lists &lt;code&gt;list1&lt;/code&gt; and &lt;code&gt;list2&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Merge them into one sorted linked list&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return the head of the merged list&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use existing nodes&lt;/li&gt;
&lt;li&gt;Maintain sorted order&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I considered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Converting both lists into arrays&lt;/li&gt;
&lt;li&gt;Merging arrays&lt;/li&gt;
&lt;li&gt;Rebuilding the linked list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But this approach uses extra space.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;p&gt;Since both lists are already sorted:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I can compare nodes one by one&lt;/li&gt;
&lt;li&gt;Attach the smaller node to the result&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Optimized Approach
&lt;/h2&gt;

&lt;p&gt;I decided to merge the lists using pointer manipulation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Compare current nodes of both lists&lt;/li&gt;
&lt;li&gt;Attach smaller node to result&lt;/li&gt;
&lt;li&gt;Move forward in that list&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Approach (Step-by-Step)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Create a dummy node&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use a pointer &lt;code&gt;curr&lt;/code&gt; for result list&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;While both lists are not empty:&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Compare values&lt;/li&gt;
&lt;li&gt;Attach smaller node&lt;/li&gt;
&lt;li&gt;Move pointer forward&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Attach remaining nodes of any list&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return &lt;code&gt;dummy.next&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code (Python)
&lt;/h2&gt;

&lt;p&gt;Below is the implementation clearly separated inside a code block:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```python id="k4p9zs"&lt;br&gt;
class ListNode:&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, val=0, next=None):&lt;br&gt;
        self.val = val&lt;br&gt;
        self.next = next&lt;/p&gt;

&lt;p&gt;class Solution:&lt;br&gt;
    def mergeTwoLists(self, list1, list2):&lt;br&gt;
        dummy = ListNode()&lt;br&gt;
        curr = dummy&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    while list1 and list2:
        if list1.val &amp;lt;= list2.val:
            curr.next = list1
            list1 = list1.next
        else:
            curr.next = list2
            list2 = list2.next
        curr = curr.next

    if list1:
        curr.next = list1
    else:
        curr.next = list2

    return dummy.next
&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;


---

## Example Walkthrough

### Input:



```text id="x2n9rf"
list1 = [1, 2, 4], list2 = [1, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Steps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Compare nodes and attach smaller values&lt;/li&gt;
&lt;li&gt;Continue until one list ends&lt;/li&gt;
&lt;li&gt;Attach remaining elements&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;p&gt;```text id="v5k3qp"&lt;br&gt;
[1, 1, 2, 3, 4, 4]&lt;/p&gt;

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


---

## Complexity Analysis

| Type             | Complexity |
| ---------------- | ---------- |
| Time Complexity  | O(n + m)   |
| Space Complexity | O(1)       |

---

## Key Takeaways

* Two pointer technique is effective
* No extra space required
* Dummy node simplifies implementation

---

## Conclusion

This problem helped me understand how to efficiently merge two sorted linked lists using pointer manipulation.

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

&lt;/div&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
      <category>computerscience</category>
      <category>interview</category>
    </item>
    <item>
      <title>Merge Sort for Linked List - CA24</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 22 Mar 2026 15:20:22 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/merge-sort-for-linked-list-ca24-27c4</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/merge-sort-for-linked-list-ca24-27c4</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this problem, I was given the head of a linked list and asked to sort it using Merge Sort.&lt;/p&gt;

&lt;p&gt;Merge Sort is efficient for linked lists because it does not require random access like arrays.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Given the head of a linked list&lt;/li&gt;
&lt;li&gt;Sort the linked list using Merge Sort&lt;/li&gt;
&lt;li&gt;Return the sorted list&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I considered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Converting the linked list into an array&lt;/li&gt;
&lt;li&gt;Sorting the array&lt;/li&gt;
&lt;li&gt;Rebuilding the linked list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But this approach uses extra space.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;p&gt;Merge Sort works well with linked lists because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can split the list into halves easily&lt;/li&gt;
&lt;li&gt;We can merge sorted lists efficiently&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Optimized Approach
&lt;/h2&gt;

&lt;p&gt;I decided to use Merge Sort with recursion.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Divide the list into two halves&lt;/li&gt;
&lt;li&gt;Recursively sort both halves&lt;/li&gt;
&lt;li&gt;Merge the sorted halves&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Approach (Step-by-Step)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Base Case:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If list is empty or has one node → return head&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Find middle of list:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Use slow and fast pointer&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Split the list into two halves&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recursively sort both halves&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Merge the two sorted lists&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Code (Python)
&lt;/h2&gt;

&lt;p&gt;Below is the implementation clearly separated inside a code block:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```python id="m7x4qp"&lt;br&gt;
class Node:&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, data):&lt;br&gt;
        self.data = data&lt;br&gt;
        self.next = None&lt;/p&gt;

&lt;p&gt;class Solution:&lt;br&gt;
    def mergeSort(self, head):&lt;br&gt;
        if not head or not head.next:&lt;br&gt;
            return head&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    mid = self.getMiddle(head)
    next_to_mid = mid.next
    mid.next = None

    left = self.mergeSort(head)
    right = self.mergeSort(next_to_mid)

    return self.sortedMerge(left, right)

def getMiddle(self, head):
    slow = head
    fast = head.next

    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    return slow

def sortedMerge(self, left, right):
    if not left:
        return right
    if not right:
        return left

    if left.data &amp;lt;= right.data:
        result = left
        result.next = self.sortedMerge(left.next, right)
    else:
        result = right
        result.next = self.sortedMerge(left, right.next)

    return result
&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;


---

## Example Walkthrough

### Input:



```text id="v9q1kz"
10 -&amp;gt; 30 -&amp;gt; 20 -&amp;gt; 40 -&amp;gt; 50 -&amp;gt; 60
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Steps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Split into halves&lt;/li&gt;
&lt;li&gt;Sort each half recursively&lt;/li&gt;
&lt;li&gt;Merge sorted halves&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;p&gt;```text id="g6x2we"&lt;br&gt;
10 -&amp;gt; 20 -&amp;gt; 30 -&amp;gt; 40 -&amp;gt; 50 -&amp;gt; 60&lt;/p&gt;

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


---

## Complexity Analysis

| Type             | Complexity |
| ---------------- | ---------- |
| Time Complexity  | O(n log n) |
| Space Complexity | O(log n)   |

---

## Key Takeaways

* Merge Sort is efficient for linked lists
* Slow and fast pointers help in splitting
* Merging step is crucial

---

## Conclusion

This problem helped me understand how to apply merge sort on linked lists efficiently without using extra space for arrays.

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

&lt;/div&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Remove Duplicates from a Sorted Linked List - CA23</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 22 Mar 2026 15:17:58 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/remove-duplicates-from-a-sorted-linked-list-ca23-37pj</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/remove-duplicates-from-a-sorted-linked-list-ca23-37pj</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this problem, I was given a sorted singly linked list and asked to remove duplicate nodes.&lt;/p&gt;

&lt;p&gt;Since the list is sorted, duplicate values appear next to each other.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Given the head of a sorted linked list&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Remove duplicate nodes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return the updated list&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Conditions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not use extra space&lt;/li&gt;
&lt;li&gt;Maintain original order&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I considered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using a set to store values&lt;/li&gt;
&lt;li&gt;Rebuilding the list with unique elements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But this approach uses extra space.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;p&gt;Because the list is sorted:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Duplicate elements are adjacent&lt;/li&gt;
&lt;li&gt;I only need to compare current node with next node&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Optimized Approach
&lt;/h2&gt;

&lt;p&gt;I decided to traverse the list once and remove duplicates in-place.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If current node value equals next node value
→ skip the next node&lt;/li&gt;
&lt;li&gt;Else
→ move forward&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Approach (Step-by-Step)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Start with &lt;code&gt;curr = head&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Traverse while &lt;code&gt;curr&lt;/code&gt; and &lt;code&gt;curr.next&lt;/code&gt; exist&lt;/li&gt;
&lt;li&gt;At each step:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;curr.data == curr.next.data&lt;/code&gt;
 → remove duplicate
 → &lt;code&gt;curr.next = curr.next.next&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Else
 → move to next node&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Code (Python)
&lt;/h2&gt;

&lt;p&gt;Below is the implementation clearly separated inside a code block:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```python id="k9f2xw"&lt;br&gt;
class Node:&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, data):&lt;br&gt;
        self.data = data&lt;br&gt;
        self.next = None&lt;/p&gt;

&lt;p&gt;class Solution:&lt;br&gt;
    def removeDuplicates(self, head):&lt;br&gt;
        curr = head&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    while curr and curr.next:
        if curr.data == curr.next.data:
            curr.next = curr.next.next
        else:
            curr = curr.next

    return head
&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;


---

## Example Walkthrough

### Input:



```text id="x6p3lm"
2 -&amp;gt; 2 -&amp;gt; 4 -&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Steps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Compare 2 and 2 → remove duplicate&lt;/li&gt;
&lt;li&gt;Move forward and compare remaining nodes&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;p&gt;```text id="r2g7zs"&lt;br&gt;
2 -&amp;gt; 4 -&amp;gt; 5&lt;/p&gt;

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


---

## Complexity Analysis

| Type             | Complexity |
| ---------------- | ---------- |
| Time Complexity  | O(n)       |
| Space Complexity | O(1)       |

---

## Key Takeaways

* Sorted property simplifies the problem
* No extra space is required
* Pointer manipulation is important

---

## Conclusion

This problem helped me understand how to efficiently remove duplicates from a linked list using in-place operations.

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

&lt;/div&gt;

</description>
    </item>
    <item>
      <title>Reverse a Linked List - CA22</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 22 Mar 2026 15:14:28 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/reverse-a-linked-list-ca22-4i2e</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/reverse-a-linked-list-ca22-4i2e</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this problem, I was given the head of a linked list and asked to reverse the list.&lt;/p&gt;

&lt;p&gt;After reversing, I need to return the new head of the linked list.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Given the head of a linked list&lt;/li&gt;
&lt;li&gt;Reverse the linked list&lt;/li&gt;
&lt;li&gt;Return the new head&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I considered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Storing elements in an array&lt;/li&gt;
&lt;li&gt;Reversing the array&lt;/li&gt;
&lt;li&gt;Rebuilding the linked list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But this approach uses extra space.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;p&gt;Instead of using extra space:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I can reverse the links between nodes&lt;/li&gt;
&lt;li&gt;Change the direction of pointers&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Optimized Approach
&lt;/h2&gt;

&lt;p&gt;I decided to reverse the list using pointer manipulation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Keep track of previous, current, and next nodes&lt;/li&gt;
&lt;li&gt;Reverse the link at each step&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Approach (Step-by-Step)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Initialize:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;prev = None&lt;/li&gt;
&lt;li&gt;curr = head&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Traverse the list:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Store next node&lt;/li&gt;
&lt;li&gt;Reverse current node link&lt;/li&gt;
&lt;li&gt;Move prev and curr forward&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;At the end:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;prev will be the new head&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Code (Python)
&lt;/h2&gt;

&lt;p&gt;Below is the implementation clearly separated inside a code block:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```python id="8x3q1n"&lt;br&gt;
class ListNode:&lt;br&gt;
    def &lt;strong&gt;init&lt;/strong&gt;(self, val=0, next=None):&lt;br&gt;
        self.val = val&lt;br&gt;
        self.next = next&lt;/p&gt;

&lt;p&gt;class Solution:&lt;br&gt;
    def reverseList(self, head):&lt;br&gt;
        prev = None&lt;br&gt;
        curr = head&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    while curr:
        next_node = curr.next
        curr.next = prev
        prev = curr
        curr = next_node

    return prev
&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;


---

## Example Walkthrough

### Input:



```text id="z4s8mk"
1 -&amp;gt; 2 -&amp;gt; 3 -&amp;gt; 4 -&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Steps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Reverse links one by one&lt;/li&gt;
&lt;li&gt;Final list becomes reversed&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;p&gt;```text id="b0h9pe"&lt;br&gt;
5 -&amp;gt; 4 -&amp;gt; 3 -&amp;gt; 2 -&amp;gt; 1&lt;/p&gt;

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


---

## Complexity Analysis

| Type             | Complexity |
| ---------------- | ---------- |
| Time Complexity  | O(n)       |
| Space Complexity | O(1)       |

---

## Key Takeaways

* Pointer manipulation is important in linked lists
* No extra space is required
* Iterative approach is efficient

---

## Conclusion

This problem helped me understand how to reverse a linked list efficiently using pointer manipulation.

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

&lt;/div&gt;

</description>
    </item>
    <item>
      <title>Majority Element - CA21</title>
      <dc:creator>Lokeshwaran S</dc:creator>
      <pubDate>Sun, 22 Mar 2026 15:10:58 +0000</pubDate>
      <link>https://forem.com/lokeshwaran_s_db77da73b51/majority-element-ca21-1gbk</link>
      <guid>https://forem.com/lokeshwaran_s_db77da73b51/majority-element-ca21-1gbk</guid>
      <description>&lt;h2&gt;
  
  
  My Thinking and Approach
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this problem, I was given an array and asked to find the majority element.&lt;/p&gt;

&lt;p&gt;A majority element is one that appears more than n/2 times in the array.&lt;/p&gt;




&lt;h2&gt;
  
  
  Problem Statement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Given an array &lt;code&gt;arr&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Find the element that appears more than n/2 times&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If no such element exists:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Return -1&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Initial Thought
&lt;/h2&gt;

&lt;p&gt;At first, I considered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using a hashmap to count frequencies&lt;/li&gt;
&lt;li&gt;Returning the element with highest count&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But this approach uses extra space.&lt;/p&gt;




&lt;h2&gt;
  
  
  Key Observation
&lt;/h2&gt;

&lt;p&gt;There can be at most one majority element.&lt;/p&gt;

&lt;p&gt;This allows a more optimized approach without extra space.&lt;/p&gt;




&lt;h2&gt;
  
  
  Optimized Approach
&lt;/h2&gt;

&lt;p&gt;I decided to use Moore’s Voting Algorithm.&lt;/p&gt;

&lt;h3&gt;
  
  
  Logic:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Maintain a candidate and count&lt;/li&gt;
&lt;li&gt;Increase count if same element appears&lt;/li&gt;
&lt;li&gt;Decrease count if different element appears&lt;/li&gt;
&lt;li&gt;Final candidate may be majority&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  My Approach (Step-by-Step)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Initialize:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;candidate = None&lt;/li&gt;
&lt;li&gt;count = 0&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Traverse array:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;If count == 0:
 → set candidate = current element&lt;/li&gt;
&lt;li&gt;If element == candidate:
 → count += 1&lt;/li&gt;
&lt;li&gt;Else:
 → count -= 1&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Verify candidate:&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;Count occurrences of candidate&lt;/li&gt;
&lt;li&gt;If count &amp;gt; n/2 → return candidate&lt;/li&gt;
&lt;li&gt;Else → return -1&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Code (Python)
&lt;/h2&gt;

&lt;p&gt;Below is the implementation clearly separated inside a code block:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```python id="y3sk9q"&lt;br&gt;
class Solution:&lt;br&gt;
    def majorityElement(self, arr):&lt;br&gt;
        candidate = None&lt;br&gt;
        count = 0&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    for num in arr:
        if count == 0:
            candidate = num
        if num == candidate:
            count += 1
        else:
            count -= 1

    count = 0
    for num in arr:
        if num == candidate:
            count += 1

    if count &amp;gt; len(arr) // 2:
        return candidate
    return -1
&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;


---

## Example Walkthrough

### Input:



```text id="l4u2qg"
arr = [1, 1, 2, 1, 3, 5, 1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Steps:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Candidate becomes 1&lt;/li&gt;
&lt;li&gt;Count stabilizes with majority&lt;/li&gt;
&lt;li&gt;Verify count &amp;gt; n/2&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Output:
&lt;/h3&gt;



&lt;p&gt;```text id="qv6y2x"&lt;br&gt;
1&lt;/p&gt;

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


---

## Complexity Analysis

| Type             | Complexity |
| ---------------- | ---------- |
| Time Complexity  | O(n)       |
| Space Complexity | O(1)       |

---

## Key Takeaways

* Only one majority element can exist
* Moore’s Voting Algorithm is optimal
* Verification step is important

---

## Conclusion

This problem helped me understand how to find the majority element efficiently using a constant space approach.

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

&lt;/div&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>interview</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
