{"id":6408,"date":"2025-09-07T06:50:37","date_gmt":"2025-09-07T06:50:37","guid":{"rendered":"https:\/\/techtrendfeed.com\/?p=6408"},"modified":"2025-09-07T06:50:38","modified_gmt":"2025-09-07T06:50:38","slug":"most-candidates-fail-these-sql-ideas-in-information-interviews","status":"publish","type":"post","link":"https:\/\/techtrendfeed.com\/?p=6408","title":{"rendered":"Most Candidates Fail These SQL Ideas in Information Interviews"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div id=\"post-\">\n<p>    <center><img decoding=\"async\" alt=\"Most Candidates Fail These SQL Concepts in Data Interviews\" width=\"100%\" class=\"perfmatters-lazy\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-1.png\"\/><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-1.png\" alt=\"Most Candidates Fail These SQL Concepts in Data Interviews\" width=\"100%\"\/><br \/><span>Picture by writer | Canva<\/span><\/center><br \/>\n\u00a0<\/p>\n<p>An interviewer&#8217;s job is to search out essentially the most appropriate candidates for the marketed place. In doing so, they are going to gladly arrange SQL interview inquiries to see if they&#8217;ll catch you off guard. There are a number of SQL ideas at which candidates typically fail.<\/p>\n<p>Hopefully, you\u2019ll be a type of who keep away from that future, as I\u2019ll clarify these ideas intimately under, full with examples of easy methods to resolve sure issues appropriately.<\/p>\n<p>\u00a0<br \/><img decoding=\"async\" alt=\"Most Candidates Fail These SQL Concepts in Data Interviews\" width=\"100%\" class=\"perfmatters-lazy\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-2-scaled.png\"\/><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-2-scaled.png\" alt=\"Most Candidates Fail These SQL Concepts in Data Interviews\" width=\"100%\"\/><br \/>\u00a0<\/p>\n<h2><span>#\u00a0<\/span>1. Window Capabilities<\/h2>\n<p>\u00a0<br \/>Why It\u2019s Laborious: Candidates memorize what every <strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.stratascratch.com\/blog\/the-ultimate-guide-to-sql-window-functions\/?utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\">window perform<\/a><\/strong> does however don\u2019t actually perceive how window frames, partitions, or ordering truly work.<\/p>\n<p>Widespread Errors: A standard mistake shouldn&#8217;t be specifying <code style=\"background: #F5F5F5;\">ORDER BY<\/code> in rating window capabilities or worth window capabilities, similar to <strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/learnsql.com\/blog\/lead-and-lag-functions-in-sql\/\" target=\"_blank\"><code>LEAD()<\/code> or <code>LAG()<\/code><\/a><\/strong>, and anticipating the question to work or for the end result to be deterministic.<\/p>\n<p>Instance: In <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/platform.stratascratch.com\/coding\/10553-finding-purchases?code_type=1&amp;utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\">this instance,<\/a> you might want to discover customers who made a second buy inside 7 days of any earlier buy.<\/p>\n<p>You may write this question.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>WITH ordered_tx AS (&#13;\n\u00a0 SELECT user_id,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0created_at::date AS tx_date,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0LAG(created_at::DATE) OVER (PARTITION BY user_id) AS prev_tx_date&#13;\n\u00a0 FROM amazon_transactions&#13;\n)&#13;\n&#13;\nSELECT DISTINCT user_id&#13;\nFROM ordered_tx&#13;\nWHERE prev_tx_date IS NOT NULL AND tx_date - prev_tx_date &lt;= 7;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>At first look, every little thing might sound proper. The code even outputs one thing that may seem like an accurate reply.<\/p>\n<p>\u00a0<br \/><img decoding=\"async\" alt=\"Window Functions\" width=\"100%\" class=\"perfmatters-lazy\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-3.1.png\"\/><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-3.1.png\" alt=\"Window Functions\" width=\"100%\"\/><br \/>\u00a0<\/p>\n<p>To begin with, we&#8217;re fortunate that the code works in any respect! This occurs just because I\u2019m writing it in <strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.postgresql.org\/\" target=\"_blank\">PostgreSQL<\/a><\/strong>. In another SQL flavors, you\u2019d get an error since <code style=\"background: #F5F5F5;\">ORDER BY<\/code> is necessary in rating and analytical window capabilities.<\/p>\n<p>Second, the output is unsuitable; I highlighted some rows that shouldn\u2019t be there. Why do they seem, then?<\/p>\n<p>They seem as a result of we didn\u2019t specify an <code style=\"background: #F5F5F5;\">ORDER BY<\/code> clause within the <code style=\"background: #F5F5F5;\">LAG()<\/code> window perform. With out it, the row order is bigoted. So, we&#8217;re evaluating the present transaction to some random earlier row for that person, not the one which occurred instantly earlier than it in time.<\/p>\n<p>This isn&#8217;t what the query asks. We have to evaluate every transaction to the earlier one by date. In different phrases, we have to specify this explicitly within the <code style=\"background: #F5F5F5;\">ORDER BY<\/code> clause throughout the <code style=\"background: #F5F5F5;\">LAG()<\/code> perform.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>WITH ordered_tx AS (&#13;\n\u00a0 SELECT user_id,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0created_at::date AS tx_date,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0LAG(created_at::DATE) OVER (PARTITION BY user_id ORDER BY created_at) AS prev_tx_date&#13;\n\u00a0 FROM amazon_transactions&#13;\n)&#13;\n&#13;\nSELECT DISTINCT user_id&#13;\nFROM ordered_tx&#13;\nWHERE prev_tx_date IS NOT NULL AND tx_date - prev_tx_date &lt;= 7;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<h2><span>#\u00a0<\/span>2. Filtering With Aggregates (Particularly HAVING vs. WHERE)<\/h2>\n<p>\u00a0<br \/>Why It\u2019s Laborious: Folks typically don\u2019t perceive the execution order in SQL, which is: <code style=\"background: #F5F5F5;\">FROM<\/code> -&gt; <code style=\"background: #F5F5F5;\">WHERE<\/code> -&gt; <code style=\"background: #F5F5F5;\">GROUP BY<\/code> -&gt; <code style=\"background: #F5F5F5;\">HAVING<\/code> -&gt; <code style=\"background: #F5F5F5;\">SELECT<\/code> -&gt; <code style=\"background: #F5F5F5;\">ORDER BY<\/code>. This order implies that <strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.shiksha.com\/online-courses\/articles\/difference-between-where-and-having-clause\/\" target=\"_blank\"><code>WHERE<\/code> filters rows earlier than aggregation, and <code>HAVING<\/code> filters after<\/a><\/strong>. That additionally, logically, means that you would be able to\u2019t use combination capabilities within the <code style=\"background: #F5F5F5;\">WHERE<\/code> clause.<\/p>\n<p>Widespread Mistake: Making an attempt to make use of combination capabilities in <code style=\"background: #F5F5F5;\">WHERE<\/code> in a grouped question and getting an error.<\/p>\n<p>Instance: <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/platform.stratascratch.com\/coding\/10030-total-wine-revenue?code_type=1&amp;utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\">This interview query<\/a> asks you to search out the whole income made by every vineyard. Solely wineries the place 90 is the bottom variety of factors for any of their varieties needs to be thought of.<\/p>\n<p>Many will see this as a simple query and unexpectedly write this question.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>SELECT vineyard,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0selection,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0SUM(worth) AS total_revenue&#13;\nFROM winemag_p1&#13;\nWHERE MIN(factors) &gt;= 90&#13;\nGROUP BY vineyard, selection&#13;\nORDER BY vineyard, total_revenue DESC;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>Nonetheless, that code will throw an error stating that combination capabilities will not be allowed within the <code style=\"background: #F5F5F5;\">WHERE<\/code> clause. This beautiful a lot explains every little thing. The answer? Transfer the filtering situation from <code style=\"background: #F5F5F5;\">WHERE<\/code> to <code style=\"background: #F5F5F5;\">HAVING<\/code>.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>SELECT vineyard,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0selection,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0SUM(worth) AS total_revenue&#13;\nFROM winemag_p1&#13;\nGROUP BY vineyard, selection&#13;\nHAVING MIN(factors) &gt;= 90&#13;\nORDER BY vineyard, total_revenue DESC;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<h2><span>#\u00a0<\/span>3. Self-Joins for Time-Based mostly or Occasion-Based mostly Comparisons<\/h2>\n<p>\u00a0<br \/>Why It\u2019s Laborious: The thought of <strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.stratascratch.com\/blog\/illustrated-guide-about-self-join-in-sql\/?utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\">becoming a member of a desk with itself<\/a><\/strong> is sort of unintuitive, so candidates typically neglect it\u2019s an choice.<\/p>\n<p>Widespread Mistake: Utilizing subqueries and complicating the question when becoming a member of a desk with itself can be less complicated and sooner, particularly when filtering by dates or occasions.<\/p>\n<p>Instance: Right here\u2019s a <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/platform.stratascratch.com\/coding\/2063-change-of-currency-exchange-rates?code_type=1&amp;utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\">query<\/a> asking you to point out the change of each foreign money\u2019s change price between 1 January 2020 and 1 July 2020.<\/p>\n<p>You may resolve this by writing an outer correlated subquery that fetches the July 1 change charges, then subtracts the January 1 change charges, which come from the inside subquery.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>SELECT jan_rates.source_currency,&#13;\n\u00a0 (SELECT exchange_rate\u00a0&#13;\n\u00a0 \u00a0FROM sf_exchange_rate\u00a0&#13;\n\u00a0 \u00a0WHERE source_currency = jan_rates.source_currency AND date=\"2020-07-01\") - jan_rates.exchange_rate AS distinction&#13;\nFROM (SELECT source_currency, exchange_rate&#13;\n\u00a0 \u00a0 \u00a0 FROM sf_exchange_rate&#13;\n\u00a0 \u00a0 \u00a0 WHERE date=\"2020-01-01\"&#13;\n) AS jan_rates;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>This returns an accurate output, however such an answer is unnecessarily sophisticated. A a lot less complicated answer, with fewer strains of code, includes self-joining a desk with itself after which making use of two date filtering situations within the <code style=\"background: #F5F5F5;\">WHERE<\/code> clause.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>SELECT jan.source_currency,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0jul.exchange_rate - jan.exchange_rate AS distinction&#13;\nFROM sf_exchange_rate jan&#13;\nJOIN sf_exchange_rate jul ON jan.source_currency = jul.source_currency&#13;\nWHERE jan.date=\"2020-01-01\" AND jul.date=\"2020-07-01\";<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<h2><span>#\u00a0<\/span>4. Subqueries vs. Widespread Desk Expressions (CTEs)<\/h2>\n<p>\u00a0<br \/>Why It\u2019s Laborious: Folks typically get caught on subqueries as a result of they be taught them earlier than Widespread Desk Expressions (CTEs) and proceed utilizing them for any question with layered logic. Nonetheless, subqueries can get messy in a short time.<\/p>\n<p>Widespread Mistake: Utilizing deeply nested <code style=\"background: #F5F5F5;\">SELECT<\/code> statements when CTEs can be a lot less complicated.<\/p>\n<p>Instance: Within the <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/platform.stratascratch.com\/coding\/10548-top-actor-ratings-by-genre?code_type=1&amp;utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\">interview query<\/a> from Google and Netflix, you might want to discover the highest actors based mostly on their common film score throughout the style through which they seem most ceaselessly.<\/p>\n<p>The answer utilizing CTEs is as follows.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>WITH genre_stats AS&#13;\n\u00a0 (SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0FROM top_actors_rating&#13;\n\u00a0 \u00a0GROUP BY actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 style),&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0&#13;\nmax_genre_count AS&#13;\n\u00a0 (SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 MAX(movie_count) AS max_count&#13;\n\u00a0 \u00a0FROM genre_stats&#13;\n\u00a0 \u00a0GROUP BY actor_name),&#13;\n\u00a0 \u00a0 \u00a0&#13;\ntop_genres AS&#13;\n\u00a0 (SELECT gs.*&#13;\n\u00a0 \u00a0FROM genre_stats gs&#13;\n\u00a0 \u00a0JOIN max_genre_count mgc ON gs.actor_name = mgc.actor_name&#13;\n\u00a0 \u00a0AND gs.movie_count = mgc.max_count),&#13;\n\u00a0 \u00a0 \u00a0&#13;\ntop_genre_avg AS&#13;\n\u00a0 (SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 MAX(avg_rating) AS max_avg_rating&#13;\n\u00a0 \u00a0FROM top_genres&#13;\n\u00a0 \u00a0GROUP BY actor_name),&#13;\n\u00a0 \u00a0&#13;\nfiltered_top_genres AS&#13;\n\u00a0 (SELECT tg.*&#13;\n\u00a0 \u00a0FROM top_genres tg&#13;\n\u00a0 \u00a0JOIN top_genre_avg tga ON tg.actor_name = tga.actor_name&#13;\n\u00a0 \u00a0AND tg.avg_rating = tga.max_avg_rating),&#13;\n\u00a0 \u00a0 \u00a0ranked_actors AS&#13;\n\u00a0 (SELECT *,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 DENSE_RANK() OVER (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0ORDER BY avg_rating DESC) AS rank&#13;\n\u00a0 \u00a0FROM filtered_top_genres),&#13;\n\u00a0 \u00a0&#13;\nfinal_selection AS&#13;\n\u00a0 (SELECT MAX(rank) AS max_rank&#13;\n\u00a0 \u00a0FROM ranked_actors&#13;\n\u00a0 \u00a0WHERE rank &lt;= 3)&#13;\n\u00a0 \u00a0&#13;\nSELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0avg_rating&#13;\nFROM ranked_actors&#13;\nWHERE rank &lt;=&#13;\n\u00a0 \u00a0 (SELECT max_rank&#13;\n\u00a0 \u00a0 \u00a0FROM final_selection);&#13;\n<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>It&#8217;s comparatively sophisticated, but it surely nonetheless consists of six clear CTEs, with the code&#8217;s readability enhanced by clear aliases.<\/p>\n<p>Curious what the identical answer would appear to be utilizing solely subqueries? Right here it&#8217;s.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>SELECT ra.actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0ra.style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0ra.avg_rating&#13;\nFROM (&#13;\n\u00a0 \u00a0 SELECT *,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0DENSE_RANK() OVER (ORDER BY avg_rating DESC) AS rank&#13;\n\u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 SELECT tg.*&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT gs.*&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM top_actors_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name, style&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS gs&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 JOIN (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0MAX(movie_count) AS max_count&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM top_actors_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name, style&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS genre_stats&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS mgc&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ON gs.actor_name = mgc.actor_name AND gs.movie_count = mgc.max_count&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 ) AS tg&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 JOIN (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0MAX(avg_rating) AS max_avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT gs.*&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM top_actors_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name, style&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS gs&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 JOIN (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0MAX(movie_count) AS max_count&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM top_actors_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name, style&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS genre_stats&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS mgc&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ON gs.actor_name = mgc.actor_name AND gs.movie_count = mgc.max_count&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS top_genres&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 ) AS tga&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 ON tg.actor_name = tga.actor_name AND tg.avg_rating = tga.max_avg_rating&#13;\n\u00a0 \u00a0 ) AS filtered_top_genres&#13;\n) AS ra&#13;\nWHERE ra.rank &lt;= (&#13;\n\u00a0 \u00a0 SELECT MAX(rank)&#13;\n\u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 SELECT *,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0DENSE_RANK() OVER (ORDER BY avg_rating DESC) AS rank&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT tg.*&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT gs.*&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM top_actors_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name, style&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS gs&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 JOIN (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0MAX(movie_count) AS max_count&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM top_actors_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name, style&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS genre_stats&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS mgc&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ON gs.actor_name = mgc.actor_name AND gs.movie_count = mgc.max_count&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS tg&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 JOIN (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0MAX(avg_rating) AS max_avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT gs.*&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM top_actors_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name, style&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS gs&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 JOIN (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0MAX(movie_count) AS max_count&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM (&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SELECT actor_name,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0style,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0COUNT(*) AS movie_count,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0AVG(movie_rating) AS avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 FROM top_actors_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name, style&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS genre_stats&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS mgc&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ON gs.actor_name = mgc.actor_name AND gs.movie_count = mgc.max_count&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS top_genres&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 GROUP BY actor_name&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ) AS tga&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ON tg.actor_name = tga.actor_name AND tg.avg_rating = tga.max_avg_rating&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 ) AS filtered_top_genres&#13;\n\u00a0 \u00a0 ) AS ranked_actors&#13;\n\u00a0 \u00a0 WHERE rank &lt;= 3&#13;\n);<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>There&#8217;s redundant logic repeated throughout subqueries. What number of subqueries is that? I do not know. The code is unimaginable to keep up. Though I simply wrote it, I\u2019d nonetheless want half a day to grasp it if I needed to alter one thing tomorrow. Moreover, the utterly meaningless subquery aliases don&#8217;t assist.<\/p>\n<p>\u00a0<\/p>\n<h2><span>#\u00a0<\/span>5. Dealing with NULLs in Logic<\/h2>\n<p>\u00a0<br \/>Why It\u2019s Laborious: Candidates typically suppose that <code style=\"background: #F5F5F5;\">NULL<\/code> is the same as one thing. It\u2019s not. <code style=\"background: #F5F5F5;\">NULL<\/code> isn\u2019t equal to something \u2014 not even itself. Logic involving <code style=\"background: #F5F5F5;\">NULL<\/code>s behaves in another way from logic involving precise values.<\/p>\n<p>Widespread Mistake: Utilizing <code style=\"background: #F5F5F5;\">= NULL<\/code> as an alternative of <code style=\"background: #F5F5F5;\">IS NULL<\/code> in filtering or lacking output rows as a result of <code style=\"background: #F5F5F5;\">NULL<\/code>s break the situation logic.<\/p>\n<p>Instance: There\u2019s an <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/platform.stratascratch.com\/coding\/10542-interaction-summary?code_type=1&amp;utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\">interview query by IBM<\/a> that asks you to calculate the whole variety of interactions and the whole variety of contents created for every buyer.<\/p>\n<p>It doesn\u2019t sound too difficult, so that you may write this answer with two CTEs, the place one CTE counts the variety of interactions per buyer, whereas the opposite counts the variety of content material objects created by a buyer. Within the remaining <code style=\"background: #F5F5F5;\">SELECT<\/code>, you <code style=\"background: #F5F5F5;\">FULL OUTER JOIN<\/code> the 2 CTEs, and you&#8217;ve got the answer. Proper?<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>WITH interactions_summary AS&#13;\n\u00a0 (SELECT customer_id,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 COUNT(*) AS total_interactions&#13;\n\u00a0 \u00a0FROM customer_interactions&#13;\n\u00a0 \u00a0GROUP BY customer_id),&#13;\n\u00a0 \u00a0&#13;\ncontent_summary AS&#13;\n\u00a0 (SELECT customer_id,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 COUNT(*) AS total_content_items&#13;\n\u00a0 \u00a0FROM user_content&#13;\n\u00a0 \u00a0GROUP BY customer_id)&#13;\n\u00a0 \u00a0&#13;\nSELECT i.customer_id,&#13;\n\u00a0 i.total_interactions,&#13;\n\u00a0 c.total_content_items&#13;\nFROM interactions_summary AS i&#13;\nFULL OUTER JOIN content_summary AS c ON i.customer_id = c.customer_id&#13;\nORDER BY customer_id;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>Virtually proper. Right here\u2019s the output. (By the best way, you see double citation marks (&#8220;&#8221;) as an alternative of <code style=\"background: #F5F5F5;\">NULL<\/code>. That\u2019s how the StrataScratch UI shows it, however belief me, the engine nonetheless treats them for what they&#8217;re: <code style=\"background: #F5F5F5;\">NULL<\/code> values).<\/p>\n<p>\u00a0<br \/><img decoding=\"async\" alt=\"5. Handling NULLs in Logic\" width=\"100%\" class=\"perfmatters-lazy\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-4-scaled.png\"\/><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-4-scaled.png\" alt=\"5. Handling NULLs in Logic\" width=\"100%\"\/><br \/>\u00a0<\/p>\n<p>The highlighted rows comprise <code style=\"background: #F5F5F5;\">NULL<\/code>s. This makes the output incorrect. A <code style=\"background: #F5F5F5;\">NULL<\/code> worth is neither the shopper ID nor the variety of interactions and contents, which the query explicitly asks you to point out.<\/p>\n<p>What we\u2019re lacking within the above answer is <strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/www.stratascratch.com\/blog\/sql-coalesce-function-a-guide-for-postgresql-users\/?utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\"><code>COALESCE()<\/code><\/a><\/strong> to deal with <code style=\"background: #F5F5F5;\">NULL<\/code>s within the remaining <code style=\"background: #F5F5F5;\">SELECT<\/code>. Now, all the purchasers with out interactions will get their IDs from the <code style=\"background: #F5F5F5;\">content_summary<\/code> CTE. Additionally, for purchasers that don\u2019t have interactions, or content material, or each, we\u2019ll now exchange <code style=\"background: #F5F5F5;\">NULL<\/code> with 0, which is a sound quantity.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>WITH interactions_summary AS&#13;\n\u00a0 (SELECT customer_id,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 COUNT(*) AS total_interactions&#13;\n\u00a0 \u00a0FROM customer_interactions&#13;\n\u00a0 \u00a0GROUP BY customer_id),&#13;\n\u00a0 \u00a0&#13;\ncontent_summary AS&#13;\n\u00a0 (SELECT customer_id,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 COUNT(*) AS total_content_items&#13;\n\u00a0 \u00a0FROM user_content&#13;\n\u00a0 \u00a0GROUP BY customer_id)&#13;\n\u00a0 \u00a0&#13;\nSELECT COALESCE(i.customer_id, c.customer_id) AS customer_id,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0COALESCE(i.total_interactions, 0) AS total_interactions,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0COALESCE(c.total_content_items, 0) AS total_content_items&#13;\nFROM interactions_summary AS i&#13;\nFULL OUTER JOIN content_summary AS c ON i.customer_id = c.customer_id&#13;\nORDER BY customer_id;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<h2><span>#\u00a0<\/span>6. Group-Based mostly Deduplication<\/h2>\n<p>\u00a0<br \/>Why It\u2019s Laborious: Group-based deduplication means you\u2019re choosing one row per group, e.g., \u201cmost up-to-date\u201d, \u201chighest rating\u201d, and so on. At first, it sounds such as you solely want to choose one row per person. However you may\u2019t use <code style=\"background: #F5F5F5;\">GROUP BY<\/code> except you combination. Alternatively, you typically want a full row, not a single worth that aggregation and <code style=\"background: #F5F5F5;\">GROUP BY<\/code> return.<\/p>\n<p>Widespread Mistake: Utilizing <code style=\"background: #F5F5F5;\">GROUP BY<\/code> + <code style=\"background: #F5F5F5;\">LIMIT 1<\/code> (or <strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/neon.com\/postgresql\/postgresql-tutorial\/postgresql-distinct-on\" target=\"_blank\">DISTINCT ON<\/a><\/strong>, which is PostgreSQL-specific) as an alternative of <code style=\"background: #F5F5F5;\">ROW_NUMBER()<\/code> or <code style=\"background: #F5F5F5;\">RANK()<\/code>, the latter in order for you ties included.<\/p>\n<p>Instance: <a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/platform.stratascratch.com\/coding\/10172-best-selling-item?code_type=1&amp;utm_source=blog&amp;utm_medium=click&amp;utm_campaign=kdn+candidates+fail+these+sql+concepts\" target=\"_blank\">This query<\/a> asks you to establish the best-selling merchandise for every month, and there\u2019s no have to separate months by 12 months. One of the best-selling merchandise is calculated as <code style=\"background: #F5F5F5;\">unitprice * amount<\/code>.<\/p>\n<p>The naive strategy can be this. First, extract the sale month from <code style=\"background: #F5F5F5;\">invoicedate<\/code>, choose <code style=\"background: #F5F5F5;\">description<\/code>, and discover the whole gross sales by summing <code style=\"background: #F5F5F5;\">unitprice * amount<\/code>. Then, to get the whole gross sales by month and product description, we merely <code style=\"background: #F5F5F5;\">GROUP BY<\/code> these two columns. Lastly, we solely want to make use of <code style=\"background: #F5F5F5;\">ORDER BY<\/code> to kind the output from the very best to the worst-selling product and use <code style=\"background: #F5F5F5;\">LIMIT 1<\/code> to output solely the primary row, i.e., the best-selling merchandise.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>SELECT DATE_PART('MONTH', invoicedate) AS sale_month,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0description,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0SUM(unitprice * amount) AS total_paid&#13;\nFROM online_retail&#13;\nGROUP BY sale_month, description&#13;\nORDER BY total_paid DESC&#13;\nLIMIT 1;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>As I stated, that is naive; the output considerably resembles what we&#8217;d like, however we&#8217;d like this for each month, not only one.<\/p>\n<p>\u00a0<br \/><img decoding=\"async\" alt=\"Group-Based Deduplication\" width=\"100%\" class=\"perfmatters-lazy\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-5-scaled.png\"\/><img decoding=\"async\" src=\"https:\/\/www.kdnuggets.com\/wp-content\/uploads\/Rosidi-Most_Candidates_Fail_These_SQL_Concepts-5-scaled.png\" alt=\"Group-Based Deduplication\" width=\"100%\"\/><br \/>\u00a0<\/p>\n<p>One of many appropriate approaches is to make use of the <code style=\"background: #F5F5F5;\">RANK()<\/code> window perform. With this strategy, we observe an analogous technique to the earlier code. The distinction is that the question now turns into a subquery within the <code style=\"background: #F5F5F5;\">FROM<\/code> clause. As well as, we use <code style=\"background: #F5F5F5;\">RANK()<\/code> to partition the information by month after which rank the rows inside every partition (i.e., for every month individually) from the best-selling to the worst-selling merchandise.<\/p>\n<p>Then, in the principle question, we merely choose the required columns and output solely rows the place the rank is 1 utilizing the <code style=\"background: #F5F5F5;\">WHERE<\/code> clause.<\/p>\n<div style=\"width: 98%; overflow: auto; padding-left: 10px; padding-bottom: 10px; padding-top: 10px; background: #F5F5F5;\">\n<pre><code>SELECT month,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0description,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0total_paid&#13;\nFROM&#13;\n\u00a0 (SELECT DATE_PART('month', invoicedate) AS month,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 description,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 SUM(unitprice * amount) AS total_paid,&#13;\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 RANK() OVER (PARTITION BY DATE_PART('month', invoicedate) ORDER BY SUM(unitprice * amount) DESC) AS rnk&#13;\n\u00a0 \u00a0FROM online_retail&#13;\n\u00a0 \u00a0GROUP BY month, description) AS tmp&#13;\nWHERE rnk = 1;<\/code><\/pre>\n<\/div>\n<p>\u00a0<\/p>\n<p>\u00a0<\/p>\n<h2><span>#\u00a0<\/span>Conclusion<\/h2>\n<p>\u00a0<br \/>The six ideas we\u2019ve lined generally seem in SQL coding interview questions. Take note of them, follow interview questions that contain these ideas, be taught the right approaches, and also you\u2019ll considerably enhance your probabilities in your interviews.<br \/>\u00a0<br \/>\u00a0<\/p>\n<p><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/twitter.com\/StrataScratch\" rel=\"noopener\"><b><strong><a rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/twitter.com\/StrataScratch\" target=\"_blank\" rel=\"noopener noreferrer\">Nate Rosidi<\/a><\/strong><\/b><\/a> is an information scientist and in product technique. He is additionally an adjunct professor educating analytics, and is the founding father of StrataScratch, a platform serving to information scientists put together for his or her interviews with actual interview questions from high firms. Nate writes on the most recent traits within the profession market, provides interview recommendation, shares information science initiatives, and covers every little thing SQL.<\/p>\n<\/p><\/div>\n<p><template id="YFeR0KPSFeHtWq2tWRuS"></template><\/script><br \/>\n<br \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Picture by writer | Canva \u00a0 An interviewer&#8217;s job is to search out essentially the most appropriate candidates for the marketed place. In doing so, they are going to gladly arrange SQL interview inquiries to see if they&#8217;ll catch you off guard. There are a number of SQL ideas at which candidates typically fail. Hopefully, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":6410,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[5210,1893,157,5211,451,1808],"class_list":["post-6408","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-machine-learning","tag-candidates","tag-concepts","tag-data","tag-fail","tag-interviews","tag-sql"],"_links":{"self":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/6408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6408"}],"version-history":[{"count":1,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/6408\/revisions"}],"predecessor-version":[{"id":6409,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/posts\/6408\/revisions\/6409"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=\/wp\/v2\/media\/6410"}],"wp:attachment":[{"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtrendfeed.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- This website is optimized by Airlift. Learn more: https://airlift.net. Template:. Learn more: https://airlift.net. Template: 69d9690a190636c2e0989534. Config Timestamp: 2026-04-10 21:18:02 UTC, Cached Timestamp: 2026-05-18 23:05:34 UTC -->