diff --git a/README.md b/README.md index 608c120..e07ef7f 100644 --- a/README.md +++ b/README.md @@ -60,28 +60,43 @@ ON s.product_id = p.product_id [1581 - Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/) ```sql -SELECT customer_id, COUNT(*) as count_no_trans -FROM Visits -WHERE visit_id NOT IN (SELECT DISTINCT visit_id FROM Transactions) -GROUP BY customer_id +select v.customer_id, count(v.visit_id) as count_no_trans +from Visits v +left join Transactions t +On v.visit_id = t.visit_id +Where t.transaction_id is Null +group by v.customer_id; + +--2nd approach: using Subquery (This approach is not ideal as this problem can be solved using joins) +SELECT customer_id, COUNT(visit_id) as count_no_trans +FROM Visits +WHERE visit_id NOT IN ( + SELECT visit_id FROM Transactions + ) +GROUP BY customer_id; ``` [197 - Rising Temperature](https://leetcode.com/problems/rising-temperature/) ```sql -SELECT w1.id -FROM Weather w1, Weather w2 -WHERE DATEDIFF(w1.recordDate, w2.recordDate) = 1 -AND w1.temperature > w2.temperature +SELECT + w1.id +FROM + Weather w1 +JOIN + Weather w2 +ON + DATEDIFF(w1.recordDate, w2.recordDate) = 1 +WHERE + w1.temperature > w2.temperature; --- OR -SELECT w1.id -FROM Weather w1, Weather w2 -WHERE w1.temperature > w2.temperature -AND SUBDATE(w1.recordDate, 1) = w2.recordDate +-- OR We can use lag() window function to get previous_temperature and record_date columns beside our table and filter it ``` + [1661 - Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine/) ```sql + +-- 1ST approach is by writing subquery to make temporary table first and then we apply math function SELECT machine_id, ROUND(AVG(end - start), 3) AS processing_time FROM (SELECT machine_id, process_id, @@ -90,28 +105,41 @@ FROM FROM Activity GROUP BY machine_id, process_id) AS subq GROUP BY machine_id + +-- OR this is another approach but this put math expession in one go +SELECT + machine_id, + round(SUM(CASE WHEN activity_type='start' THEN timestamp*-1 ELSE timestamp END) + / (SELECT COUNT(DISTINCT process_id)),3) AS processing_time +FROM + Activity +GROUP BY machine_id; + ``` [577 - Employee Bonus](https://leetcode.com/problems/employee-bonus/solutions/) ```sql +-- Note that e.name b.bonus can also be written in select statement SELECT name, bonus FROM Employee e LEFT JOIN Bonus b ON e.empId = b.empId -WHERE bonus < 1000 -OR bonus IS NULL +WHERE bonus < 1000 OR bonus IS NULL ``` [1280 - Students and Examinations](https://leetcode.com/problems/students-and-examinations/) ```sql -SELECT a.student_id, a.student_name, b.subject_name, COUNT(c.subject_name) AS attended_exams -FROM Students a -JOIN Subjects b -LEFT JOIN Examinations c -ON a.student_id = c.student_id -AND b.subject_name = c.subject_name -GROUP BY 1, 3 -ORDER BY 1, 3 +-- Beautiful and brain storming one cross join is must + +select st.student_id, st.student_name, s.subject_name, +count(e.student_id) as attended_exams + +from Students st +cross join Subjects s +left join Examinations e +On st.student_id = e.student_id and s.subject_name = e.subject_name +group by 1,2,3 +order by 1,2,3 ``` [570. Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports) ```sql @@ -124,13 +152,6 @@ WHERE id IN HAVING COUNT(*) >= 5 ) --- OR -SELECT a.name -FROM Employee a -JOIN Employee b -WHERE a.id = b.managerId -GROUP BY b.managerId -HAVING COUNT(*) >= 5 ``` [1934. Confirmation Rate](https://leetcode.com/problems/confirmation-rate/)