MySQL Assignment-2
Basic Questions
- Write SQL to insert a new student record into a students table with fields (id, name, age).
- Write SQL to retrieve all columns from a table named employees.
- Write SQL to update the salary of an employee with id = 5 in the employees table.
- Write SQL to delete a record of a customer with id = 10 from the customers table.
- Write SQL to select only name and salary columns froam employees.
- Write SQL to filter employees with a salary greater than 50000.
- Write SQL to select employees whose age is between 25 and 40.
- Write SQL to find all products where the price is not NULL.
- Write SQL to sort employees by salary in descending order.
- Write SQL to limit results to the first 10 rows from the students table.
- Write SQL to skip the first 5 rows and then fetch the next 10 rows from employees.
- Write SQL to count the total number of employees.
- Write SQL to find the minimum salary from the employees table.
- Write SQL to calculate the average price of all products.
- Write SQL to find the sum of all salaries in the employees table.
- Write SQL to group employees by department and count how many employees each department has.
- Write SQL to filter grouped departments where total salary > 1,00,000.
- Write SQL to insert multiple rows in a departments table using one statement.
- Write SQL to select distinct city names from the customers table.
- Write SQL to fetch employees whose name starts with ‘A’.
Intermediate Questions
- Write SQL to fetch employees with salary greater than 50000 AND working in department IT.
- Write SQL to fetch employees who are NOT in department HR.
- Write SQL to select customers who live in either Delhi OR Mumbai.
- Write SQL to retrieve all employees who do not have a manager (manager_id IS NULL).
- Write SQL to sort customers first by city ascending and then by name descending.
- Write SQL to find the top 3 highest salaries from employees.
- Write SQL to group products by category and find the average price per category.
- Write SQL to count the number of orders placed by each customer.
- Write SQL to find employees whose salary is greater than the average salary of all employees.
- Write SQL to join employees and departments tables and show employee names with department names.
- Write SQL to retrieve all employees even if they are not assigned to any department (LEFT JOIN).
- Write SQL to retrieve all departments even if no employee belongs to them (RIGHT JOIN).
- Write SQL to retrieve all employees and all departments regardless of match (FULL OUTER JOIN).
- Write SQL to perform a self-join on employees to find employees and their managers.
- Write SQL to perform a cross join between products and categories.
- Write SQL to use a subquery in WHERE clause: find employees who earn more than the average salary.
- Write SQL to use a subquery in FROM clause: calculate the max salary from a derived table of departments.
- Write SQL to use a subquery in SELECT clause: display employee name and their department’s average salary.
- Write SQL using UNION to combine names from employees and customers.
- Write SQL using UNION ALL to combine names from employees and customers including duplicates.
Advanced Questions
- Write SQL using INTERSECT to find common names in employees and customers tables.
- Write SQL using EXCEPT to find employee names that are not present in customers.
- Write a correlated subquery to find employees whose salary is greater than the salary of any employee in HR department.
- Write a nested subquery to find the department name of the employee with the highest salary.
- Write SQL to rank employees by salary using ROW_NUMBER().
- Write SQL to rank employees by salary within each department using RANK().
- Write SQL to assign dense ranks to employees by salary across all departments using DENSE_RANK().
- Write SQL to calculate a running total of employee salaries using SUM() OVER (ORDER BY salary).
- Write SQL to calculate the average salary partitioned by department using window functions.
- Write SQL to fetch the top 2 highest-paid employees from each department using window functions.