🧠 HOTS SQL Questions
🔹 Case Table: EMPLOYEE
EmpID Name Salary Dept City
101 Ravi 50000 IT Delhi
102 Anu 60000 HR Mumbai
103 John 45000 IT Delhi
104 Meena 70000 Sales Chennai
105 Raj 60000 IT Mumbai
🔥 Questions
1. Display names of employees whose salary is equal to maximum salary.
2. Display second highest salary.
3. Count number of employees in each department.
4. Display departments having more than 1 employee.
5. Display employees whose name starts with 'R'.
6. Display employees whose name ends with 'a'.
7. Display employees who are NOT from Delhi.
8. Display employees with salary between 50000 and 70000.
9. Display unique cities.
10. Display total salary of each department.
11. Display employees sorted by salary (descending).
12. Display highest salary in IT department.
13. Display employees whose salary is greater than average salary.
14. Update salary by 10% for employees in IT department.
15. Delete employees with salary less than 50000.
✅ Answers (Queries)
1.
SELECT Name FROM EMPLOYEE
WHERE Salary = (SELECT MAX(Salary) FROM EMPLOYEE);
2.
SELECT MAX(Salary) FROM EMPLOYEE
WHERE Salary < (SELECT MAX(Salary) FROM EMPLOYEE);
3.
SELECT Dept, COUNT(*)
FROM EMPLOYEE
GROUP BY Dept;
4.
SELECT Dept, COUNT(*)
FROM EMPLOYEE
GROUP BY Dept
HAVING COUNT(*) > 1;
5.
SELECT * FROM EMPLOYEE
WHERE Name LIKE 'R%';
6.
SELECT * FROM EMPLOYEE
WHERE Name LIKE '%a';
7.
SELECT * FROM EMPLOYEE
WHERE City <> 'Delhi';
8.
SELECT * FROM EMPLOYEE
WHERE Salary BETWEEN 50000 AND 70000;
9.
SELECT DISTINCT City FROM EMPLOYEE;
10.
SELECT Dept, SUM(Salary)
FROM EMPLOYEE
GROUP BY Dept;
11.
SELECT * FROM EMPLOYEE
ORDER BY Salary DESC;
12.
SELECT MAX(Salary)
FROM EMPLOYEE
WHERE Dept = 'IT';
13.
SELECT * FROM EMPLOYEE
WHERE Salary > (SELECT AVG(Salary) FROM EMPLOYEE);
14.
UPDATE EMPLOYEE
SET Salary = Salary + Salary*0.10
WHERE Dept = 'IT';
15.
DELETE FROM EMPLOYEE
WHERE Salary < 50000;
🎯 Common Mistakes (VERY
IMPORTANT)
Forgetting GROUP BY with aggregate functions
Using WHERE instead of HAVING
Missing quotes 'Delhi'
Confusing = and LIKE
⚡ Exam Trick
👉 If question says:
“group” → GROUP BY
“condition on group” → HAVING
“pattern” → LIKE
If you want next:
✅ SQL case study questions (CBSE pattern)
✅ Full practical exam viva questions
✅ Python + SQL combined questions
Python + SQL combined questions
Here are 🔥 Python + SQL Combined Questions (CBSE Board Level)
👉 Very important for practical + theory + viva
🧠 Python + SQL Combined Questions
🔹 Case Study 1: Insert Data
Write a Python program to connect to MySQL database and insert a record into table
STUDENT.
✅ Answer:
import [Link]
con = [Link](
host="localhost",
user="root",
password="your_password",
database="school"
)
cursor = [Link]()
query = "INSERT INTO STUDENT (RollNo, Name, Marks, City) VALUES (%s,%s,%s,
%s)"
data = (1, "Ravi", 85, "Delhi")
[Link](query, data)
[Link]()
print("Record Inserted")
[Link]()
🔹 Case Study 2: Fetch Data
Write a Python program to display all records from STUDENT.
✅ Answer:
import [Link]
con = [Link](host="localhost", user="root",
password="your_password", database="school")
cursor = [Link]()
[Link]("SELECT * FROM STUDENT")
records = [Link]()
for row in records:
print(row)
[Link]()
🔹 Case Study 3: Condition Query
Write a Python program to display students scoring more than 80 marks.
✅ Answer:
[Link]("SELECT * FROM STUDENT WHERE Marks > 80")
for row in [Link]():
print(row)
🔹 Case Study 4: Update Record
Write a Python program to update marks of a student.
✅ Answer:
query = "UPDATE STUDENT SET Marks = 95 WHERE RollNo = 2"
[Link](query)
[Link]()
print("Record Updated")
🔹 Case Study 5: Delete Record
Write a Python program to delete a student record.
✅ Answer:
query = "DELETE FROM STUDENT WHERE RollNo = 3"
[Link](query)
[Link]()
print("Record Deleted")
🔹 Case Study 6: Count Records
Write a Python program to count total students.
✅ Answer:
[Link]("SELECT COUNT(*) FROM STUDENT")
result = [Link]()
print("Total Students:", result[0])
🔹 Case Study 7: Search by Name
Write a Python program to search student by name.
✅ Answer:
name = input("Enter name: ")
query = "SELECT * FROM STUDENT WHERE Name = %s"
[Link](query, (name,))
for row in [Link]():
print(row)
🔹 Case Study 8: Display Sorted Data
Write a Python program to display records in descending order of marks.
✅ Answer:
[Link]("SELECT * FROM STUDENT ORDER BY Marks DESC")
for row in [Link]():
print(row)
🎯 Viva Questions (Very Important)
👉 Be ready to answer:
What is cursor()?
👉 Used to execute SQL queries
Why commit() is used?
👉 To save changes
Difference between fetchone() and fetchall()
👉 One row vs all rows
⚡ Common Mistakes
❌ Forgetting [Link]()
❌ Not closing connection
❌ Wrong SQL syntax inside Python
❌ Not using %s placeholder