🚀 SQL Tip: Defining a window frame with ROWS!
Use ROWS BETWEEN start_point AND end_point to select rows relative to the current row:
1. n PRECEDING: n rows before
2. n FOLLOWING: n rows after
3. UNBOUNDED: All rows before/after
#sql#data#dataengineering#windowfunction
I solved this hard category question from @Analyst_Builder in Python. @Analyst_Builder is the platform of @Alex_TheAnalyst and I really enjoyed solving this question.
The question asked to get the highest score of student across various courses. And if the maximum score is same then we need to select the marks with smallest course id.
Now, Let's discuss how I solved it:
-- Firstly, I created a dataframe "df" to get the maximum score of each student from the original dataframe and to get that I grouped dataframe according to "student_name" and then used "transform" function to get "maximum" grades.
-- Then I created another dataframe named as "df_merged" and stored the merged data between original dataframe and "df" by using inner join. This allowed me to get the effective and required maximum marks of student along with other columns like "student_name", "class_id" and "grades".
-- Then I created a "rank" column in "df_merged" dataframe where I grouped the data according to grade and then performed "dense rank" operation on "class_id". This allowed me to first group the data with grades (I was having maximum grades already" and with dense rank on "class_id", I was able to rank then according to smallest "class_id" when grades were same.
-- Then I created last dataframe named as "df_final" where I used ".query()" to select only those rows that have "1" as rank.
-- Then I displayed the required columns and sorted them by "student_name".
#DataAnalyst#python#pandas#AnalystBuilder#mysql#postgresql#sql#mysql#CTE#windowfunction#BusinessAnalyst#sql#dataanalysisus#techjobsus#BusinessIntelligence#BI#BusinessAnalytics#USJobs#USA#USHiring#USCompanies#AmericanBusiness#USTech#FinTech#HealthcareAnalytics#RetailAnalytics#MarketingAnalytics#Networking#ProfessionalDevelopment#CareerGrowth#IndustryInsights
SQL Window Function Tip ⚡
What are the rows and range Between in a Frame Clause?
It is used to determine the upper and lower bounds of the window.
Extremely powerful because it enables you to include or exclude sets of rows! 🚀
#SQL#Programming#BigData#WindowFunction
ALT What is the rows and range Between in a Frame Clause?
Are you trying to get the latest/earliest event in a table of events? Use the ROW_NUMBER window function with ORDER BY the event creation time with QUALIFY row_num = 1.
#SQL#dataengineering#windowfunction#dataprocessing
To finalize this great Window Function week, I created a comprehensive blog post on my website! 🚀
It will cover introductory concepts and demonstrate real-world examples with PySpark!
Link in the first comment!👇🏻
#SQL#PySpark#DataEngineering#Spark#WindowFunction
SQL Window Function Tip ⚡
What is the rows and range Between in a Frame Clause?
It is used to determine the upper and lower bounds of the window.
Extremely powerful because it enables you to include or exclude sets of rows! 🚀
#SQL#Programming#BigData#WindowFunction#ETL
ALT What is the rows and range Between in a Frame Clause?
🔢 Have you ever needed to number the records in an SQL result set?
💡 Find out how SQL’s ROW_NUMBER function can help! Also learn about more online courses and articles that will develop your SQL window function skills.
#SQL#WindowFunction#Resources#LearnSQL
⬇️Read more⬇️