Skip to content

Commit 0998d20

Browse files
committed
updating-data-with-joins-in-sql
1 parent 2739a08 commit 0998d20

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- update data with case case condition
2+
UPDATE Student
3+
SET graduation_date = CASE
4+
WHEN e.semester = 'FALL' THEN '2024-12-15'
5+
WHEN e.semester = 'SPRING' THEN '2024-06-15'
6+
ELSE NULL
7+
END
8+
FROM Student AS s
9+
JOIN Exam AS e ON s.id = e.student_id;
10+
11+
12+
-- display outpt for SPRING semester
13+
SELECT * FROM Student AS s
14+
JOIN Exam AS e ON e.student_id = s.id
15+
WHERE e.semester = 'SPRING'
16+
LIMIT 10;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- display top 10 rows of the Student table
2+
SELECT * FROM Student
3+
LIMIT 10;
4+
5+
-- display top 10 result from a join of Exam
6+
-- and Student table
7+
SELECT * FROM Exam
8+
JOIN Student ON Exam.student_id = Student.id
9+
LIMIT 10;
10+
11+
12+
-- UPDATE JOIN with WHERE clause
13+
UPDATE Exam
14+
SET e.grade = 'C'
15+
FROM Exam AS e
16+
JOIN Student AS s ON e.student_id = s.id
17+
WHERE s.national_id = 123345566 AND e.grade IS NULL;
18+
19+
20+
-- confirm the output
21+
SELECT * FROM Exam AS e
22+
JOIN Student AS s ON e.student_id = s.id
23+
WHERE national_id = 123345566;

0 commit comments

Comments
 (0)