SQL Assignment

idnamedepartmentmanager_idsalarycitycreated_atupdated_at
1Amit SharmaSalesNULL75000Mumbai2023-01-102023-06-01
2Priya PatelMarketing168000Ahmedabad2023-02-152023-05-18
3Rahul SinghIT192000Bengaluru2023-03-052023-04-22
4Neha GuptaHR261000Delhi2023-04-202023-05-30
5Karan VermaIT388000Pune2023-05-092023-06-10
6Sneha RoySales174000Kolkata2023-02-112023-05-25
7Rohan MehtaMarketing270000Mumbai2023-03-222023-05-17
8Anjali RaoIT393000Hyderabad2023-04-152023-06-05
table: employees

BASIC SELECT (FOUNDATION)

1. Display all employees in descending order of salary.


2. Display all employees who belong to Mumbai city.
(Assume city is stored inside department or via mapping — students must clarify)


3. Find all employees whose salary is more than the average salary.


4. Display the total sum of salary of all employees.


5. Display all unique employee addresses.
(If address column is not available, students must modify table)


6. Display a column called details that shows:
name + address concatenated, along with salary.


INTERMEDIATE AGGREGATION & FILTERING

7. Display the name(s) of employee(s) having the maximum salary.


8. Find the employee(s) having the second highest salary.


9. Display the count of employees by address, ordered by employee count.


10. Show the total number of employees from Nagpur only.


11. Display employees whose names start OR end with a vowel (a,e,i,o,u).


12. Find the employee(s) having maximum salary in each city.


13. Display the top 5 highest salaried employees.


14. Display the second highest salaried employee using a subquery.


15. Show the list of cities where total salary is more than 200000.


JOINS BASED ASSIGNMENTS (STUDENT SYSTEM)

idnamecity
1Rahul KumarPatna
2Meera NairChennai
3Ayesha KhanLucknow
4Arjun DasKolkata
5Sanya SinghJaipur
table: students

idname
1Mathematics
2Science
3English
4Computer Science
table: subject_master

idstudent_iddepartment_idmarks
11185
21478
32282
42390
53175
63380
74495
85288
table: result

16. Display all students who have appeared for the exam.
(Students who exist in result table)


17. Display:
Student Name, Subject (Department Name), and Marks

Using:

  • students
  • departments
  • result

18. Display Student Name and Total Marks only for students whose
total marks are greater than 80.


TRIGGERS & EVENTS (ADVANCED)

19. (Trigger Assignment)

Create a trigger such that:

Whenever an employee is deleted, their data is automatically inserted into deleted_employees table.


20. (Event Scheduler Assignment)

Create a MySQL event that:

Automatically deletes audit logs older than 5 minutes, and
Runs every 2 minutes.


CHALLENGE

  • Find employees who do not have a manager
  • Find departments having more than 5 employees
  • Show manager name with employee name (self join)

TABLES

🔹 employee

id, name, department, manager_id, salary, created_at, updated_at

🔹 students

id, name

🔹 department

id, name

🔹 result

id, student_id, department_id, marks

🔹 auditlog

id, username, activity, created_at