8 August 2023
Guest Blog β SQL Framework
π Following my last post, I want to continue to have a more detailed framework for the query language branch.
Imagine that I have several tables to extract data, what data do I want?
πͺ The most frequent steps are:
Step 1. Choose tables with FROMΒ Β Β Β Β Β Β Β Β
Step 2. Add conditions with WHERE (=, non-equals, BETWEEN, IN, LIKE)
Step 3. Choose columns with SELECT and simple aggregation {COUNT, MAX, MIN, AVG, SUM, DISTINCT}
Step 4. ORDER BY to sort out the rows and LIMIT to choose the specific amount from the beginning
π The possible scenarios are:
Option 1. Merge columns into one table with JOIN {LEFT, SELF, FULL, CROSS} and ON (=), USING
Β Β Β Β Β Β Β Β Β Β Merge rows into one table with UNION {INTERSECT, EXCEPT}
Option 2. Classify all rows into groups for calculation and comparison {GROUP BY, HAVING}
Option 3. Add new aggregated or transformed columns in temporary WINDOWs and add new values to each row, WINDOW can be one if ignores the PARTITION BY
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Aggregation functions MIN / MAX/ AVG/ SUM (β¦) OVER(PARTITION BYβ¦)
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Ranking in groups RANK()/ DENSE_RANK() OVER(PARTITION BYβ¦ ORDER BYβ¦)
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Move the value up or down LEAD/LAG (β¦, offset) OVER(ORDER BYβ¦)
               Create a WINDOW related to the current row OVER(ORDER BY⦠ROWS/RANGE/GROUPS BETWEEN {n /UNBOUNDED PRECEDING, CURRENT ROW} AND { n /UNBOUNDED FOLLOWING, CURRENT ROW})

πββοΈ The special techniques are:
Technique 1: Use () to combine queries, called subquery, the latter can use the output of the former one
Technique 2: Create Common Table Expression (CTE) to create a temporary table for simplification WITH table_name AS (β¦β¦)
Technique 3: Define a Window then OVER it in SELECT clause {WINDOW window_name AS (PARTITION BYβ¦)}
β With this, I can solve problems by combining them together. The more questions I solve, the more functions I will know, and the framework will be enriched.Β #sql #learningandgrowing Β Β Β Β Β Β Β Β Β Β Β
Β
Follow up Guest Blog by Ivy Li who has been reskilling and is Actively looking for a ππππ ππ§ππ₯π²π¬π role – check out her website here.
If any of our members or women looking to get into or are already in the tech industry and wish to share their knowledge then please feel free to do a guest blog with us – we’re here to support the community.

