From e2fb121c279e35bea6e6360b7a7c4608609e766d Mon Sep 17 00:00:00 2001 From: Myla RamReddy Date: Tue, 3 Sep 2024 12:20:43 +0530 Subject: [PATCH] Update 01_SQL.md --- Notebooks/Interview Questions/01_SQL.md | 73 +++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/Notebooks/Interview Questions/01_SQL.md b/Notebooks/Interview Questions/01_SQL.md index a9f2b58..2c9fa97 100644 --- a/Notebooks/Interview Questions/01_SQL.md +++ b/Notebooks/Interview Questions/01_SQL.md @@ -10,4 +10,77 @@ 1. Can we get all salesmans information where dept name is Sales? select * from emp,dept where emp.deptno=dept.deptno and dept.deptno=30 and job = 'SALESMAN' +1. Write query to get studentname,teachername,subjectname based on below data. + +```sql +Create table dbo.student( +Student_ID int, +Student_Name Varchar(50), +Class int) + +Create table dbo.TEACHER( +TID int, +Teacher_Name Varchar(50), +) + +create table dbo.SUBJECT( +Sub_Id int, +Sub_Name Varchar(50)) + +create table dbo.STUDENT_SUBJECT( +Stu_Id int, +Sub_Id int, +Teacher_Id int) + +Create table dbo.Student_Marks( +Student_ID int, +Sub_Id int, +Marks int, +Class int) + +INSERT INTO dbo.student (Student_ID, Student_Name,Class) VALUES +(1, 'aaa',1), +(2, 'bbb',2); + +INSERT INTO dbo.TEACHER (TID, Teacher_Name) VALUES +(1, 'hhh'), +(2, 'gggg'); + +INSERT INTO dbo.SUBJECT (Sub_Id, Sub_Name) VALUES +(1, '2maths'), +(2, 'Science'); + +INSERT INTO STUDENT_SUBJECT (Stu_Id, Sub_id, Teacher_Id) VALUES +(1, 1, 1), +(2, 1, 1); + +INSERT INTO Student_Marks (Student_ID, Sub_Id, Marks, Class) VALUES +(1, 1, 34, 1), +(1, 2, 90, 1), +(2, 1, 55, 1), +(2, 2, 40, 1); +``` +``` sql +SELECT + st.Student_Name, + T.Teacher_Name, Sb.Sub_Name, sm.Marks, + CASE + WHEN sm.Marks >= 0 and sm.Marks < 35 THEN 'Fail' + WHEN sm.Marks >= 35 and sm.Marks < 50 THEN 'Just Pass' + WHEN sm.Marks >= 50 and sm.Marks < 60 THEN 'Second Class' + WHEN sm.Marks >= 60 and sm.Marks <= 100 THEN 'First Class' + END AS Grade +FROM + STUDENT_SUBJECT ss +JOIN + STUDENT st ON st.Student_ID = ss.Stu_Id +join + TEACHER T ON T.TID = ss.Teacher_Id +join SUBJECT sb ON sb.Sub_Id = ss.Sub_id +join Student_Marks sm ON sm.Sub_Id = ss.Sub_id and sm.Student_ID = st.Student_ID +``` + + + +2.