Skip to content

Add update-using-select folder with three SQL Server update methods #372

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

Merged
merged 3 commits into from
Aug 3, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Method 2: Update via JOIN
UPDATE Course
SET is_active =
CASE WHEN Department.code = 'EC' THEN 'Yes' ELSE 'No' END
FROM Course
JOIN Department ON Course.department_id = Department.id;

-- View result
SELECT * FROM Course;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Method 3: Update via MERGE
MERGE Course AS Target
USING (
SELECT id, code FROM Department
) AS Source
ON Target.department_id = Source.id
WHEN MATCHED THEN
UPDATE SET Target.is_active =
CASE
WHEN Source.code = 'ME' THEN 'Yes'
ELSE 'No'
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Method 1: Update via Subquery
UPDATE Course
SET is_active = (
SELECT CASE
WHEN Department.code = 'CS' THEN 'Yes'
ELSE 'No'
END
FROM Department
WHERE Department.id = Course.department_id
);

-- View result
SELECT * FROM Course;