From 0722d86dc3720a0e89fe1b7243ac0cfb1a919563 Mon Sep 17 00:00:00 2001 From: dvohra09 Date: Mon, 30 Jun 2025 17:03:30 -0700 Subject: [PATCH 1/6] Create fix-when-using-UPDATE-statement.sql --- .../fix-when-using-UPDATE-statement.sql | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql diff --git a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql new file mode 100644 index 00000000..a5116c7f --- /dev/null +++ b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql @@ -0,0 +1,37 @@ +-- This statement generates the Error 1093 +UPDATE Program +SET end_date = (SELECT start_date FROM Program); + +-- This statement fixes the Error 1093 in the preceding example using a derived table +UPDATE Program +SET end_date = ( + SELECT subquery_program.start_date + FROM ( + SELECT start_date + FROM Program + WHERE description ='Major in Operating Systems' + ) AS subquery_program +); + +-- This statement generates the Error 1093 +UPDATE Program +SET name = (SELECT description FROM Program); + +-- This set of statements fixes the Error 1093 in the preceding example using a temporary table +-- Create a temporary table +CREATE TEMPORARY TABLE IF NOT EXISTS temp_table AS +SELECT id, description +FROM Program; + +-- Update the main table using the temporary table +UPDATE Program t +JOIN temp_table temp ON t.id = temp.id +SET t.name = temp.description; + +-- Drop the temporary table +DROP TEMPORARY TABLE IF EXISTS temp_table; + +-- This statement fixes the Error 1093 using a self-join +UPDATE Program p1 +JOIN Program p2 ON p1.id = p2.id +SET p1.name = p2.description; From d641326e53a1917f8d98e2773b1a5f153b1c3187 Mon Sep 17 00:00:00 2001 From: dvohra09 Date: Mon, 30 Jun 2025 17:04:22 -0700 Subject: [PATCH 2/6] Create fix-when-using-DELETE-statement.sql --- .../fix-when-using-DELETE-statement.sql | 1 + 1 file changed, 1 insertion(+) create mode 100644 sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql diff --git a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql @@ -0,0 +1 @@ + From ef703f0cbc903608a551507a6666adfb96ace2d5 Mon Sep 17 00:00:00 2001 From: dvohra09 Date: Mon, 30 Jun 2025 17:08:00 -0700 Subject: [PATCH 3/6] Update fix-when-using-DELETE-statement.sql --- .../fix-when-using-DELETE-statement.sql | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql index 8b137891..424edf42 100644 --- a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql +++ b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql @@ -1 +1,41 @@ +-- This statement generates the Error 1093 +DELETE FROM Program +WHERE id IN +(SELECT id FROM Program WHERE department_id=1); + +-- This statement fixes the Error 1093 in the preceding example using a derived table +DELETE FROM Program +WHERE id IN ( + SELECT subquery_program.id + FROM ( + SELECT id + FROM Program + WHERE department_id=1 + ) AS subquery_program +); + + +-- This set of statements fixes the Error 1093 in the example using a temporary table +-- Create a temporary table +CREATE TEMPORARY TABLE temp_ids AS +SELECT id +FROM Program +WHERE department_id=1; + +-- Delete from the main table using the temporary table +DELETE FROM Program +WHERE id +IN (SELECT id FROM temp_ids); + +-- Drop the temporary table +DROP TEMPORARY TABLE IF EXISTS temp_ids; + +-- This statement fixes the Error 1093 using a self-join +DELETE t1 +FROM Program t1 +JOIN ( + SELECT id + FROM Program + WHERE department_id=1 +) AS t2 ON t1.id = t2.id; From 632b1b689b367a7785b283a13315027cfc3da5fa Mon Sep 17 00:00:00 2001 From: dvohra09 Date: Thu, 3 Jul 2025 09:19:51 -0700 Subject: [PATCH 4/6] Update fix-when-using-UPDATE-statement.sql --- .../fix-when-using-UPDATE-statement.sql | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql index a5116c7f..40ebd03d 100644 --- a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql +++ b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql @@ -1,37 +1,34 @@ -- This statement generates the Error 1093 -UPDATE Program -SET end_date = (SELECT start_date FROM Program); +UPDATE Program t +SET t.end_date = (SELECT start_date FROM Program WHERE id=t.id); -- This statement fixes the Error 1093 in the preceding example using a derived table -UPDATE Program -SET end_date = ( - SELECT subquery_program.start_date - FROM ( - SELECT start_date - FROM Program - WHERE description ='Major in Operating Systems' +UPDATE Program t +SET t.end_date = ( + SELECT subquery_program.start_date FROM ( + SELECT id, start_date FROM Program WHERE id=t.id ) AS subquery_program ); --- This statement generates the Error 1093 -UPDATE Program -SET name = (SELECT description FROM Program); - --- This set of statements fixes the Error 1093 in the preceding example using a temporary table +-- This set of statements fixes the Error 1093 in the same example using a temporary table -- Create a temporary table -CREATE TEMPORARY TABLE IF NOT EXISTS temp_table AS -SELECT id, description +CREATE TEMPORARY TABLE +IF NOT EXISTS temp_table +AS SELECT id, start_date FROM Program; -- Update the main table using the temporary table -UPDATE Program t -JOIN temp_table temp ON t.id = temp.id -SET t.name = temp.description; +UPDATE Program t +JOIN temp_table temp +ON t.id = temp.id +SET t.end_date = temp.start_date; -- Drop the temporary table -DROP TEMPORARY TABLE IF EXISTS temp_table; +DROP TEMPORARY TABLE +IF EXISTS temp_table; -- This statement fixes the Error 1093 using a self-join -UPDATE Program p1 -JOIN Program p2 ON p1.id = p2.id -SET p1.name = p2.description; +UPDATE Program p1 +JOIN Program p2 +ON p1.id = p2.id +SET p1.end_date = p2.start_date; From 73f8c2123624f5cd5c4787e5b5b2844f70080bab Mon Sep 17 00:00:00 2001 From: dvohra09 Date: Thu, 3 Jul 2025 09:23:11 -0700 Subject: [PATCH 5/6] Update fix-when-using-DELETE-statement.sql --- .../fix-when-using-DELETE-statement.sql | 43 ++++++++----------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql index 424edf42..648689c2 100644 --- a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql +++ b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-DELETE-statement.sql @@ -1,41 +1,36 @@ -- This statement generates the Error 1093 -DELETE FROM Program -WHERE id IN -(SELECT id FROM Program WHERE department_id=1); +DELETE FROM Program +WHERE start_date > +(SELECT MIN(start_date) FROM Program); -- This statement fixes the Error 1093 in the preceding example using a derived table DELETE FROM Program -WHERE id IN ( - SELECT subquery_program.id +WHERE start_date > ( + SELECT subquery_program.start_date FROM ( - SELECT id - FROM Program - WHERE department_id=1 + SELECT MIN(start_date) AS start_date FROM Program ) AS subquery_program ); -- This set of statements fixes the Error 1093 in the example using a temporary table --- Create a temporary table -CREATE TEMPORARY TABLE temp_ids AS -SELECT id -FROM Program -WHERE department_id=1; +- Create a temporary table +CREATE TEMPORARY TABLE temp_min_start_date AS +SELECT MIN(start_date) AS min_date +FROM Program; -- Delete from the main table using the temporary table DELETE FROM Program -WHERE id -IN (SELECT id FROM temp_ids); +WHERE start_date > +(SELECT min_date FROM temp_min_start_date); -- Drop the temporary table -DROP TEMPORARY TABLE IF EXISTS temp_ids; +DROP TEMPORARY TABLE temp_min_start_date; --- This statement fixes the Error 1093 using a self-join -DELETE t1 -FROM Program t1 -JOIN ( - SELECT id - FROM Program - WHERE department_id=1 -) AS t2 ON t1.id = t2.id; +-- This statement fixes the Error 1093 using a join with a derived table +DELETE p +FROM Program AS p +JOIN (SELECT MIN(start_date) AS min_start_date FROM Program) +AS min_program_date +ON p.start_date > min_program_date.min_start_date; From 312b738bb7d6df3f854654463ff96c768bf587c5 Mon Sep 17 00:00:00 2001 From: dvohra09 Date: Sun, 6 Jul 2025 10:20:29 -0700 Subject: [PATCH 6/6] Update fix-when-using-UPDATE-statement.sql --- .../fix-when-using-UPDATE-statement.sql | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql index 40ebd03d..6c544df0 100644 --- a/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql +++ b/sql-queries-11/how-to-interpret-and-fix-the-MySQL-Error-1093/fix-when-using-UPDATE-statement.sql @@ -3,11 +3,13 @@ UPDATE Program t SET t.end_date = (SELECT start_date FROM Program WHERE id=t.id); -- This statement fixes the Error 1093 in the preceding example using a derived table -UPDATE Program t -SET t.end_date = ( - SELECT subquery_program.start_date FROM ( - SELECT id, start_date FROM Program WHERE id=t.id - ) AS subquery_program +UPDATE Program +SET end_date = ( + SELECT max_start_date + FROM ( + SELECT MAX(start_date) AS max_start_date + FROM Program + ) AS derived_table ); -- This set of statements fixes the Error 1093 in the same example using a temporary table