👇 MySQL 4 solutions | very easy 🔥 if | having | join
\uD83D\uDE4B\uD83C\uDFFB\u200D\u2640\uFE0F Hello, here are my solutions to the problem.\n### Please upvote to motivate me post future solutions. HAPPY CODING \u
### \uD83D\uDE4B\uD83C\uDFFB\u200D\u2640\uFE0F Hello, here are my solutions to the problem.\n### Please upvote to motivate me post future solutions. HAPPY CODING \u2764\uFE0F\n##### Any suggestions and improvements are always welcome.\n##### Solution 1: If, join, long \uD83E\uDD26\uD83C\uDFFB\u200D\u2640\uFE0F\n##### \u2705 Runtime: 1642 ms, faster than 32.63% of MySQL .\n\n```\nwith q as (\nselect \n    product_id,\n    sum(if(sale_date between \'2019-01-01\' and \'2019-03-31\', 1, 0)) sold_in_period,\n    sum(if(sale_date not between \'2019-01-01\' and \'2019-03-31\', 1, 0)) sold_not_in_period\nfrom sales\ngroup by 1\nhaving sold_in_period > 0 and sold_not_in_period = 0\n)\nselect\n    p.product_id,\n    product_name\nfrom product p join q\non p.product_id = q.product_id\n```\n##### Solution 2: Same, but withoun join with from twice and work faster \uD83E\uDD37\uD83C\uDFFB\u200D\u2640\uFE0F\n##### \u2705 Runtime: 1016 ms, faster than 88.22% of MySQL.\n\n```\n# Write your MySQL query statement below\nwith q as (\nselect \n    product_id,\n    sum(if(sale_date between \'2019-01-01\' and \'2019-03-31\', 1, 0)) sold_in_period,\n    sum(if(sale_date not between \'2019-01-01\' and \'2019-03-31\', 1, 0)) sold_not_in_period\nfrom sales\ngroup by 1\nhaving sold_in_period > 0 and sold_not_in_period = 0\n)\nselect\n    p.product_id,\n    product_name\nfrom product p, q\nwhere p.product_id = q.product_id\n```\n##### Solution 3: Same, with in close \uD83E\uDD37\uD83C\uDFFB\u200D\u2640\uFE0F\n##### \u2705 Runtime: 1157 ms, faster than 67.46% of MySQL.\n\n```\nwith q as (\nselect \n    product_id,\n    sum(if(sale_date between \'2019-01-01\' and \'2019-03-31\', 1, 0)) sold_in_period,\n    sum(if(sale_date not between \'2019-01-01\' and \'2019-03-31\', 1, 0)) sold_not_in_period\nfrom sales\ngroup by 1\nhaving sold_in_period > 0 and sold_not_in_period = 0\n)\nselect\n    product_id,\n    product_name\nfrom product\nwhere product_id in (select product_id from q)\n```\n##### Solution 4: MIn and max, with join, work fast, but you can do it with where close or in close (work longer)  \uD83E\uDD37\uD83C\uDFFB\u200D\u2640\uFE0F\n##### I really don\'t  like joins, but work faster\n##### \u2705 Runtime: 996 ms, faster than 91.25% of MySQL.\n```\nwith q as (\nselect product_id\nfrom sales\ngroup by 1\nhaving min(sale_date) >= \'2019-01-01\' and max(sale_date) <= \'2019-03-31\'\n)\nselect\n    p.product_id,\n    product_name\nfrom product p join q\non p.product_id = q.product_id\n```\n\n##### If you like the solutions, please upvote \uD83D\uDD3C\n##### For any questions, or discussions, comment below. \uD83D\uDC47\uFE0F
['MySQL']