<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jordan Gamba</title>
    <description>The latest articles on DEV Community by Jordan Gamba (@jordan_gamba).</description>
    <link>https://dev.to/jordan_gamba</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%2F1170780%2Fa702a02a-0049-41fa-a627-09704cadd68a.jpg</url>
      <title>DEV Community: Jordan Gamba</title>
      <link>https://dev.to/jordan_gamba</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jordan_gamba"/>
    <language>en</language>
    <item>
      <title>Optimizing Database Performance with Views Functions and Triggers in PostgreSQL</title>
      <dc:creator>Jordan Gamba</dc:creator>
      <pubDate>Tue, 12 Mar 2024 04:34:25 +0000</pubDate>
      <link>https://dev.to/jordan_gamba/optimizing-database-performance-with-views-functions-and-triggers-in-postgresql-3c6</link>
      <guid>https://dev.to/jordan_gamba/optimizing-database-performance-with-views-functions-and-triggers-in-postgresql-3c6</guid>
      <description>&lt;h3&gt;
  
  
  &lt;u&gt;Introduction&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;Views, Functions, and Triggers are important tools that are used to maintain databases and ensure that information is kept under wraps without interference.&lt;/p&gt;

&lt;p&gt;Let's start with the definition of each term.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;u&gt;Views&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Views&lt;/em&gt; are a virtual table that is created from a saved query. They prevent tampering or interference with data and ensure that any adjustment made is done with permission from the database administrator. They also reduce repetition and increase the simplicity of your code.&lt;/p&gt;

&lt;p&gt;The syntax of &lt;em&gt;Views&lt;/em&gt; are as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create or replace view view_name as
select column1,column_2
from your_dataset_in_your_table
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
Let's say we want to change the information of a particular sales department in our 'sales dataset' containing the following columns:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      - ✔️ Sales_name
      - ✔️ Sales_id
      - ✔️ Sales_company
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;For example if we want to change only the &lt;code&gt;sales_id&lt;/code&gt; of a particular company without interfering with other rows in the dataset we can do the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create or replace view sales_view_name_you_want
select sales_name,
sales company
from sales_dataset
where sales_id =1
with local check option

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;with local check option&lt;/code&gt; restricts any update done on the &lt;em&gt;View&lt;/em&gt; above.&lt;/p&gt;

&lt;p&gt;Let's see what changes we can implement on other CRUD operations.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;u&gt;UPDATE&lt;/u&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;## We can decide to update the particular view 
update sales_view_name_you_want
set company_name = 'Bobbyaxelrod99'
where sales_id = 1

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  &lt;u&gt;INSERT&lt;/u&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Insert into sales_view_name_you_want (sales_name,sales_company,sales_id)
values ('jordan','Apple',1)

insert into sales_view_name_you_want(sales_name,sales_company,sales_id)
values ('John','' Amazon',2)

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

&lt;/div&gt;



&lt;p&gt;The first query in the insert operation above runs while the second query doesn't because of our &lt;code&gt;with local check option&lt;/code&gt; that we had inserted earlier in the first code. This restricts our changes to only sales_id = 1.&lt;/p&gt;

&lt;p&gt;The same logic applies to other CRUD operations that we might want to introduce. &lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Advantages of Views&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Security&lt;/strong&gt; - Views restrict access to certain columns or rows thus allowing users to see only what the database administrator intends for them to see.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Reusability&lt;/strong&gt; - Views can be used across all queries thus reducing the need for duplicate code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version Control&lt;/strong&gt; - Views can be used to control changes made to a particular set of queries without making changes to other code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query Simplification&lt;/strong&gt; - Views make it easier for users to manipulate data without needing to understand the underlying data schema&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Disadvantages of Views&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Limited Portability&lt;/strong&gt; - Views may not always be portable across different database management systems (DBMS). While SQL standards define views, different DBMS implementations may have variations in their support for certain view features or syntax, limiting portability between systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Complexity Maintenance&lt;/strong&gt; - Managing and maintaining views, especially in a dynamic database environment where underlying tables may change, can become challenging and error-prone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging and Troubleshooting&lt;/strong&gt; -  Identifying issues, such as performance bottlenecks or incorrect query results, may require tracing through multiple layers of abstraction introduced by views, making troubleshooting more challenging&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's move on to functions.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;u&gt;Functions&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Functions&lt;/em&gt; are blocks of code that accept parameters, perform a series of operations, and return a value.&lt;br&gt;
Examples of &lt;em&gt;Functions&lt;/em&gt; in PostgreSQL include: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;sum() &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;lower() &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;upper() &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We have to first understand the syntax of a &lt;em&gt;Function&lt;/em&gt; in PostgreSQL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create or replace function 
function_name( column_name1 numeric,column_name2 numeric,
              places_of_decimals integer default 1)
returns numeric as
' select ( column_name1 - column_name2)::,places_of_decimals);'
language sql
immutable
returns null or null input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function above can be broken down into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;function_name&lt;/code&gt;- this is the name of the function which contains 
a list of arguments.Each argument in the parenthensis has a data 
type.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;default 1&lt;/code&gt; - indicates that we want only one value to be 
displayed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;returns numeric&lt;/code&gt; - returns the function as numeric&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;language sql&lt;/code&gt; - it pecifies that it is written in sql&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;immutable&lt;/code&gt; - it indicates that the function won't be making any 
changes to the database.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;u&gt;So how do we know it works?&lt;/u&gt;
&lt;/h3&gt;

&lt;p&gt;We can check the &lt;em&gt;Function&lt;/em&gt; if it works by simply&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select function_name(argument1,argument2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Advantages of Functions&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance Optimization&lt;/strong&gt;- Functions can improve performance by allowing you to pre-compile and optimize frequently executed code segments. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reusability&lt;/strong&gt;- Once defined, functions can be reused in multiple queries, reducing code duplication and promoting consistency across your database application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encapsulation&lt;/strong&gt;- Functions allow you to encapsulate and modularize complex or frequently used logic, making your code more organized and easier to maintain.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Disadvantages of Functions&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Deployment Challenges&lt;/strong&gt; - Managing versions and deployments of functions can be challenging, particularly in environments with multiple database instances or distributed systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dependency Management&lt;/strong&gt; - Functions can create dependencies between different parts of the database schema. Changes to a function's signature or behavior may require updates to dependent objects, such as views, and triggers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging Complexity&lt;/strong&gt; - Errors in function logic or parameter handling may be harder to identify and troubleshoot, particularly if the function is part of a larger application or system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After Views and functions, we can now move on to triggers.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;u&gt;Triggers&lt;/u&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Triggers&lt;/em&gt; are used to execute a function when a specified event occurs like a CRUD operation. We can set a trigger right before, after, or instead of an event occurring. It is used as a shortcut particularly when we have a large query that executes a given operation one by one.&lt;/p&gt;

&lt;p&gt;The power of &lt;em&gt;Triggers&lt;/em&gt; is that it helps us to execute a given instruction all at once. &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;u&gt;Types of Triggers&lt;/u&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;1.&lt;/strong&gt; &lt;strong&gt;&lt;u&gt;Row-level Triggers&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt; &lt;em&gt;Triggers&lt;/em&gt;: These triggers fire before the triggering event (e.g., Insert, Update, Delete) occurs on each affected row. They can be used to modify data before it is written to the table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;After&lt;/strong&gt; &lt;em&gt;Triggers&lt;/em&gt;: These triggers fire after the triggering event has occurred and after any associated constraints have been checked. They can be used for logging or auditing purposes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2.&lt;/strong&gt;  &lt;strong&gt;&lt;u&gt;Statement Triggers&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Before&lt;/strong&gt; &lt;em&gt;Triggers&lt;/em&gt;: These triggers fire once for each SQL statement that triggers them, regardless of the number of affected rows. They can be used for validation or to modify the query's behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;After&lt;/strong&gt; &lt;em&gt;Triggers&lt;/em&gt;: These triggers also fire once for each SQL statement but after the statement has been executed. They are commonly used for actions that don't need to be performed for each affected row individually.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;How to create Triggers&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Step 1&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a Function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create or replace function _trigger_name_()
returns trigger as
$$
begin
(your statement depending on the CRUD operation you are performing)

Return NEW;
END;
$$ LANGUAGE plpgsql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Step 2&lt;/u&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Create a trigger depending on your CRUD operation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create trigger _trigger_you_want_
after (CRUD Operation you want to perform)
ON _interested_row_you_want_to_be_manipulated_
for each row
execute procedure _trigger_name_ ()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Step 3&lt;/u&gt;&lt;/strong&gt; &lt;br&gt;
Test the trigger to ensure it is working.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This can be done by running the query check if the instruction you have given has been compiled in the code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Advantages of Triggers&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Validation and Integrity&lt;/strong&gt; - &lt;em&gt;Triggers&lt;/em&gt; allow you to validate data before it is inserted, updated, or deleted from a table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Maintenance&lt;/strong&gt; - &lt;em&gt;Triggers&lt;/em&gt; allow you to maintain derived or calculated data fields automatically. For example, you can use triggers to update summary statistics, generate denormalized views.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Constraint Enforcement&lt;/strong&gt; - &lt;em&gt;Triggers&lt;/em&gt; provide a mechanism for enforcing complex constraints that cannot be expressed using standard SQL constraints alone. For example, you can use &lt;em&gt;Triggers&lt;/em&gt; to enforce referential integrity across multiple tables or to implement custom validation logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Disadvantages of Triggers&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Transaction Control&lt;/strong&gt; - &lt;em&gt;Triggers&lt;/em&gt; may interfere with transaction control mechanisms, such as commit and rollback operations. Depending on when triggers are fired and their behavior, they may cause unexpected side effects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintenance Complexity&lt;/strong&gt; - &lt;em&gt;Triggers&lt;/em&gt; add complexity to the database schema and codebase, making it harder to understand and maintain the system over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging Challenges&lt;/strong&gt; - Debugging &lt;em&gt;Triggers&lt;/em&gt; can be challenging, as they execute automatically in response to database events. Tracking down errors or unexpected behavior in trigger logic may require specialized debugging tools or techniques.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can summarize the Views, Functions, and Triggers through a cheat sheet.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Feature&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;View&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Function&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Trigger&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Definition&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Virtual table that you can create by using a saved query&lt;/td&gt;
&lt;td&gt;Blocks of code that perform a specific task and can be invoked by SQL queries, procedural languages, or other functions&lt;/td&gt;
&lt;td&gt;These are Procedural code blocks that automatically execute in response to specified events occurring on database tables or views&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Have minimal performance overhead when querying them, as they are essentially saved SQL queries that are executed when the view is accessed&lt;/td&gt;
&lt;td&gt;Have varying performance characteristics depending on factors such as the complexity of the function logic, the volume of data processed&lt;/td&gt;
&lt;td&gt;Have a significant performance impact, especially if they are invoked frequently or involve complex logic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Suitability&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;They are used for complex queries&lt;/td&gt;
&lt;td&gt;They are used for encapsulating complex computations&lt;/td&gt;
&lt;td&gt;Enforcing data integrity constraints, implementing complex business rules, and automating tasks that need to be executed in response to database events&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;&lt;u&gt;Conclusion&lt;/u&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;All in all Views, Functions, and Triggers are important tools to use when you are scrutinizing data which requires time and patience to master.&lt;/p&gt;

</description>
      <category>database</category>
      <category>beginners</category>
      <category>sql</category>
      <category>discuss</category>
    </item>
    <item>
      <title>A simple Mall Analysis project using Postgresql 👨‍💻</title>
      <dc:creator>Jordan Gamba</dc:creator>
      <pubDate>Sat, 06 Jan 2024 19:42:03 +0000</pubDate>
      <link>https://dev.to/jordan_gamba/a-simple-mall-analysis-project-using-posgesql-2dd7</link>
      <guid>https://dev.to/jordan_gamba/a-simple-mall-analysis-project-using-posgesql-2dd7</guid>
      <description>&lt;h2&gt;
  
  
  &lt;u&gt;Introduction&lt;/u&gt;
&lt;/h2&gt;

&lt;p&gt;I created a simple data analytics project that analyzes data from  mall dataset I got from Kaggle.The goal of this project was to qury simple business questions that would arise when client wants to build mall in a particular geographic areas.&lt;/p&gt;

&lt;p&gt;Here are simple questions I queried the data:&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;&lt;u&gt;Performance Metrics&lt;/u&gt;&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;What are the top-performing malls based on the rank or other performance indicators?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How does the gross leasable area (GLA) correlate with the overall performance of the malls?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Are there trends or patterns in the ranking of malls over the years?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;b. &lt;u&gt;&lt;strong&gt;Geographic Analysis&lt;/strong&gt;&lt;/u&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Which countries and cities have the highest concentration of malls in the dataset?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is there a correlation between the location (country or city) and the performance or size of the malls?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;c. &lt;u&gt;&lt;strong&gt;Market Penetration&lt;/strong&gt;&lt;/u&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How many shops, on average, are there in each mall?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Is there a correlation between the number of shops and the success of a mall?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;d. &lt;u&gt;&lt;strong&gt;Temporal Analysis&lt;/strong&gt;&lt;/u&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Are there trends in the year of mall opening? Does the opening year correlate with performance?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How has the gross leasable area (GLA) changed over the years in newly opened malls?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;e. &lt;u&gt;&lt;strong&gt;Expansion Opportunities&lt;/strong&gt;&lt;/u&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Can you identify trends in the opening of new malls over the years?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Are there regions or cities where the client could potentially open new malls based on market demand? &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;f. &lt;u&gt;&lt;strong&gt;Customer Experience&lt;/strong&gt;&lt;/u&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Can you estimate the average shopping area per shop to understand customer experience?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How might the distribution of shops influence the overall shopping experience for customers?&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I will attempt to go step by step to answer the questions above.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We must first load the dataset into our database &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I used the &lt;a href="https://www.kaggle.com/datasets/drahulsingh/largest-malls"&gt;largest mall dataset&lt;/a&gt; from kaggle&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create table largest_malls(
Rank smallint,
Mall varchar(50),
Country varchar(50),
City varchar(60),
Year_Opened smallint,
Gross_leasable_area varchar(100),
Shops bigint
);

drop table largest_malls

COPY largest_malls
from 'D:\Data Science e-books\EDA  SQL Project\Largest-Malls.csv'
WITH (Format csv,header);

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

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;&lt;strong&gt;Performance Metrics&lt;/strong&gt; &lt;/u&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--  Ranking the top 10 malls 
select rank,Mall
from largest_malls
limit 10

-- #Ranking the Country with the highest no.of malls
select Country,count(*)
from largest_malls
group by Country
order by count(*) desc
limit 10

-- How does the gross leasable area (GLA) correlate with the overall performance of the malls?
select rank, Mall, Gross_leasable_area
from largest_malls
limit 10
-- ###The malls with the highest GLA are the ones with the highest rank.

-- Are there trends or patterns in the ranking of malls over the years?
select Country,count(*)
from largest_malls
group by Country
order by count(*) desc
limit 5

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

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;&lt;strong&gt;Geographic Analysis&lt;/strong&gt; &lt;/u&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In the previous code I discovered this:

-- Phillipines has the highest amount of malls in the world
-- Phillipines has 10 malls.
-- Compared to the rest of the countries the Philippines has a smaller land mass as US,China. 
-- The reason that keeps popping up is because Philippines has a hot and humid climate. 
-- Therefore it makes sense to build malls in order ffor people to chill wwhen the Temperature goes up

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb91jjfvj6zw5n768d91e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb91jjfvj6zw5n768d91e.png" alt="Image description" width="521" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="![count%20of%20no%20of%20malls%20in%20each%20country](https://github.com/JORDANGAMBA99/Mall-Analysis-using-Postgresql/assets/112898413/2d856d46-19cb-40c8-8ab9-9a10ae1f7089)"&gt;count of no of malls in each country&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;Market Penetration&lt;/strong&gt;&lt;/u&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- I used median to depict the number of shops in each mall in the dataset
-- The answer was 300
select round(avg(shops),2) as "Average_Shops",
percentile_cont(.5)
within group (order by shops) as "Median_shops "
from largest_malls

-- Is there a correlation between the number of shops and the success of a mall?

-- There are so many determinants of mall success. 
-- In our data set we have a few columns that we can’t actually have any metric to measure the healthiness of a  mall . 
-- However here are a few factors that are used to measure the healthiness of a mall.

-- This include:
-- Retail mix
-- -Entertainment
--  Dining Options

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Temporal Analysis&lt;/u&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
-- Are there trends in the year of mall opening? Does the opening year correlate with performance?
-- I could not find an appropriate answer to Qn 1.
-- However using our mall success based on the number of shops we can make an assumption on the performance.

-- How has the gross leasable area (GLA) changed over the years in newly opened malls?
-- I was able to find the answer on Qn 2.
-- I exported the query to Excel

copy(
select Mall,Country,City,Year_opened,Gross_leasable_area
from largest_malls
order by Year_opened asc
TO 'C:\Data Science e-books\EDA  SQL Project\MallS_over_the_Years.csv'
WITH(FORMAT CSV,HEADER,DELIMITER ',');

-- There is generally a flat trend over the years the except when the Iran mall was built in 2018.
-- The years have been arranged in ascending order.

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ykgg7v3ndbs4zbz8vty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ykgg7v3ndbs4zbz8vty.png" alt="Image description" width="706" height="204"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Expansion Opportunities&lt;/u&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select Rank,Country,Year_Opened,shops
from largest_malls
where Year_opened &amp;gt;2010
order by shops desc

-- I would advise to open malls in the Asian market but specifically in the middle East. 
-- Because as much as these countries are oil dependent they would be moving away from fossil fuels in the future thus focusing on tourism and Renewables.
-- These economies have also shown to loose their social and restrictive rules towards their own. This Liberisation of women could open up new opportunities
-- Therefore, Opening up more shops would be a smart move also because the local purchasing power is not weak and will continue to grow


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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Customer Experience&lt;/u&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- Can you estimate the average shopping area per shop to understand customer experience?
-- How might the distribution of shops influence the overall shopping experience for customers?

-- We exported a query into an excel file and therefore performed data cleaning for the Gross leasable area from being a character to an integer

copy(
select Country,City,Gross_leasable_Area,Year_Opened,shops
from largest_malls
order by Year_Opened asc)
TO 'C:\Data Science e-books\EDA  SQL Project\Malls_Opened_Over_the_Years.csv'
WITH(FORMAT CSV,DELIMITER',')

-- We then created another Table in the Postgresql Database
Create table Malls_Opened_over_the_Years(
   Country varchar(50),
   City varchar(60),
   Gross_leasable_Area_in_m2 bigint,
   Year_Opened smallint,
   Shops smallint
)
-- Imported the data that we had

COPY Malls_Opened_Over_the_Years
from 'C:\Data Science e-books\EDA  SQL Project\Malls_Opened_Over_the_Years.csv'
WITH(FORMAT CSV,HEADER)

-- The average shopping area per shop per country

select Country,avg(round((Shops/Gross_leasable_Area_in_m2::numeric(10,1))*1000,4)) as avg_shop_per_Country  
from Malls_Opened_Over_the_Years
Group by Country 

-- How might the distribution of shops affect the customer experience?
-- Customer Experience in a shopping mall is affected by various factors:
-- •  Quality of service
-- •  Product costs
-- •  Distribution of shops to customers
-- All these affect the customer experience. It should be noted all these factors have to be harmonized in order for the customer experience to be positive. 
-- All negative deviation of one of this factors can jeopardize the customer experience and it will require for the rest of these other factors to read just in order for the customer experience to remain positive

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Conclusion&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
This project is limited in that it is not too complex to conduct analysis.However,it is beginner friendly and it is easy to understand for anyone starting out.&lt;/p&gt;

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