└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # SQL 50 - LeetCode 2 | Solutions for [SQL 50 Study Plan](https://leetcode.com/studyplan/top-sql-50/) on LeetCode 3 | 4 | --- 5 | 6 | [1757 - Recyclable and Low Fat Products](https://leetcode.com/problems/recyclable-and-low-fat-products/) 7 | ```sql 8 | SELECT product_id 9 | FROM Products 10 | WHERE low_fats = 'Y' 11 | AND recyclable = 'Y' 12 | ``` 13 | 14 | [584 - Find Customer Referee](https://leetcode.com/problems/find-customer-referee) 15 | ```sql 16 | SELECT name 17 | FROM Customer 18 | WHERE referee_id != 2 OR referee_id IS null 19 | ``` 20 | 21 | [595 - Big Countries](https://leetcode.com/problems/big-countries/) 22 | ```sql 23 | SELECT name, population, area 24 | FROM WORLD 25 | WHERE area >= 3000000 26 | OR population >= 25000000 27 | ``` 28 | 29 | [1148 - Article Views I](https://leetcode.com/problems/article-views-i) 30 | ```sql 31 | SELECT DISTINCT author_id as id 32 | FROM Views 33 | WHERE viewer_id >= 1 34 | AND author_id = viewer_id 35 | ORDER BY author_id 36 | ``` 37 | 38 | [1683 - Invalid Tweets](https://leetcode.com/problems/invalid-tweets/) 39 | ```sql 40 | SELECT tweet_id 41 | FROM Tweets 42 | WHERE length(content) > 15 43 | ``` 44 | 45 | [1378 - Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier) 46 | ```sql 47 | SELECT unique_id, name 48 | FROM Employees e 49 | LEFT JOIN EmployeeUNI eu 50 | ON e.id = eu.id 51 | ``` 52 | 53 | [1068 - Product Sales Analysis I](https://leetcode.com/problems/product-sales-analysis-i/) 54 | ```sql 55 | SELECT product_name, year, price 56 | FROM Sales s 57 | LEFT JOIN Product p 58 | ON s.product_id = p.product_id 59 | ``` 60 | 61 | [1581 - Customer Who Visited but Did Not Make Any Transactions](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/) 62 | ```sql 63 | SELECT customer_id, COUNT(*) as count_no_trans 64 | FROM Visits 65 | WHERE visit_id NOT IN (SELECT DISTINCT visit_id FROM Transactions) 66 | GROUP BY customer_id 67 | ``` 68 | 69 | [197 - Rising Temperature](https://leetcode.com/problems/rising-temperature/) 70 | ```sql 71 | SELECT w1.id 72 | FROM Weather w1, Weather w2 73 | WHERE DATEDIFF(w1.recordDate, w2.recordDate) = 1 74 | AND w1.temperature > w2.temperature 75 | 76 | -- OR 77 | SELECT w1.id 78 | FROM Weather w1, Weather w2 79 | WHERE w1.temperature > w2.temperature 80 | AND SUBDATE(w1.recordDate, 1) = w2.recordDate 81 | ``` 82 | 83 | [1661 - Average Time of Process per Machine](https://leetcode.com/problems/average-time-of-process-per-machine/) 84 | ```sql 85 | SELECT machine_id, ROUND(AVG(end - start), 3) AS processing_time 86 | FROM 87 | (SELECT machine_id, process_id, 88 | MAX(CASE WHEN activity_type = 'start' THEN timestamp END) AS start, 89 | MAX(CASE WHEN activity_type = 'end' THEN timestamp END) AS end 90 | FROM Activity 91 | GROUP BY machine_id, process_id) AS subq 92 | GROUP BY machine_id 93 | ``` 94 | 95 | [577 - Employee Bonus](https://leetcode.com/problems/employee-bonus/solutions/) 96 | ```sql 97 | SELECT name, bonus 98 | FROM Employee e 99 | LEFT JOIN Bonus b 100 | ON e.empId = b.empId 101 | WHERE bonus < 1000 102 | OR bonus IS NULL 103 | ``` 104 | 105 | [1280 - Students and Examinations](https://leetcode.com/problems/students-and-examinations/) 106 | ```sql 107 | SELECT a.student_id, a.student_name, b.subject_name, COUNT(c.subject_name) AS attended_exams 108 | FROM Students a 109 | JOIN Subjects b 110 | LEFT JOIN Examinations c 111 | ON a.student_id = c.student_id 112 | AND b.subject_name = c.subject_name 113 | GROUP BY 1, 3 114 | ORDER BY 1, 3 115 | ``` 116 | [570. Managers with at Least 5 Direct Reports](https://leetcode.com/problems/managers-with-at-least-5-direct-reports) 117 | ```sql 118 | SELECT name 119 | FROM Employee 120 | WHERE id IN 121 | (SELECT managerId 122 | FROM Employee 123 | GROUP BY managerId 124 | HAVING COUNT(*) >= 5 125 | ) 126 | 127 | -- OR 128 | SELECT a.name 129 | FROM Employee a 130 | JOIN Employee b 131 | WHERE a.id = b.managerId 132 | GROUP BY b.managerId 133 | HAVING COUNT(*) >= 5 134 | ``` 135 | 136 | [1934. Confirmation Rate](https://leetcode.com/problems/confirmation-rate/) 137 | ```sql 138 | SELECT 139 | s.user_id, 140 | ROUND( 141 | COALESCE( 142 | SUM( 143 | CASE WHEN ACTION = 'confirmed' THEN 1 END 144 | ) / COUNT(*), 0),2) 145 | AS confirmation_rate 146 | FROM Signups s 147 | LEFT JOIN Confirmations c 148 | ON s.user_id = c.user_id 149 | GROUP BY s.user_id; 150 | ``` 151 | 152 | [620. Not Boring Movies](https://leetcode.com/problems/not-boring-movies) 153 | ```sql 154 | -- odd id, "boring", rating desc 155 | SELECT * 156 | FROM Cinema 157 | WHERE id % 2 <> 0 158 | AND description <> "boring" 159 | ORDER BY rating DESC 160 | ``` 161 | 162 | [1251. Average Selling Price](https://leetcode.com/problems/average-selling-price/) 163 | ```sql 164 | -- avg(selling), round 2 165 | SELECT p.product_id, 166 | ROUND(SUM(price * units) / SUM(units), 2) AS average_price 167 | FROM Prices p 168 | LEFT JOIN UnitsSold s 169 | ON p.product_id = s.product_id 170 | AND purchase_date BETWEEN start_date AND end_date 171 | GROUP BY p.product_id 172 | ``` 173 | 174 | [1075. Project Employees I](https://leetcode.com/problems/project-employees-i) 175 | ```sql 176 | -- avg(exp_yr), round 2, by project 177 | SELECT project_id, ROUND(AVG(experience_years), 2) average_years 178 | FROM Project p 179 | LEFT JOIN Employee e 180 | ON p.employee_id = e.employee_id 181 | GROUP BY project_id 182 | ``` 183 | 184 | [1633. Percentage of Users Attended a Contest](https://leetcode.com/problems/percentage-of-users-attended-a-contest) 185 | ```sql 186 | -- % desc, contest_id asc, round 2 187 | SELECT r.contest_id, 188 | ROUND(COUNT(DISTINCT r.user_id) * 100 / (SELECT COUNT(DISTINCT user_id) FROM Users), 2) AS percentage 189 | FROM Register r 190 | GROUP BY r.contest_id 191 | ORDER BY percentage DESC, r.contest_id ASC; 192 | ``` 193 | 194 | [1211 Queries Quality and Percentage](https://leetcode.com/problems/queries-quality-and-percentage) 195 | 196 | ```sql 197 | --quality - avg(rating/position), poor query % - %(rating < 3), round 2 198 | SELECT query_name, 199 | ROUND(AVG(rating/position), 2) AS quality, 200 | ROUND(SUM(IF(rating < 3, 1, 0)) * 100/ COUNT(rating), 2) AS poor_query_percentage 201 | FROM Queries 202 | GROUP BY query_name 203 | 204 | -- OR 205 | SELECT query_name, 206 | ROUND(AVG(rating/position), 2) AS quality, 207 | ROUND(SUM( 208 | CASE WHEN rating < 3 THEN 1 ELSE 0 END 209 | ) * 100/ COUNT(rating), 2) AS poor_query_percentage 210 | FROM Queries 211 | GROUP BY query_name 212 | ``` 213 | 214 | [1193. Monthly Transactions I](https://leetcode.com/problems/monthly-transactions-i/) 215 | ```sql 216 | -- month, country, count(trans), total(amt), count(approved_trans), total(amt) 217 | SELECT DATE_FORMAT(trans_date, '%Y-%m') month, country, 218 | COUNT(state) trans_count, 219 | SUM(IF(state = 'approved', 1, 0)) approved_count, 220 | SUM(amount) trans_total_amount, 221 | SUM(IF(state = 'approved', amount, 0)) approved_total_amount 222 | FROM Transactions 223 | GROUP BY 1, 2 224 | 225 | -- OR 226 | SELECT DATE_FORMAT(trans_date, '%Y-%m') month, country, 227 | COUNT(state) trans_count, 228 | SUM(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) approved_count, 229 | SUM(amount) trans_total_amount, 230 | SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) approved_total_amount 231 | FROM Transactions 232 | GROUP BY 1, 2 233 | ``` 234 | 235 | [1174. Immediate Food Delivery II](https://leetcode.com/problems/immediate-food-delivery-ii/) 236 | ```sql 237 | SELECT 238 | ROUND((COUNT(CASE WHEN d.order_date = d.customer_pref_delivery_date THEN 1 END) / COUNT(*)) * 100, 2) immediate_percentage 239 | FROM Delivery d 240 | WHERE d.order_date = ( 241 | SELECT 242 | MIN(order_date) 243 | FROM Delivery 244 | WHERE customer_id = d.customer_id 245 | ); 246 | 247 | -- OR 248 | SELECT ROUND(AVG(temp.order_date=temp.customer_pref_delivery_date) * 100, 2) immediate_percentage 249 | FROM ( 250 | SELECT *, RANK() OVER(partition by customer_id ORDER BY order_date) od 251 | FROM Delivery) temp 252 | WHERE temp.od = 1 253 | ``` 254 | 255 | [550. Game Play Analysis IV](https://leetcode.com/problems/game-play-analysis-iv/) 256 | ```sql 257 | WITH login_date AS (SELECT player_id, MIN(event_date) AS first_login 258 | FROM Activity 259 | GROUP BY player_id), 260 | 261 | recent_login AS ( 262 | SELECT *, DATE_ADD(first_login, INTERVAL 1 DAY) AS next_day 263 | FROM login_date) 264 | 265 | SELECT ROUND((SELECT COUNT(DISTINCT(player_id)) 266 | FROM Activity 267 | WHERE (player_id, event_date) IN 268 | (SELECT player_id, next_day FROM recent_login)) / (SELECT COUNT(DISTINCT player_id) FROM Activity), 2) AS fraction 269 | ``` 270 | [2356. Number of Unique Subjects Taught by Each Teacher](https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher) 271 | ```sql 272 | SELECT teacher_id, COUNT(DISTINCT subject_id) cnt 273 | FROM Teacher 274 | GROUP BY teacher_id 275 | ``` 276 | 277 | [1141. User Activity for the Past 30 Days I](https://leetcode.com/problems/user-activity-for-the-past-30-days-i/) 278 | ```sql 279 | SELECT activity_date as day, COUNT(DISTINCT user_id) AS active_users 280 | FROM Activity 281 | WHERE activity_date BETWEEN DATE_SUB('2019-07-27', INTERVAL 29 DAY) AND '2019-07-27' 282 | GROUP BY activity_date 283 | ``` 284 | 285 | [1070. Product Sales Analysis III 286 | ](https://leetcode.com/problems/product-sales-analysis-iii/) 287 | ```sql 288 | SELECT s.product_id, s.year AS first_year, s.quantity, s.price 289 | FROM Sales s 290 | JOIN ( 291 | SELECT product_id, MIN(year) AS year 292 | FROM sales 293 | GROUP BY product_id 294 | ) p 295 | ON s.product_id = p.product_id 296 | AND s.year = p.year 297 | 298 | -- OR 299 | WITH first_year_sales AS ( 300 | SELECT s.product_id, MIN(s.year) as first_year 301 | FROM Sales s 302 | INNER JOIN Product p 303 | ON s.product_id = p.product_id 304 | GROUP BY s.product_id) 305 | SELECT f.product_id, f.first_year, s.quantity, s.price 306 | FROM first_year_sales f 307 | JOIN Sales s 308 | ON f.product_id = s.product_id 309 | AND f.first_year = s.year 310 | ``` 311 | 312 | [596. Classes More Than 5 Students](https://leetcode.com/problems/classes-more-than-5-students/) 313 | ```sql 314 | SELECT class 315 | FROM Courses 316 | GROUP BY class 317 | HAVING COUNT(student) >= 5 318 | ``` 319 | 320 | [1729. Find Followers Count](https://leetcode.com/problems/find-followers-count/) 321 | ```sql 322 | SELECT user_id, COUNT(DISTINCT follower_id) AS followers_count 323 | FROM Followers 324 | GROUP BY user_id 325 | ORDER BY user_id ASC 326 | ``` 327 | 328 | [619. Biggest Single Number](https://leetcode.com/problems/biggest-single-number/) 329 | ```sql 330 | SELECT COALESCE( 331 | (SELECT num 332 | FROM MyNumbers 333 | GROUP BY num 334 | HAVING COUNT(num) = 1 335 | ORDER BY num DESC 336 | LIMIT 1), null) 337 | AS num 338 | ``` 339 | 340 | [1045. Customers Who Bought All Products](https://leetcode.com/problems/customers-who-bought-all-products/) 341 | ```sql 342 | SELECT customer_id 343 | FROM Customer 344 | GROUP BY customer_id 345 | HAVING COUNT(DISTINCT product_key) = ( 346 | SELECT COUNT(product_key) 347 | FROM Product 348 | ) 349 | ``` 350 | [1731. The Number of Employees Which Report to Each Employee 351 | ](https://leetcode.com/problems/the-number-of-employees-which-report-to-each-employee/) 352 | 353 | ```sql 354 | SELECT e1.employee_id, e1.name, COUNT(e2.employee_id) reports_count, ROUND(AVG(e2.age)) average_age 355 | FROM Employees e1, Employees e2 356 | WHERE e1.employee_id = e2.reports_to 357 | GROUP BY e1.employee_id 358 | HAVING reports_count > 0 359 | ORDER BY e1.employee_id 360 | ``` 361 | [1789. Primary Department for Each Employee 362 | ](https://leetcode.com/problems/primary-department-for-each-employee/?envType=study-plan-v2&envId=top-sql-50) 363 | ```sql 364 | SELECT employee_id, department_id 365 | FROM Employee 366 | WHERE primary_flag = 'Y' 367 | UNION 368 | SELECT employee_id, department_id 369 | FROM Employee 370 | GROUP BY employee_id 371 | HAVING COUNT(employee_id)=1 372 | 373 | -- OR 374 | SELECT employee_id,department_id 375 | FROM Employee 376 | WHERE primary_flag = 'Y' OR employee_id IN 377 | (SELECT employee_id 378 | FROM employee 379 | GROUP BY employee_id 380 | HAVING COUNT(department_id) = 1 381 | ) 382 | ``` 383 | 384 | [610. Triangle Judgement](https://leetcode.com/problems/triangle-judgement/) 385 | 386 | ```sql 387 | SELECT x, y, z, 388 | CASE WHEN x + y > z AND x + z > y AND y + z > x THEN 'Yes' 389 | ELSE 'No' END AS triangle 390 | FROM Triangle 391 | ``` 392 | 393 | [180. Consecutive Numbers 394 | ](https://leetcode.com/problems/consecutive-numbers/) 395 | ```sql 396 | WITH cte AS ( 397 | SELECT id, num, 398 | LEAD(num) OVER (ORDER BY id) AS next, 399 | LAG(num) OVER (ORDER BY id) AS prev 400 | FROM Logs 401 | ) 402 | SELECT DISTINCT(num) AS ConsecutiveNums 403 | FROM cte 404 | WHERE num = next AND num = prev 405 | ``` 406 | 407 | 408 | [1164. Product Price at a Given Date](https://leetcode.com/problems/product-price-at-a-given-date) 409 | ```sql 410 | SELECT product_id, new_price AS price 411 | FROM products 412 | WHERE (product_id, change_date) IN 413 | ( 414 | SELECT product_id, MAX(change_date) 415 | FROM products 416 | WHERE change_date <= '2019-08-16' 417 | GROUP BY product_id 418 | ) 419 | UNION 420 | 421 | SELECT product_id, 10 AS price 422 | FROM products 423 | WHEN product_id NOT IN 424 | ( 425 | SELECT product_id 426 | FROM products 427 | WHERE change_date <= '2019-08-16' 428 | ) 429 | ``` 430 | 431 | 432 | [1978. Employees Whose Manager Left the Company](https://leetcode.com/problems/employees-whose-manager-left-the-company) 433 | ```sql 434 | SELECT employee_id 435 | FROM Employees 436 | WHERE manager_id NOT IN ( 437 | SELECT employee_id 438 | FROM Employees 439 | ) 440 | AND salary < 30000 441 | ORDER BY employee_id 442 | ``` 443 | 444 | [185. Department Top Three Salaries](https://leetcode.com/problems/department-top-three-salaries) 445 | ```sql 446 | WITH RankedSalaries AS 447 | (SELECT 448 | e.Id AS employee_id, 449 | e.name AS employee, 450 | e.salary, 451 | e.departmentId, 452 | DENSE_RANK() OVER (PARTITION BY e.departmentId ORDER BY e.salary DESC) AS salary_rank 453 | FROM Employee e) 454 | SELECT d.name AS Department, 455 | r.employee, 456 | r.salary 457 | FROM Department d 458 | JOIN RankedSalaries r ON r.departmentId = d.id 459 | WHERE r.salary_rank <=3; 460 | ``` 461 | 462 | 463 | [1667. Fix Names in a Table](https://leetcode.com/problems/fix-names-in-a-table) 464 | ```sql 465 | SELECT user_id, CONCAT(UPPER(LEFT(name, 1)), LOWER(RIGHT(name, LENGTH(name)-1))) AS name 466 | FROM Users 467 | ORDER BY user_id 468 | ``` 469 | 470 | [1527. Patients With a Condition](https://leetcode.com/problems/patients-with-a-condition) 471 | ```sql 472 | SELECT patient_id, patient_name, conditions 473 | FROM patients 474 | WHERE conditions LIKE '% DIAB1%' 475 | OR conditions LIKE 'DIAB1%' 476 | ``` 477 | 478 | [196. Delete Duplicate Emails](https://leetcode.com/problems/delete-duplicate-emails) 479 | ```sql 480 | DELETE p 481 | FROM Person p, Person q 482 | WHERE p.id > q.id 483 | AND q.Email = p.Email 484 | ``` 485 | 486 | [176. Second Highest Salary](https://leetcode.com/problems/second-highest-salary) 487 | ```sql 488 | SELECT 489 | (SELECT DISTINCT Salary 490 | FROM Employee 491 | ORDER BY Salary DESC 492 | LIMIT 1 OFFSET 1) 493 | AS SecondHighestSalary 494 | 495 | -- HINT: subquery is used to return null if there is no SecondHighestSalary 496 | ``` 497 | 498 | 499 | 500 | [1517. Find Users With Valid E-Mails](https://leetcode.com/problems/find-users-with-valid-e-mails) 501 | ```sql 502 | SELECT * 503 | FROM Users 504 | WHERE mail REGEXP '^[A-Za-z][A-Za-z0-9_\.\-]*@leetcode\\.com$' 505 | ``` 506 | 507 | [1204. Last Person to Fit in the Bus](https://leetcode.com/problems/last-person-to-fit-in-the-bus/) 508 | ```sql 509 | -- 1000 kg limit 510 | -- name of last person 511 | 512 | WITH CTE AS ( 513 | SELECT person_name, weight, turn, SUM(weight) 514 | OVER(ORDER BY turn) AS total_weight 515 | FROM Queue 516 | ) 517 | SELECT person_name 518 | FROM cte 519 | WHERE total_weight <=1000 520 | ORDER BY total_weight DESC 521 | LIMIT 1; 522 | ``` 523 | 524 | [1907. Count Salary Categories](https://leetcode.com/problems/count-salary-categories/) 525 | ```sql 526 | 527 | SELECT 'Low Salary' AS category, SUM(IF(income<20000,1,0)) AS accounts_count 528 | FROM Accounts 529 | UNION 530 | SELECT 'Average Salary' AS category, SUM(IF(income>=20000 AND income<=50000,1,0)) AS accounts_count 531 | FROM Accounts 532 | UNION 533 | SELECT 'High Salary' AS category, SUM(IF(income>50000,1,0)) AS accounts_count 534 | FROM Accounts 535 | ``` 536 | [626. Exchange Seats](https://leetcode.com/problems/exchange-seats/) 537 | ```sql 538 | -- id, student 539 | -- swap every two consecutives 540 | -- num(students): odd? no swap for last one 541 | 542 | SELECT id, 543 | CASE WHEN MOD(id,2)=0 THEN (LAG(student) OVER (ORDER BY id)) 544 | ELSE (LEAD(student, 1, student) OVER (ORDER BY id)) 545 | END AS 'Student' 546 | FROM Seat 547 | ``` 548 | 549 | [1327. List the Products Ordered in a Period](https://leetcode.com/problems/list-the-products-ordered-in-a-period/) 550 | ```sql 551 | -- name, amt 552 | -- >= 100 units, feb 2020 553 | 554 | SELECT p.product_name, SUM(o.unit) AS unit 555 | FROM Products p 556 | LEFT JOIN Orders o 557 | ON p.product_id = o.product_id 558 | WHERE DATE_FORMAT(order_date, '%Y-%m') = '2020-02' 559 | GROUP BY p.product_name 560 | HAVING SUM(o.unit) >= 100 561 | ``` 562 | 563 | [1484. Group Sold Products By The Date](https://leetcode.com/problems/group-sold-products-by-the-date/) 564 | ```sql 565 | SELECT sell_date, 566 | COUNT(DISTINCT product) AS num_sold, 567 | GROUP_CONCAT(DISTINCT product) AS 'products' 568 | FROM Activities 569 | GROUP BY sell_date 570 | ORDER BY sell_date 571 | ``` 572 | 573 | [1341. Movie Rating](https://leetcode.com/problems/movie-rating/) 574 | 575 | ```sql 576 | (SELECT name AS results 577 | FROM Users u 578 | LEFT JOIN MovieRating mr 579 | ON u.user_id = mr.user_id 580 | GROUP BY name 581 | ORDER BY COUNT(rating) DESC, name ASC 582 | LIMIT 1) 583 | 584 | UNION ALL 585 | 586 | (SELECT title 587 | FROM Movies m 588 | LEFT JOIN MovieRating mr 589 | ON m.movie_id = mr.movie_id 590 | WHERE DATE_FORMAT(created_at, '%Y-%m') = '2020-02' 591 | GROUP BY title 592 | ORDER BY AVG(rating) DESC, title ASC 593 | LIMIT 1 594 | ) 595 | ``` 596 | 597 | [1321. Restaurant Growth](https://leetcode.com/problems/restaurant-growth/) 598 | ```sql 599 | -- pay: last 7 days (today inclusive) - avg.amt (round, 2) 600 | 601 | SELECT visited_on, amount, ROUND(amount/7, 2) AS average_amount 602 | FROM ( 603 | SELECT DISTINCT visited_on, 604 | SUM(amount) OVER(ORDER BY visited_on RANGE BETWEEN INTERVAL 6 DAY PRECEDING AND CURRENT ROW) AS amount, 605 | MIN(visited_on) OVER() day_1 606 | FROM Customer 607 | ) t 608 | WHERE visited_on >= day_1+6; 609 | ``` 610 | 611 | 612 | [602. Friend Requests II: Who Has the Most Friends](https://leetcode.com/problems/friend-requests-ii-who-has-the-most-friends) 613 | 614 | ```sql 615 | -- `union` selects only unique vals, so we use `union all` here 616 | 617 | WITH CTE AS ( 618 | SELECT requester_id AS id FROM RequestAccepted 619 | UNION ALL 620 | SELECT accepter_id AS id FROM RequestAccepted 621 | ) 622 | 623 | SELECT id, COUNT(id) AS num 624 | FROM CTE 625 | GROUP BY id 626 | ORDER BY num DESC 627 | LIMIT 1 628 | ``` 629 | 630 | [585. Investments in 2016](https://leetcode.com/problems/investments-in-2016) 631 | ```sql 632 | SELECT 633 | ROUND(SUM(tiv_2016),2) AS tiv_2016 634 | FROM insurance 635 | WHERE tiv_2015 IN (SELECT tiv_2015 FROM insurance GROUP BY tiv_2015 HAVING COUNT(*) > 1) 636 | AND (lat,lon) IN (SELECT lat,lon FROM insurance GROUP BY lat,lon HAVING COUNT(*) = 1) 637 | ``` 638 | 639 | --------------------------------------------------------------------------------