Temotec Data Science, ML & Data Engineering: Interview Notes - Projects - Courses.’s Post

📢 Hello LinkedIn community! 🌟💼 🌟 Day 38/50 Days of SQL Challenge 🌟 SQL Bootcamp 2024: Master SQL & PostgreSQL - Hands-On Course https://lnkd.in/gaHnijmg SQL Course 2024: SQL for Data Analysis and Data Science. https://lnkd.in/g45cbiXa Today, let's solve a SQL challenge that involves finding the largest single number from a table of numbers. 🧮🔢 We have a table called MyNumbers with a single column num of type int. The table may contain duplicate numbers, and there is no primary key defined for this table. The result format is in the following example. Example 1: Input: MyNumbers table: +-----+ | num | +-----+ | 8   | | 8   | | 3   | | 3   | | 1   | | 4   | | 5   | | 6   | +-----+ Output: +-----+ | num | +-----+ | 6   | +-----+ Explanation: The single numbers are 1, 4, 5, and 6. Since 6 is the largest single number, we return it. Example 2: Input: MyNumbers table: +-----+ | num | +-----+ | 8   | | 8   | | 7   | | 7   | | 3   | | 3   | | 3   | +-----+ Output: +------+ | num  | +------+ | null | +------+ Explanation: There are no single numbers in the input table so we return null. Here's the SQL query that will provide the desired result: SELECT MAX(num) AS num FROM (   SELECT num   FROM MyNumbers   GROUP BY num   HAVING COUNT(*) = 1 ) AS SingleNumbers; In this query, we use a subquery to find all the numbers that appear exactly once in the MyNumbers table. The subquery selects the distinct numbers (num) using the GROUP BY clause and filters out the groups with a count of 1 using the HAVING clause. The outer query then selects the maximum (MAX) value from the single numbers. If there are no single numbers, the MAX function will return NULL. Example result 1: +-----+ | num | +-----+ | 6  | +-----+ In this example, the single numbers are 1, 4, 5, and 6. Since 6 is the largest single number, it is returned. Example result 2: +------+ | num | +------+ | null | +------+ In this example, there are no single numbers in the input table, so the result is NULL. Feel free to try out this query on your own database or the provided sample data to see the results. Happy coding! 💻🔢 #SQL hashtag #Databases hashtag #DataAnalysis hashtag #LeetCode hashtag #SQL hashtag #DataAnalysis hashtag #DataManipulation hashtag #LinkedInLearning hashtag #ContinuousLearning hashtag #Temotec hashtag #TemotecAcademy hashtag #TamerAhmed hashtag #50DaysOFSQL hashtag #50daysofsqls hashtag #SQLChallenges hashtag #DataAnalysis hashtag #SQLQueries hashtag #DataManipulation hashtag #ProblemSolving hashtag #DataDrivenInsights hashtag

  • No alternative text description for this image

To view or add a comment, sign in

Explore topics