Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQL-195] - Merging Two Rows in SQL #134

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mysql-queries/mysql-store-json/creating-table-example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE Departments (
id INT PRIMARY KEY NOT NULL,
name VARCHAR(50) NOT NULL,
info JSON
);
14 changes: 14 additions & 0 deletions mysql-queries/mysql-store-json/inserting-json-data.sql
Original file line number Diff line number Diff line change
@@ -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));
5 changes: 5 additions & 0 deletions mysql-queries/mysql-store-json/retrieving-json-data.sql
Original file line number Diff line number Diff line change
@@ -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;
19 changes: 19 additions & 0 deletions mysql-queries/mysql-store-json/updating-json-data.sql
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 15 additions & 0 deletions sql-queries-3/merging-two-rows/common-table-expression.sql
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 15 additions & 0 deletions sql-queries-3/merging-two-rows/updating-with-coalesce.sql
Original file line number Diff line number Diff line change
@@ -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);
16 changes: 16 additions & 0 deletions sql-queries-3/merging-two-rows/using-insert-into-and-delete.sql
Original file line number Diff line number Diff line change
@@ -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;