Learn-SQL icon indicating copy to clipboard operation
Learn-SQL copied to clipboard

Alternate solution to Exercise 7 (Not an Issue)

Open johnny-brav0 opened this issue 3 years ago • 2 comments

First off, thanks for creating this repo. It really has helped me a lot in getting started with SQL. Now as posted in the solutions for exercise 7, the solution, I feel, is quite static. So, I searched about the error, Error Code: 1093. You can't specify target table 'albums' for update in FROM clause I got while trying to solve it in a single query. The following solution works dynamically:

UPDATE albums
SET release_year = 1986
WHERE albums.id = (
    SELECT id
    FROM (SELECT a.id
        FROM albums AS a
        WHERE release_year IS NULL) AS c
);

Upon reading this article I found that the above used to be a complex query in earlier versions of MYSQL which involved creating a temporary table. But starting from version 5.6 onward it has been optimised. So, I assume it is safe to use. Also, coming from python background, this feels quite satisfying to do the task in one line (query in this case).

johnny-brav0 avatar Mar 07 '22 15:03 johnny-brav0

This looks good

Rufaid786 avatar Feb 21 '23 05:02 Rufaid786

UPDATE albums SET release_year = 1986 WHERE release_year IS NULL;

lepiin avatar Aug 04 '23 17:08 lepiin