Skip to content
Merged
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
26 changes: 26 additions & 0 deletions sql-queries-3/remove-duplicates/remove-duplicates-postgresql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- Inserting Duplicate Records
INSERT INTO Registration (id, semester, year, course_id, student_id)
VALUES
(100, 'Fall', 2021, 'CS211', 1001),
(200, 'Fall', 2021, 'CS211', 1001),
(300, 'Spring', 2022, 'CS212', 1007),
(400, 'Spring', 2022, 'CS212', 1007),
(500, 'Spring', 2022, 'CS213', 1003),
(600, 'Spring', 2022, 'CS213', 1003);


-- Deleting Duplicates using the USING clause
DELETE FROM Registration
USING (
SELECT id
FROM (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY semester, year, course_id, student_id
ORDER BY id
) as rn
FROM Registration
) t
WHERE t.rn > 1
) duplicate
WHERE Registration.id = duplicate.id;
21 changes: 21 additions & 0 deletions sql-queries-3/remove-duplicates/remove-duplicates-sqlserver.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Inserting Duplicate Records
INSERT INTO Registration (id, semester, year, course_id, student_id)
VALUES
(100, 'Fall', 2021, 'CS211', 1001),
(200, 'Fall', 2021, 'CS211', 1001),
(300, 'Spring', 2022, 'CS212', 1007),
(400, 'Spring', 2022, 'CS212', 1007),
(500, 'Spring', 2022, 'CS213', 1003),
(600, 'Spring', 2022, 'CS213', 1003);

-- Deleting Duplicates using a Common Table Expression (CTE)
WITH COUNTED_DUPLICATES AS (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY semester, year, course_id, student_id
ORDER BY id
) AS rn
FROM Registration
)
DELETE FROM COUNTED_DUPLICATES
WHERE rn > 1;
59 changes: 59 additions & 0 deletions sql-queries-3/remove-duplicates/remove-duplicates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- Inserting Duplicate Records
INSERT INTO Registration (id, semester, year, course_id, student_id)
VALUES
(100, 'Fall', 2021, 'CS211', 1001),
(200, 'Fall', 2021, 'CS211', 1001),
(300, 'Spring', 2022, 'CS212', 1007),
(400, 'Spring', 2022, 'CS212', 1007),
(500, 'Spring', 2022, 'CS213', 1003),
(600, 'Spring', 2022, 'CS213', 1003);

-- Finding Duplicates using GROUP BY and COUNT
SELECT semester, year, course_id, student_id, COUNT(*) as count
FROM Registration
GROUP BY semester, year, course_id, student_id
HAVING COUNT(*) > 1;
Select version();
-- Finding Duplicates using ROW_NUMBER() with PARTITION BY
SELECT id, semester, year, course_id, student_id,
ROW_NUMBER() OVER (
PARTITION BY semester, year, course_id, student_id
ORDER BY id
) AS rn
FROM
Registration;

-- Deleting Duplicates using Self-Join
DELETE r1 FROM Registration r1
INNER JOIN Registration r2
WHERE
r1.semester = r2.semester AND
r1.year = r2.year AND
r1.course_id = r2.course_id AND
r1.student_id = r2.student_id AND
r1.id > r2.id;

-- Inserting Duplicate Records
INSERT INTO Registration (id, semester, year, course_id, student_id)
VALUES
(100, 'Fall', 2021, 'CS211', 1001),
(200, 'Fall', 2021, 'CS211', 1001),
(300, 'Spring', 2022, 'CS212', 1007),
(400, 'Spring', 2022, 'CS212', 1007),
(500, 'Spring', 2022, 'CS213', 1003),
(600, 'Spring', 2022, 'CS213', 1003);

-- Deleting Duplicates using Subqueries
DELETE FROM Registration
WHERE id IN (
SELECT id
FROM (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY semester, year, course_id, student_id
ORDER BY id
) as rn
FROM Registration
) t
WHERE t.rn > 1
);