SQL Query Interview Questions with Answers
Error in SQL:
Error: SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]At least one of the arguments to COALESCE must be an expression that is not the NULL constant.Inner Join: It is used to retrieve matching records from both the tables
Department:
| Department_No | Department_Name |
| 10 | ECE |
| 20 | ECE |
| 30 | CSE |
| 40 | IT |
Employee Details:
| Employee_No | Emp_Name | Address | Age | Department_No | Salary |
| 1 | Anil | Hyderabad | 23 | 10 | 20000 |
| 2 | Sunil | Hyderabad | 22 | 10 | 21000 |
| 3 | Ajay | Chennai | 24 | 20 | 23000 |
| 4 | Vijay | Chennai | 25 | 30 | 22000 |
| 5 | James | Hyderabad | 24 | 50 | 230000 |
Q1) Write a Query to display employee details who are working in the ECE department?
Ans.
1 2 3 4 | SELECT employee.employee_name, employee.address, employee.salary, employee.age, FROM Department DINNER JOIN Employees EON department.D_no=employee.D_no WHERE department.D_name= ‘ECE’ |
Q2) Write a Query to display employee details?
Ans.
1 | SELECT * FROM employee; |
Q3) Write a Query to display employee details along with department_name?
Ans.
1 2 3 4 | SELECT employee.employee_no, employee.employee_name, employee.address, employee.salary, employee.age, department.department_name FROM department DINNER JOIN employee EON department.D_no=employee.D_no |
Q4) Write a Query to display employee details whose sal>20000 and who is working in the ECE department?
Ans.
1 2 3 4 5 | SELECT employee.employee_no, employee.employee_name, employee.address, employee.salary, employee.age FROM department DINNER JOIN employee E ON dept.D_no=emp.D_no WHERE dept.D_name=’ECE’ and E.salary>20000 |
5) Write a Query to display employee details along with department_name and who is working in the ECE department, whose name starts with a?
Ans.
1 2 3 4 5 | SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age, dept.dname FROM department DINNER JOIN employee EON dept.D_no=emp.D_no WHERE dept.D_name=’ECE’ and emp.E_name like ‘a%’ |
Q6) Write a Query to display employee details along with department_name and whose age between 20 and 24?
Ans.
1 2 3 4 5 | SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age, dept.d_name FROM department DINNER JOIN employee E ON dept.D_no=emp.D_no WHERE E.age between 20 and 24 |
Q7) Write a Query to display employee details along with department_name and who are staying in Hyderabad?
Ans.
1 2 3 4 5 | SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age, dept.d_name FROM department DINNER JOIN employee E ON dept.D_no=emp.D_no WHERE E.address=’hyd’ |
Q8) Write a Query to display employee details whose salary>20000 and whose age>20 & who is working in the ECE department?
Ans.
1 2 3 4 5 | SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age, dept.d_name FROM department DINNER JOIN employee EON dept.D_no=emp.D_no WHERE E.age>20 and E.salary>20000 and dept.D_name=’ECE’ |
State Table:
| State ID | State Name |
| S1 | Telangana |
| S2 | AP |
| S3 | Tamil Nadu |
| S4 | Karnataka |
| S5 | Kerala |
City
| City ID | City Name | State ID |
| 1 | Hyderabad | S1 |
| 2 | Vizag | S2 |
| 3 | Vijayawada | S2 |
| 4 | Chennai | S3 |
| 5 | Madhurai | S3 |
| 6 | Bangalore | S4 |
Blood Group Details
| Blood Group ID | Blood Group |
| B1 | A+ve |
| B2 | B+ve |
| B3 | AB +ve |
| B4 | A -ve |
| B5 | O +ve |
Donor Details
| Donor ID | Donor Name | Phone Number | City ID | Blood Group ID |
| D1 | Anil | 9999 | 1 | B1 |
| D2 | Sunil | 8888 | 1 | B1 |
| D3 | Ajay | 7777 | 2 | B1 |
| D4 | John | 6666 | 4 | B3 |
| D5 | James | 5555 | 4 | B5 |
Q9) Write a Query to display city names belongs to AP?
Ans.
1 2 3 4 5 | SELECT C.City_NameFROM State SINNER JOIN City CON S.State_IDWHERE S.State_Name ‘AP’ |
Q10) Write a Query to display Donor_ID, Donor_Name, Phone No, City?
Ans.
1 2 3 4 | SELECT D.Donor_ID, D_Name, D_Phone No, C.City_Name FROM Donor DINNER JOIN City CON D.City_ID=C.City_ID |
Q11) Write a Query to display Donor_ID, Donor_Name, Phone No, Blood Group?
Ans.
1 2 3 4 | SELECT D.Donor_ID, D_Name, D_Phone No, B.Blood_GroupFROM Donor DINNER JOIN Blood BON D.Blood_ID=B.Blood_ID; |
Q12) Write a Query to display Donor_ID, Donor_Name, Phone No and who are staying in Hyderabad?
Ans.
1 2 3 4 5 | SELECT D.Donor_ID, D_Name, D_Phone No, C.City_Name FROM Donor DINNER JOIN City CON C.City_ID=D.City_IDWHERE C.City_Name=’hyderabad’ |
Q13) Write a Query to display donor details whose blood group is A +ve?
Ans.
1 2 3 4 5 | SELECT D.Donor_ID, D_Name, D_Phone NoFROM Donor DINNER JOIN Blood BON D.Donor_ID=B.Blood_ID WHERE B.Blood_Group=’A+ve’ |
Q14) Write a Query to display Donor_ID, Donor_Name, Phone No, City, Blood Group?
Ans.
1 2 3 4 5 6 | SELECT D.Donor_ID, D_Name, D_Phone No, C.City_Name B.Blood_GroupFROM Blood BINNER JOIN Donor DON D.Blood_ID=B.Donor_NameINNER JOIN City CON D.City_ID=C.City_ID |
Q15) Write a Query to display Donor_Name, Phone No, Blood Group of the donors who are staying in Hyderabad and whose blood group is A+ve?
Ans.
1 2 3 4 5 6 7 | SELECT D.Donor_Name, D. Phone_Number, B.Blood_GroupFROM Donor DINNER JOIN Blood BON D.Blood_ID=B.Blood_IDINNER JOIN City CON D.City_ID=C.City_ID WHERE C.City_Name=’hyderabad’ and B.Blood_Group=’A+ve’ |
Outer Join A join that includes rows even if they do not have related rows in the joined table is an Outer Join.. You can create three different outer join to specify the unmatched rows to be included:
- Left Outer Join
- Right Outer Join
- Full Outer Join
Employee Details Table
| Employee_No | Employee_Name | Dept_No |
| 101 | Anil | 10 |
| 102 | Sunil | 20 |
| 103 | Ajay | 30 |
| 104 | Vijay | 40 |
| 105 | Null | Null |
Department Details Table
| Dept_No | Depat_Name |
| 10 | EEE |
| 20 | EEE |
| 30 | CSE |
| Null | Null |
| 50 | IT |
Q16) Write a Query to display only left records?
Ans.
1 2 3 4 5 | SELECT e.*FROM Employee ELEFT OUTER JOIN Department DON E.D_noWHERE D.D_No IS NULL |
Q17) Write a Query to display employee details where employee no is 101?
Ans.
1 2 3 | SELECT *FROM Employee EWHERE E_No=101 |
Q18) Write a Query to display employee details where employee number is null?
Ans.
1 2 3 | SELECT *FROM Employee EWHERE E_No IS NULL |
Q19) Write a Query to display only right records?
Ans.
1 2 3 4 5 | SELECT D.*FROM Employee ERIGHT OUTER JOIN Department DON E.D.No=D.D_NoWHERE E.D_No IS NULL |
Q20) Write a Query to display all the records from the table except matching records?
Ans.
1 2 3 4 5 | SELECT E.*, D.*FROM Employee EFULL JOIN Department DON E.D_No=D.D_No WHERE E.D_No IS NULL or D.D_No IS NULL |
Department Details Table
| Dept_No | Dept_Name |
| 1 | ECE |
| 2 | CSE |
| 3 | EEE |
Course Details Table
| Course_ID | Course_Name | Cr |
| 1 | EDC | 4 |
| 2 | PDC | 4 |
| 3 | SS | 4 |
| 4 | DAA | 4 |
| 5 | OS | 4 |
Student Details Table
| Student_No | Student_Name |
| 101 | Anil |
| 102 | Sunil |
| 103 | Ajay |
| 104 | Vijay |
| 105 | John |
Enroll Details Table
| Enroll_Date | Student_No | Dpet_No | S_ID |
| 1/2/2014 | 101 | 10 | S1 |
| 3/2/2016 | 102 | 10 | S1 |
| 3/2/2016 | 103 | 10` | S1 |
| 3/2/2016 | 104 | 20 | S2 |
| 3/2/2016 | 105 | 20 | S2 |
Address Table
| Emp_No | Address |
| E1 | Hyderabad |
| E2 | Vizag |
| E3 | Hyderabad |
| E4 | Bangalore |
| E5 | Hyderabad |
Employee Details Table
| Emp_No | Emp_Name |
| E1 | Arun |
| E2 | Kiran |
| E3 | Kumar |
| E4 | Anus |
| E5 | James |
Semester Details Table
| Semester | Sn |
| S1 | 1 |
| S2 | 2-1 |
| S3 | 2-2 |
| S4 | 3-1 |
| S5 | 3-2 |
| S6 | 4-1 |
| S7 | 4-2 |
Course Department Details
| Dept_No | Course_ID |
| 10 | 1 |
| 10 | 2 |
| 10 | 3 |
| 20 | 4 |
| 20 | 5 |
Syllabus Table
| Dept_No | Course_ID | S_ID |
| 10 | 1 | S1 |
| 10 | 2 | S1 |
| 10 | 3 | S1 |
| 20 | 4 | S2 |
| 20 | 5 | S2 |
Instructor Details Table
| Emp_No | Dept_No |
| E1 | 10 |
| E2 | 10 |
| E3 | 10 |
| E4 | 20 |
| E5 | 30 |
Course Instructor Table
| Course_ID | Emp_No | S_ID | Dept_No |
| 1 | E1 | S1 | 10 |
| 1 | E1 | S1 | 20 |
| 1 | E2 | S1 | 30 |
| 2 | E3 | S1 | 10 |
| 4 | E4 | S2 | 20 |
| 5 | E4 | S2 | 20 |
| 5 | E5 | S1 | 10 |
Q21) Write a query to display Student No, Student Name, Enroll Date, Department Name?
Ans.
1 2 3 4 5 6 | SELECT S.Student_No, S.Student_Name, S.Enroll_Date, D.Dept_Name FROM Student SINNER JOIN Enroll EON S.Student_No=E.Student_NoINNER JOIN Department DON D.Dept_No=E.Dept_No |
Q22) Write a query to display Employee Number, Employee Name and address, department name?
Ans.
1 2 3 4 5 6 7 8 | SELECT E.Emp_No, E.Emp_Name, A.Address, D.Dept_NameFROM Employee EINNER JOIN Address AON E.Emp_No=A.Emp_NoINNER JOIN Instructor ION A.Emp_No=I.Emp_NoINNER JOIN Department DON I.Dept_No=D.Dept_No |
Q23) Write a query to display course name belongs to ECE department?
Ans.
1 2 3 4 5 6 7 | SELECT C.Course_NameFROM Department DINNER JOIN Course Department CDON D.Dept_NO=CD.Dept_NOINNER JOIN Course CON CD.CourseDept_ID=C.Course_IDWHERE D.Dept_Name=’ECE’ |
Q24) Write a query to display student number, student name, enroll date, dept name, semester name?
Ans.
1 2 3 4 5 6 7 8 | SELECT S.Student_No, S.Student_Name, S.Enroll_Date, D.Dpet_Name, Sem.Student_NameFROM Enroll EINNER JOIN Student SON S.Student_No=E.Student_NoINNER JOIN Deprtment DON E.Dept_No=D.Dept_No INNER JOIN Semester SEON E.Student_ID=Sem.Student_ID |
Q25) Write a query to display the syllabus of ECE department 1st year?
Ans.
1 2 3 4 5 6 7 8 9 | SELECT C.Course_NameFROM Department DINNER JOIN Syllabus SyON D.Dept_No=Sy.Dept_NoINNER JOIN Course CON Sy.Course_ID=C.Course_IDINNER JOIN Semester SeON Syllabus_Sy_ID=Se_Sy_IDWHERE D.Dept_Name=’ECE’ and Se.Semester=’1’ |
Q26) Write a query to display the employee names and faculty names of ECE dept 1st year?
Ans.
1 2 3 4 5 6 7 8 9 | SELECT E.Emp_NameFROM Employee EINNER JOIN Course Instructor CiON E.Emp_No=Ci.Emp_NoINNER JOIN Semester SeON Se.Student_ID=Ci.Student_IDINNER JOIN Dept DON Ci.Dept_No=D.Dept_NoWHERE D.Dept_Name=’ECE’ and Se.Student_Name=’1’ |
Q27) Write a query to display student details who enrolled for the ECE department?
Ans.
1 2 3 4 5 6 7 | SELECT S.Student_NO, S.Student_Name, S.Enroll_DateFROM Student SINNER JOIN Enroll EON S.Student_No=E.Student_NoINNER JOIN Department DON E.Dept_No=D.Dept_NoWHERE D.Dept_Name=’ECE’ |
Q28) Write a query to display student details along with dept name who are enrolled in the ECE department the first year?
Ans.
1 2 3 4 5 6 7 8 9 | SELECT S.Student_No, S.Student_Name, S.Enroll_Date, D.Dept_NameFROM Student SINNER JOIN Enrollment EON S.Student_No=E.Student_NoINNER JOIN Department DON D.Dept_No=E.Dept_NoINNER JOIN Semester SeON E.Student_ID=Se.Student_IDWHERE D.Dept_Name=’ECE’ and Se.Student_Name=’1’ |
Q29) Write a query to display employee name who is teaching EDC?
Ans.
1 2 3 4 5 6 7 | SELECT E.Emp_NameFROM Employee EINNER JOIN Course Instructor CiON E.Emp_No=Ci.Emp_NoINNER JOIN Course CON Ci.Course_ID=C.Course_IDWHERE C.Course_Name=’EDC’ |
Q30) Write a query to display employee details along with dept name who are staying in Hyderabad?
Ans.
1 2 3 4 5 6 7 8 9 | SELECT E.Emp_No, Emp_Name, D.Dept_NameFROM Employee EINNER JOIN Address AON E.Emp_No=A.Emp_NoINNER JOIN Instructor ION A.Emp_No=I.Emp_NoINNER JOIN Department DON I.Dept_No=D.Dept_NoWHERE A.Address=’hyderabad’ |
| Emp_No | Emp_Name | Salary | Age | Dept_Name |
| 101 | Anil | 20,000 | 22 | ECE |
| 102 | Sunil | 23000 | 23 | EEE |
| 103 | Vijay | 32000 | 24 | CSE |
Using Range Operator:: BETWEEN, NOT BETWEEN
Q31) Write a Query to display employee details whose salary > 20000 and whose age >23?
Ans.
1 2 | SELECT * FROM Employee WHERE Salary>20000 AND Age>23; |
Q32) Write a Query to display employee details whose salary >20000 and who is working in the ECE department?
Ans.
1 2 | SELECT * FROM EmployeeWHERE Salary>20000 AND Dept_Name=’ECE’ |
Q33) Write a Query to display employee details whose age is BETWEEN 18 and 22?
Ans.
1 2 | SELECT * FROM Employee Details WHERE Age BETWEEN 18 AND 22; |
Q34) Write a Query to display employee details whose salary range BETWEEN 20000 and 23000?
Ans.
1 2 | SELECT * FROM Employee WHERE Salary BETWEEN 20000 AND 23000; |
Q35) Write a Query to display employee details whose age is NOT BETWEEN 18 & 22?
Ans.
1 2 | SELECT * FROM EmployeeWHERE Age NOT BETWEEN 18 AND 22; |
Using String Operators:: LIKE, NOT LIKE
Q36) Write a Query to display employee details whose name starts with a?
Ans.
1 2 | SELECT * FROM EmployeeWHERE Emp_Name LIKE ‘a%’ |
a% ----> starts with a
%a ----> ends with a
Q37) Write a Query to display employee details and whose age>20 & whose name starts with a?
Ans.
1 2 | SELECT * FROM EmployeeWHERE Salary>20000 AND Age>20 AND Emp_Name LIKE ‘a%’ |
Q38) Write a Query to display employee details whose name not starts with a?
Ans.
1 2 | SELECT * FROM employeeWHERE Emp_Name NOT LIKE ‘a%’ |
No comments:
Post a Comment