SQL : Top Three Wineries - Fill in the blanks too
Video 44: In this tutorial, we explore the process of arranging a list of wineries based on their points, specifically targeting the top three wineries. The challenge lies in formatting the output: 1. Adjacent Column Output: The top three wineries are arranged within the same row, each in an adjacent column. 2. Formatted Winery Names: Each winery name includes its respective points. 3. Handling Missing Wineries: If there isn't a second or third winery, placeholders "No second winery" and "No third winery" are displayed. 4. Resolving Tiebreakers: In cases where multiple wineries share the same points, we sort based on the winery name to determine their positions. We employed window functions to create a rank() based on winery points, enabling us to order by both points and winery names. Leveraging this rank as a key column, we utilized CASE statements and MAX() functions to pivot rows into columns. We also discussed the nuances and potential pitfalls when using these functions in tandem. We explained an alternate approach using a standard self-join, emphasizing the complexity of sorting wineries with identical points. Handling scenarios where multiple wineries possess the same points adds intricacy to the sorting process. For detailed SQL code and supplementary sample data, please refer to the GitHub file provided below. code: https://github.com/jeganpillai/sql_reference/blob/main/v0022_top_three_wineries.sql Follow me on, Website : https://growwithdata.co/ YouTube : https://www.youtube.com/@growwithdata TikTok : https://www.tiktok.com/@growwithdata LinkedIn : https://www.linkedin.com/company/growwithdata/ Facebook : https://www.facebook.com/growwithdata.co/ twitter : https://twitter.com/growwithdata_co Instagram : https://www.instagram.com/growwithdata.co/ WhatsApp : https://whatsapp.com/channel/0029VaF8pkb77qVNfbp5pA0S #sql #dataengineers #tablejoins #ceil #floor #bucket #meta #google #facebook #apple #paypal #netflix #amazon #deinterview #sqlinterview #interviewquestions #leetcode #faang #maanga #mysql #oracle #dbms #query #sqlserver #mysql #coderpad #aggregates #aggregation #nonaggregation #database #placementpreparation #lead #lag #windowsfunction #nullcheck #coalesce #sqlperformance #ifnull #case
Download
0 formatsNo download links available.