From 6ce6078f96c86c039ade9a3c7d1f7f9a3713e9b9 Mon Sep 17 00:00:00 2001 From: GeoSegun Date: Fri, 26 Jul 2024 17:54:58 -0400 Subject: [PATCH 1/4] [SQL-156](Storing Data in MySQL as JSON) --- .../creating-table-example.sql | 5 +++++ .../inserting-json-data.sql | 14 ++++++++++++++ .../retrieving-json-data.sql | 5 +++++ .../updating-json-data.sql | 19 +++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 sql-queries/storing-data-in-mysql-as-json/creating-table-example.sql create mode 100644 sql-queries/storing-data-in-mysql-as-json/inserting-json-data.sql create mode 100644 sql-queries/storing-data-in-mysql-as-json/retrieving-json-data.sql create mode 100644 sql-queries/storing-data-in-mysql-as-json/updating-json-data.sql diff --git a/sql-queries/storing-data-in-mysql-as-json/creating-table-example.sql b/sql-queries/storing-data-in-mysql-as-json/creating-table-example.sql new file mode 100644 index 00000000..b15fee24 --- /dev/null +++ b/sql-queries/storing-data-in-mysql-as-json/creating-table-example.sql @@ -0,0 +1,5 @@ +CREATE TABLE Departments ( + id INT PRIMARY KEY NOT NULL, + name VARCHAR(50) NOT NULL, + info JSON +); \ No newline at end of file diff --git a/sql-queries/storing-data-in-mysql-as-json/inserting-json-data.sql b/sql-queries/storing-data-in-mysql-as-json/inserting-json-data.sql new file mode 100644 index 00000000..d4dac5b7 --- /dev/null +++ b/sql-queries/storing-data-in-mysql-as-json/inserting-json-data.sql @@ -0,0 +1,14 @@ +INSERT INTO Departments (id, name, info) VALUES +(1, 'Computer Science', '{"head": "Dr. Smith", "location": "Building 1", "courses": ["Algorithms", "Data Structures"]}'); + +INSERT INTO Departments (id, name, info) VALUES +(2, 'Mathematics', JSON_OBJECT('head', 'Dr. Johnson', 'location', 'Building 2', 'courses', JSON_ARRAY('Calculus', 'Linear Algebra'))); + +INSERT INTO Departments (id, name, info) VALUES +(3, 'Physics', JSON_OBJECT('head', 'Dr. Clark', 'location', 'Building 3', 'courses', JSON_ARRAY('Quantum Mechanics', 'Thermodynamics'))); + +SET @head = 'Dr. Miller'; +SET @location = 'Building 4'; +SET @courses = JSON_ARRAY('Classical Mechanics', 'Electromagnetism'); +INSERT INTO Departments (id, name, info) VALUES +(4, 'Engineering', JSON_OBJECT('head', @head, 'location', @location, 'courses', @courses)); \ No newline at end of file diff --git a/sql-queries/storing-data-in-mysql-as-json/retrieving-json-data.sql b/sql-queries/storing-data-in-mysql-as-json/retrieving-json-data.sql new file mode 100644 index 00000000..58c10ffe --- /dev/null +++ b/sql-queries/storing-data-in-mysql-as-json/retrieving-json-data.sql @@ -0,0 +1,5 @@ +SELECT id, name, info FROM Departments; + +SELECT id, name, JSON_EXTRACT(info, '$.head') AS head FROM Departments; + +SELECT id, name, info->'$.head' AS head FROM Departments; \ No newline at end of file diff --git a/sql-queries/storing-data-in-mysql-as-json/updating-json-data.sql b/sql-queries/storing-data-in-mysql-as-json/updating-json-data.sql new file mode 100644 index 00000000..20aa4f54 --- /dev/null +++ b/sql-queries/storing-data-in-mysql-as-json/updating-json-data.sql @@ -0,0 +1,19 @@ +UPDATE Departments +SET info = JSON_SET(info, '$.head', 'Dr. Parker') +WHERE id = 1; +SELECT id, name, info FROM Departments WHERE id = 1; + +UPDATE Departments +SET info = JSON_REPLACE(info, '$.location', 'Building 2A') +WHERE id = 2; +SELECT id, name, info FROM Departments WHERE id = 2; + +UPDATE Departments +SET info = JSON_REMOVE(info, '$.courses') +WHERE id = 3; +SELECT id, name, info FROM Departments WHERE id = 3; + +UPDATE Departments +SET info = JSON_ARRAY_APPEND(info, '$.courses', 'Artificial Intelligence') +WHERE id = 4; +SELECT id, name, info FROM Departments WHERE id = 4; \ No newline at end of file From 474c8cdc7ab594f2a7b49d9246383fb13a7d2ee2 Mon Sep 17 00:00:00 2001 From: GeoSegun Date: Mon, 29 Jul 2024 16:16:17 -0400 Subject: [PATCH 2/4] [SQL-156](Storing Data in MySQL as JSON) --- .../mysql-storing-data-as-json}/creating-table-example.sql | 0 .../mysql-storing-data-as-json}/inserting-json-data.sql | 0 .../mysql-storing-data-as-json}/retrieving-json-data.sql | 0 .../mysql-storing-data-as-json}/updating-json-data.sql | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename {sql-queries/storing-data-in-mysql-as-json => mysql-queries/mysql-storing-data-as-json}/creating-table-example.sql (100%) rename {sql-queries/storing-data-in-mysql-as-json => mysql-queries/mysql-storing-data-as-json}/inserting-json-data.sql (100%) rename {sql-queries/storing-data-in-mysql-as-json => mysql-queries/mysql-storing-data-as-json}/retrieving-json-data.sql (100%) rename {sql-queries/storing-data-in-mysql-as-json => mysql-queries/mysql-storing-data-as-json}/updating-json-data.sql (100%) diff --git a/sql-queries/storing-data-in-mysql-as-json/creating-table-example.sql b/mysql-queries/mysql-storing-data-as-json/creating-table-example.sql similarity index 100% rename from sql-queries/storing-data-in-mysql-as-json/creating-table-example.sql rename to mysql-queries/mysql-storing-data-as-json/creating-table-example.sql diff --git a/sql-queries/storing-data-in-mysql-as-json/inserting-json-data.sql b/mysql-queries/mysql-storing-data-as-json/inserting-json-data.sql similarity index 100% rename from sql-queries/storing-data-in-mysql-as-json/inserting-json-data.sql rename to mysql-queries/mysql-storing-data-as-json/inserting-json-data.sql diff --git a/sql-queries/storing-data-in-mysql-as-json/retrieving-json-data.sql b/mysql-queries/mysql-storing-data-as-json/retrieving-json-data.sql similarity index 100% rename from sql-queries/storing-data-in-mysql-as-json/retrieving-json-data.sql rename to mysql-queries/mysql-storing-data-as-json/retrieving-json-data.sql diff --git a/sql-queries/storing-data-in-mysql-as-json/updating-json-data.sql b/mysql-queries/mysql-storing-data-as-json/updating-json-data.sql similarity index 100% rename from sql-queries/storing-data-in-mysql-as-json/updating-json-data.sql rename to mysql-queries/mysql-storing-data-as-json/updating-json-data.sql From 5dfbc03e91a38f2a7c43b140b7b2244482a7a931 Mon Sep 17 00:00:00 2001 From: GeoSegun Date: Tue, 30 Jul 2024 16:55:31 -0400 Subject: [PATCH 3/4] [SQL-156](Storing Data in MySQL as JSON) --- .../creating-table-example.sql | 0 .../inserting-json-data.sql | 0 .../retrieving-json-data.sql | 0 .../updating-json-data.sql | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename mysql-queries/{mysql-storing-data-as-json => mysql-store-json}/creating-table-example.sql (100%) rename mysql-queries/{mysql-storing-data-as-json => mysql-store-json}/inserting-json-data.sql (100%) rename mysql-queries/{mysql-storing-data-as-json => mysql-store-json}/retrieving-json-data.sql (100%) rename mysql-queries/{mysql-storing-data-as-json => mysql-store-json}/updating-json-data.sql (100%) diff --git a/mysql-queries/mysql-storing-data-as-json/creating-table-example.sql b/mysql-queries/mysql-store-json/creating-table-example.sql similarity index 100% rename from mysql-queries/mysql-storing-data-as-json/creating-table-example.sql rename to mysql-queries/mysql-store-json/creating-table-example.sql diff --git a/mysql-queries/mysql-storing-data-as-json/inserting-json-data.sql b/mysql-queries/mysql-store-json/inserting-json-data.sql similarity index 100% rename from mysql-queries/mysql-storing-data-as-json/inserting-json-data.sql rename to mysql-queries/mysql-store-json/inserting-json-data.sql diff --git a/mysql-queries/mysql-storing-data-as-json/retrieving-json-data.sql b/mysql-queries/mysql-store-json/retrieving-json-data.sql similarity index 100% rename from mysql-queries/mysql-storing-data-as-json/retrieving-json-data.sql rename to mysql-queries/mysql-store-json/retrieving-json-data.sql diff --git a/mysql-queries/mysql-storing-data-as-json/updating-json-data.sql b/mysql-queries/mysql-store-json/updating-json-data.sql similarity index 100% rename from mysql-queries/mysql-storing-data-as-json/updating-json-data.sql rename to mysql-queries/mysql-store-json/updating-json-data.sql From 19316042b606cf97188ec6d952d90e984953fb19 Mon Sep 17 00:00:00 2001 From: GeoSegun Date: Fri, 9 Aug 2024 03:29:59 -0400 Subject: [PATCH 4/4] Merging Two Rows in SQL --- .../merging-two-rows/common-table-expression.sql | 15 +++++++++++++++ .../merging-two-rows/updating-with-coalesce.sql | 15 +++++++++++++++ .../using-insert-into-and-delete.sql | 16 ++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 sql-queries-3/merging-two-rows/common-table-expression.sql create mode 100644 sql-queries-3/merging-two-rows/updating-with-coalesce.sql create mode 100644 sql-queries-3/merging-two-rows/using-insert-into-and-delete.sql diff --git a/sql-queries-3/merging-two-rows/common-table-expression.sql b/sql-queries-3/merging-two-rows/common-table-expression.sql new file mode 100644 index 00000000..6cce7218 --- /dev/null +++ b/sql-queries-3/merging-two-rows/common-table-expression.sql @@ -0,0 +1,15 @@ +WITH MergedStudent AS ( + SELECT + 2017 AS id, + COALESCE(MAX(name), MIN(name)) AS name, + COALESCE(MAX(national_id), MIN(national_id)) AS national_id, + COALESCE(MAX(birth_date), MIN(birth_date)) AS birth_date, + COALESCE(MAX(enrollment_date), MIN(enrollment_date)) AS enrollment_date, + COALESCE(MAX(graduation_date), MIN(graduation_date)) AS graduation_date, + COALESCE(MAX(gpa), MIN(gpa)) AS gpa + FROM + (SELECT * FROM Student WHERE id = 2017 + UNION ALL + SELECT * FROM Student WHERE id = 2008) s + ) + SELECT * FROM MergedStudent; diff --git a/sql-queries-3/merging-two-rows/updating-with-coalesce.sql b/sql-queries-3/merging-two-rows/updating-with-coalesce.sql new file mode 100644 index 00000000..735e08bc --- /dev/null +++ b/sql-queries-3/merging-two-rows/updating-with-coalesce.sql @@ -0,0 +1,15 @@ +SELECT * FROM Student WHERE id IN (1011, 1610); + +UPDATE Student AS target + SET name = COALESCE(target.name, source.name), + national_id = COALESCE(target.national_id, source.national_id), + birth_date = COALESCE(target.birth_date, source.birth_date), + enrollment_date = COALESCE(target.enrollment_date, source.enrollment_date), + graduation_date = COALESCE(target.graduation_date, source.graduation_date), + gpa = COALESCE(target.gpa, source.gpa) + FROM Student AS source + WHERE target.id = 1011 AND source.id = 1610; + +DELETE FROM Student WHERE id = 1610; + +SELECT * FROM Student WHERE id IN (1011); \ No newline at end of file diff --git a/sql-queries-3/merging-two-rows/using-insert-into-and-delete.sql b/sql-queries-3/merging-two-rows/using-insert-into-and-delete.sql new file mode 100644 index 00000000..8208f262 --- /dev/null +++ b/sql-queries-3/merging-two-rows/using-insert-into-and-delete.sql @@ -0,0 +1,16 @@ +INSERT INTO Student (id, name, national_id, birth_date, enrollment_date, graduation_date, gpa) + SELECT + 1000, + COALESCE(s1.name, s2.name), + COALESCE(s1.national_id, s2.national_id), + COALESCE(s1.birth_date, s2.birth_date), + COALESCE(s1.enrollment_date, s2.enrollment_date), + COALESCE(s1.graduation_date, s2.graduation_date), + COALESCE(s1.gpa, s2.gpa) + FROM + (SELECT * FROM Student WHERE id = 1717) s1, + (SELECT * FROM Student WHERE id = 1719) s2; + +DELETE FROM Student WHERE id IN (1717, 1719); + +SELECT * FROM Student WHERE id = 1000; \ No newline at end of file