{"id":17027134,"url":"https://github.com/ieshreya/sql-50-leetcode","last_synced_at":"2025-08-30T06:42:53.268Z","repository":{"id":171867765,"uuid":"648525354","full_name":"ieshreya/sql-50-leetcode","owner":"ieshreya","description":"Solutions for SQL 50 Study Plan on LeetCode","archived":false,"fork":false,"pushed_at":"2024-03-29T19:13:00.000Z","size":17,"stargazers_count":43,"open_issues_count":2,"forks_count":15,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-09T23:30:47.663Z","etag":null,"topics":["leetcode","leetcode-sql","sql"],"latest_commit_sha":null,"homepage":"https://leetcode.com/studyplan/top-sql-50/","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ieshreya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-02T07:12:21.000Z","updated_at":"2025-08-06T16:14:38.000Z","dependencies_parsed_at":"2024-03-29T19:54:14.674Z","dependency_job_id":null,"html_url":"https://github.com/ieshreya/sql-50-leetcode","commit_stats":null,"previous_names":["ieshreya/sql-50-leetcode"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ieshreya/sql-50-leetcode","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ieshreya%2Fsql-50-leetcode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ieshreya%2Fsql-50-leetcode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ieshreya%2Fsql-50-leetcode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ieshreya%2Fsql-50-leetcode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ieshreya","download_url":"https://codeload.github.com/ieshreya/sql-50-leetcode/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ieshreya%2Fsql-50-leetcode/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272815815,"owners_count":24997661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-30T02:00:09.474Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","leetcode-sql","sql"],"created_at":"2024-10-14T07:46:11.419Z","updated_at":"2025-08-30T06:42:53.232Z","avatar_url":"https://github.com/ieshreya.png","language":null,"readme":"# SQL 50 - LeetCode\nSolutions for [SQL 50 Study Plan](https://leetcode.com/studyplan/top-sql-50/) on LeetCode\n\n---\n\n[1757 - Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products/)\n```sql\nSELECT product_id\nFROM Products\nWHERE low_fats = 'Y'\nAND recyclable = 'Y'\n```\n\n[584 - Find Customer Referee](https://leetcode.com/problems/find-customer-referee)\n```sql\nSELECT name \nFROM Customer \nWHERE referee_id != 2 OR referee_id IS null\n```\n\n[595 - Big Countries](https://leetcode.com/problems/big-countries/)\n```sql\nSELECT name, population, area\nFROM WORLD\nWHERE area \u003e= 3000000\nOR population \u003e= 25000000\n```\n\n[1148 - Article Views I](https://leetcode.com/problems/article-views-i)\n```sql\nSELECT DISTINCT author_id as id\nFROM Views\nWHERE viewer_id \u003e= 1\nAND author_id = viewer_id\nORDER BY author_id\n```\n\n[1683 - Invalid Tweets](https://leetcode.com/problems/invalid-tweets/)\n```sql\nSELECT tweet_id\nFROM Tweets\nWHERE length(content) \u003e 15\n```\n\n[1378 - Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier)\n```sql\nSELECT unique_id, name\nFROM Employees e\nLEFT JOIN EmployeeUNI eu\nON e.id = eu.id\n```\n\n[1068 - Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i/)\n```sql\nSELECT product_name, year, price\nFROM Sales s\nLEFT JOIN Product p\nON s.product_id = p.product_id\n```\n\n[1581 - Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/)\n```sql\nSELECT customer_id, COUNT(*) as count_no_trans\nFROM Visits \nWHERE visit_id NOT IN (SELECT DISTINCT visit_id FROM Transactions)\nGROUP BY customer_id\n```\n\n[197 - Rising Temperature](https://leetcode.com/problems/rising-temperature/) \n```sql\nSELECT w1.id \nFROM Weather w1, Weather w2\nWHERE DATEDIFF(w1.recordDate, w2.recordDate) = 1\nAND w1.temperature \u003e w2.temperature\n\n-- OR\nSELECT w1.id\nFROM Weather w1, Weather w2\nWHERE w1.temperature \u003e w2.temperature\nAND SUBDATE(w1.recordDate, 1) = w2.recordDate\n```\n\n[1661 - Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine/)\n```sql\nSELECT machine_id, ROUND(AVG(end - start), 3) AS processing_time\nFROM \n(SELECT machine_id, process_id, \n    MAX(CASE WHEN activity_type = 'start' THEN timestamp END) AS start,\n    MAX(CASE WHEN activity_type = 'end' THEN timestamp END) AS end\n FROM Activity \n  GROUP BY machine_id, process_id) AS subq\nGROUP BY machine_id\n```\n\n[577 - Employee Bonus](https://leetcode.com/problems/employee-bonus/solutions/)\n```sql\nSELECT name, bonus\nFROM Employee e\nLEFT JOIN Bonus b\nON e.empId = b.empId\nWHERE bonus \u003c 1000\nOR bonus IS NULL\n```\n\n[1280 - Students and Examinations](https://leetcode.com/problems/students-and-examinations/)\n```sql\nSELECT a.student_id, a.student_name, b.subject_name, COUNT(c.subject_name) AS attended_exams\nFROM Students a\nJOIN Subjects b\nLEFT JOIN Examinations c\nON a.student_id = c.student_id\nAND b.subject_name = c.subject_name\nGROUP BY 1, 3\nORDER BY 1, 3 \n```\n[570. Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports)\n```sql\nSELECT name \nFROM Employee \nWHERE id IN\n  (SELECT managerId \n   FROM Employee \n   GROUP BY managerId \n   HAVING COUNT(*) \u003e= 5\n  )\n\n-- OR\nSELECT a.name\nFROM Employee a\nJOIN Employee b\nWHERE a.id = b.managerId\nGROUP BY b.managerId\nHAVING COUNT(*) \u003e= 5\n```\n\n[1934. Confirmation Rate](https://leetcode.com/problems/confirmation-rate/)\n```sql\nSELECT \n  s.user_id, \n  ROUND(\n    COALESCE(\n      SUM(\n        CASE WHEN ACTION = 'confirmed' THEN 1 END\n      ) / COUNT(*), 0),2) \n  AS confirmation_rate \nFROM Signups s \nLEFT JOIN Confirmations c \nON s.user_id = c.user_id \nGROUP BY s.user_id;  \n```\n\n[620. Not Boring Movies](https://leetcode.com/problems/not-boring-movies)\n```sql\n-- odd id, \"boring\", rating desc\nSELECT *\nFROM Cinema\nWHERE id % 2 \u003c\u003e 0 \nAND description \u003c\u003e \"boring\"\nORDER BY rating DESC\n```\n\n[1251. Average Selling Price](https://leetcode.com/problems/average-selling-price/)\n```sql\n-- avg(selling), round 2\nSELECT p.product_id, \n  ROUND(SUM(price * units) / SUM(units), 2) AS average_price\nFROM Prices p\nLEFT JOIN UnitsSold s\nON p.product_id = s.product_id\nAND purchase_date BETWEEN start_date AND end_date\nGROUP BY p.product_id\n```\n\n[1075. Project Employees I](https://leetcode.com/problems/project-employees-i)\n```sql\n-- avg(exp_yr), round 2, by project\nSELECT project_id, ROUND(AVG(experience_years), 2) average_years\nFROM Project p \nLEFT JOIN Employee e\nON p.employee_id = e.employee_id\nGROUP BY project_id\n```\n\n[1633. Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest)\n```sql\n-- % desc, contest_id asc, round 2\nSELECT r.contest_id,\n       ROUND(COUNT(DISTINCT r.user_id) * 100 / (SELECT COUNT(DISTINCT user_id) FROM Users), 2) AS percentage\nFROM Register r\nGROUP BY r.contest_id\nORDER BY percentage DESC, r.contest_id ASC;\n```\n\n[1211 Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage)\n\n```sql\n--quality - avg(rating/position), poor query % - %(rating \u003c 3), round 2\nSELECT query_name, \n    ROUND(AVG(rating/position), 2) AS quality, \n    ROUND(SUM(IF(rating \u003c 3, 1, 0)) * 100/ COUNT(rating), 2) AS poor_query_percentage\nFROM Queries\nGROUP BY query_name\n\n-- OR\nSELECT query_name, \n    ROUND(AVG(rating/position), 2) AS quality, \n    ROUND(SUM(\n        CASE WHEN rating \u003c 3 THEN 1 ELSE 0 END\n    ) * 100/ COUNT(rating), 2) AS poor_query_percentage\nFROM Queries\nGROUP BY query_name\n```\n\n[1193. Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i/)\n```sql\n-- month, country, count(trans), total(amt), count(approved_trans), total(amt)\nSELECT DATE_FORMAT(trans_date, '%Y-%m') month, country, \n        COUNT(state) trans_count, \n        SUM(IF(state = 'approved', 1, 0)) approved_count, \n        SUM(amount) trans_total_amount,\n        SUM(IF(state = 'approved', amount, 0)) approved_total_amount\nFROM Transactions\nGROUP BY 1, 2\n\n-- OR\nSELECT DATE_FORMAT(trans_date, '%Y-%m') month, country, \n        COUNT(state) trans_count, \n        SUM(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) approved_count, \n        SUM(amount) trans_total_amount,\n        SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) approved_total_amount\nFROM Transactions\nGROUP BY 1, 2\n```\n\n[1174. Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii/)\n```sql\nSELECT\n    ROUND((COUNT(CASE WHEN d.order_date = d.customer_pref_delivery_date THEN 1 END) / COUNT(*)) * 100, 2)  immediate_percentage\nFROM Delivery d\nWHERE d.order_date = (\n    SELECT\n    MIN(order_date)\n    FROM Delivery\n    WHERE customer_id = d.customer_id\n    );\n\n-- OR\nSELECT ROUND(AVG(temp.order_date=temp.customer_pref_delivery_date) * 100, 2) immediate_percentage\nFROM (\n    SELECT *, RANK() OVER(partition by customer_id ORDER BY order_date) od\n    FROM Delivery) temp\nWHERE temp.od = 1\n```\n\n[550. Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv/)\n```sql\nWITH login_date AS (SELECT player_id, MIN(event_date) AS first_login\nFROM Activity\nGROUP BY player_id),\n\nrecent_login AS (\nSELECT *, DATE_ADD(first_login, INTERVAL 1 DAY) AS next_day\nFROM login_date)\n\nSELECT ROUND((SELECT COUNT(DISTINCT(player_id))\nFROM Activity\nWHERE (player_id, event_date) IN \n(SELECT player_id, next_day FROM recent_login)) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction\n```\n[2356. Number of Unique Subjects Taught by Each Teacher](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher)\n```sql\nSELECT teacher_id, COUNT(DISTINCT subject_id) cnt\nFROM Teacher\nGROUP BY teacher_id\n```\n\n[1141. User Activity for the Past 30 Days I](https://leetcode.com/problems/user-activity-for-the-past-30-days-i/)\n```sql\nSELECT activity_date as day, COUNT(DISTINCT user_id) AS active_users\nFROM Activity\nWHERE activity_date BETWEEN DATE_SUB('2019-07-27', INTERVAL 29 DAY) AND '2019-07-27'\nGROUP BY activity_date\n```\n\n[1070. Product Sales Analysis III\n](https://leetcode.com/problems/product-sales-analysis-iii/)\n```sql\nSELECT s.product_id, s.year AS first_year, s.quantity, s.price\nFROM Sales s\nJOIN (\n  SELECT product_id, MIN(year) AS year \n  FROM sales \n  GROUP BY product_id\n  ) p\nON s.product_id = p.product_id\nAND s.year = p.year\n\n-- OR\nWITH first_year_sales AS (\n  SELECT s.product_id, MIN(s.year) as first_year\n  FROM Sales s\n  INNER JOIN Product p\n  ON s.product_id = p.product_id\n  GROUP BY s.product_id)\nSELECT f.product_id, f.first_year, s.quantity, s.price\nFROM first_year_sales f\nJOIN Sales s \nON f.product_id = s.product_id\nAND f.first_year = s.year\n```\n\n[596. Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students/)\n```sql\nSELECT class\nFROM Courses\nGROUP BY class\nHAVING COUNT(student) \u003e= 5\n```\n\n[1729. Find Followers Count](https://leetcode.com/problems/find-followers-count/)\n```sql\nSELECT user_id, COUNT(DISTINCT follower_id) AS followers_count\nFROM Followers\nGROUP BY user_id\nORDER BY user_id ASC\n```\n\n[619. Biggest Single Number](https://leetcode.com/problems/biggest-single-number/)\n```sql\nSELECT COALESCE(\n  (SELECT num\n  FROM MyNumbers\n  GROUP BY num\n  HAVING COUNT(num) = 1\n  ORDER BY num DESC\n  LIMIT 1), null) \n  AS num\n```\n\n[1045. Customers Who Bought All Products](https://leetcode.com/problems/customers-who-bought-all-products/)\n```sql\nSELECT customer_id\nFROM Customer\nGROUP BY customer_id\nHAVING COUNT(DISTINCT product_key) = (\n  SELECT COUNT(product_key)\n  FROM Product\n)\n```\n[1731. The Number of Employees Which Report to Each Employee\n](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee/)\n\n```sql\nSELECT e1.employee_id, e1.name, COUNT(e2.employee_id) reports_count, ROUND(AVG(e2.age)) average_age \nFROM Employees e1, Employees e2\nWHERE e1.employee_id = e2.reports_to\nGROUP BY e1.employee_id\nHAVING reports_count \u003e 0\nORDER BY e1.employee_id\n```\n[1789. Primary Department for Each Employee\n](https://leetcode.com/problems/primary-department-for-each-employee/?envType=study-plan-v2\u0026envId=top-sql-50)\n```sql\nSELECT employee_id, department_id\nFROM Employee \nWHERE primary_flag = 'Y'\nUNION\nSELECT employee_id, department_id\nFROM Employee\nGROUP BY employee_id\nHAVING COUNT(employee_id)=1\n\n-- OR\nSELECT employee_id,department_id\nFROM Employee\nWHERE primary_flag = 'Y' OR employee_id IN\n    (SELECT employee_id\n     FROM employee\n     GROUP BY employee_id\n     HAVING COUNT(department_id) = 1\n    )\n```\n\n[610. Triangle Judgement](https://leetcode.com/problems/triangle-judgement/)\n\n```sql\nSELECT x, y, z, \nCASE WHEN x + y \u003e z AND x + z \u003e y AND y + z \u003e x THEN 'Yes'\nELSE 'No' END AS triangle\nFROM Triangle\n```\n\n[180. Consecutive Numbers\n](https://leetcode.com/problems/consecutive-numbers/)\n```sql\nWITH cte AS (\n  SELECT id, num, \n    LEAD(num) OVER (ORDER BY id) AS next, \n    LAG(num) OVER (ORDER BY id) AS prev\n  FROM Logs\n) \nSELECT DISTINCT(num) AS ConsecutiveNums\nFROM cte\nWHERE num = next AND num = prev\n```\n\n\n[1164. Product Price at a Given Date](https://leetcode.com/problems/product-price-at-a-given-date)\n```sql\nSELECT product_id, new_price AS price\nFROM products\nWHERE (product_id, change_date) IN\n(   \n    SELECT product_id, MAX(change_date)\n    FROM products\n    WHERE change_date \u003c= '2019-08-16'\n    GROUP BY product_id\n)\nUNION\n\nSELECT product_id, 10 AS price\nFROM products\nWHEN product_id NOT IN\n(\n  SELECT product_id\n  FROM products\n  WHERE change_date \u003c= '2019-08-16'\n)\n```\n\n\n[1978. Employees Whose Manager Left the Company](https://leetcode.com/problems/employees-whose-manager-left-the-company)\n```sql\nSELECT employee_id\nFROM Employees\nWHERE manager_id NOT IN (\n    SELECT employee_id \n    FROM Employees\n)\nAND salary \u003c 30000\nORDER BY employee_id\n```\n\n[185. Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries)\n```sql\nWITH RankedSalaries AS \n(SELECT \n    e.Id AS employee_id,\n    e.name AS employee,\n    e.salary,\n    e.departmentId,\n    DENSE_RANK() OVER (PARTITION BY e.departmentId ORDER BY e.salary DESC) AS salary_rank \nFROM Employee e)\nSELECT d.name AS Department,\nr.employee,\nr.salary\nFROM Department d\nJOIN RankedSalaries r ON r.departmentId = d.id\nWHERE r.salary_rank \u003c=3;\n```\n\n\n[1667. Fix Names in a Table](https://leetcode.com/problems/fix-names-in-a-table)\n```sql\nSELECT user_id, CONCAT(UPPER(LEFT(name, 1)), LOWER(RIGHT(name, LENGTH(name)-1))) AS name\nFROM Users\nORDER BY user_id\n```\n\n[1527. Patients With a Condition](https://leetcode.com/problems/patients-with-a-condition)\n```sql\nSELECT patient_id, patient_name, conditions \nFROM patients \nWHERE conditions LIKE '% DIAB1%' \nOR conditions LIKE 'DIAB1%'\n```\n\n[196. Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails)\n```sql\nDELETE p\nFROM Person p, Person q\nWHERE p.id \u003e q.id\nAND q.Email = p.Email\n```\n\n[176. Second Highest Salary](https://leetcode.com/problems/second-highest-salary)\n```sql\nSELECT\n(SELECT DISTINCT Salary \nFROM Employee\nORDER BY Salary DESC\nLIMIT 1 OFFSET 1)\nAS SecondHighestSalary\n\n-- HINT: subquery is used to return null if there is no SecondHighestSalary\n```\n\n\n \n[1517. Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails)\n```sql\nSELECT *\nFROM Users\nWHERE mail REGEXP '^[A-Za-z][A-Za-z0-9_\\.\\-]*@leetcode\\\\.com$'\n```\n\n[1204. Last Person to Fit in the Bus](https://leetcode.com/problems/last-person-to-fit-in-the-bus/)\n```sql\n-- 1000 kg limit\n-- name of last person\n\nWITH CTE AS (\n    SELECT person_name, weight, turn, SUM(weight) \n    OVER(ORDER BY turn) AS total_weight\n    FROM Queue\n)\nSELECT person_name\nFROM cte\nWHERE total_weight \u003c=1000\nORDER BY total_weight DESC\nLIMIT 1;\n```\n\n[1907. Count Salary Categories](https://leetcode.com/problems/count-salary-categories/)\n```sql\n\nSELECT 'Low Salary' AS category, SUM(IF(income\u003c20000,1,0)) AS accounts_count \nFROM Accounts\nUNION\nSELECT 'Average Salary' AS category, SUM(IF(income\u003e=20000 AND income\u003c=50000,1,0)) AS accounts_count \nFROM Accounts\nUNION\nSELECT 'High Salary' AS category, SUM(IF(income\u003e50000,1,0)) AS accounts_count \nFROM Accounts\n```\n[626. Exchange Seats](https://leetcode.com/problems/exchange-seats/)\n```sql\n-- id, student\n-- swap every two consecutives\n-- num(students): odd? no swap for last one\n\nSELECT id, \nCASE WHEN MOD(id,2)=0 THEN (LAG(student) OVER (ORDER BY id))\nELSE (LEAD(student, 1, student) OVER (ORDER BY id))\nEND AS 'Student'\nFROM Seat\n```\n\n[1327. List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period/)\n```sql\n-- name, amt\n-- \u003e= 100 units, feb 2020\n\nSELECT p.product_name, SUM(o.unit) AS unit\nFROM Products p\nLEFT JOIN Orders o\nON p.product_id = o.product_id\nWHERE DATE_FORMAT(order_date, '%Y-%m') = '2020-02'\nGROUP BY p.product_name\nHAVING SUM(o.unit) \u003e= 100\n```\n\n[1484. Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date/)\n```sql\nSELECT sell_date, \nCOUNT(DISTINCT product) AS num_sold,\nGROUP_CONCAT(DISTINCT product) AS 'products' \nFROM Activities\nGROUP BY sell_date\nORDER BY sell_date\n```\n\n[1341. Movie Rating](https://leetcode.com/problems/movie-rating/)\n\n```sql\n(SELECT name AS results\nFROM Users u\nLEFT JOIN MovieRating mr\nON u.user_id = mr.user_id\nGROUP BY name\nORDER BY COUNT(rating) DESC, name ASC\nLIMIT 1)\n\nUNION ALL\n\n(SELECT title\nFROM Movies m\nLEFT JOIN MovieRating mr\nON m.movie_id = mr.movie_id\nWHERE DATE_FORMAT(created_at, '%Y-%m') = '2020-02'\nGROUP BY title\nORDER BY AVG(rating) DESC, title ASC\nLIMIT 1 \n)\n```\n\n[1321. Restaurant Growth](https://leetcode.com/problems/restaurant-growth/)\n```sql\n-- pay: last 7 days (today inclusive) - avg.amt (round, 2)\n\nSELECT visited_on, amount, ROUND(amount/7, 2) AS average_amount\nFROM (\n    SELECT DISTINCT visited_on,\n    SUM(amount) OVER(ORDER BY visited_on RANGE BETWEEN INTERVAL 6 DAY PRECEDING AND CURRENT ROW) AS amount,\n    MIN(visited_on) OVER() day_1\n    FROM Customer\n) t\nWHERE visited_on \u003e= day_1+6;\n```\n\n\n[602. Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends)\n\n```sql\n-- `union` selects only unique vals, so we use `union all` here\n\nWITH CTE AS (\n    SELECT requester_id AS id FROM RequestAccepted\n    UNION ALL\n    SELECT accepter_id AS id FROM RequestAccepted\n)\n\nSELECT id, COUNT(id) AS num\nFROM CTE\nGROUP BY id\nORDER BY num DESC\nLIMIT 1\n```\n\n[585. Investments in 2016](https://leetcode.com/problems/investments-in-2016)\n```sql\nSELECT\n    ROUND(SUM(tiv_2016),2) AS tiv_2016\nFROM insurance\nWHERE tiv_2015 IN (SELECT tiv_2015 FROM insurance GROUP BY tiv_2015 HAVING COUNT(*) \u003e 1)\nAND (lat,lon) IN (SELECT lat,lon FROM insurance GROUP BY lat,lon HAVING COUNT(*) = 1)\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fieshreya%2Fsql-50-leetcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fieshreya%2Fsql-50-leetcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fieshreya%2Fsql-50-leetcode/lists"}