File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
sql-queries-5/updating-data-with-joins-in-sql Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments