SQL Practice Questions - From Beginner to Pro
1. Basic SQL (Beginner Level)
1 Create a table called Employees with columns: id, name, department, salary, hire_date.
2 Insert at least 10 records into the Employees table.
3 Select all employees from the Employees table.
4 Retrieve only the name and salary of employees.
5 Find all employees working in the IT department.
6 Get employees hired after 2020-01-01.
7 Find employees with a salary greater than 50,000.
8 Retrieve employees whose names start with A.
9 Count the total number of employees.
10 Find the maximum, minimum, and average salary.
2. Filtering & Sorting
1 List all employees ordered by salary descending.
2 Retrieve the top 5 highest-paid employees.
3 Find employees not in the HR department.
4 Get employees whose salary is between 30,000 and 60,000.
5 Retrieve employees who joined in 2022.
6 Display distinct departments from the table.
7 Find employees whose name contains 'an'.
8 Sort employees by hire_date and salary.
3. Aggregate Functions & GROUP BY
1 Count employees in each department.
2 Find the average salary in each department.
3 Get the highest salary in each department.
4 Find departments with more than 5 employees.
5 Show total salary expenditure per department.
6 List departments where the average salary is above 60,000.
4. Joins (2+ Tables)
1 Join Employees with Departments to show employee names and department names.
2 Show employees who are assigned to projects.
3 List employees who are not assigned to any project.
4 Find employees and their project names.
5 Count how many employees are in each department.
6 Get employees working in the 'Finance' department.
5. Subqueries
1 Find employees earning more than the average salary.
2 Retrieve employees with the maximum salary.
3 Find employees whose department has more than 10 employees.
4 Show employees who earn the second-highest salary.
5 List employees who do not have any project (using subquery).
6. Advanced Joins & Set Operations
1 Get employees who are assigned to more than one project.
2 Find departments with no employees.
3 List employees who are in IT but not in HR.
4 Show employees who are in both Project A and Project B.
5 Combine employee lists from two tables using UNION.
7. Window Functions (Analytic Queries)
1 Rank employees by salary within each department.
2 Find the top 3 highest-paid employees in each department.
3 Calculate the running total of salaries by hire date.
4 Show each employee’s salary compared to the department average.
5 Find the difference between each employee’s salary and the previous employee’s salary.
8. Constraints & Data Definition
1 Create a table with PRIMARY KEY, FOREIGN KEY, and UNIQUE constraints.
2 Add a NOT NULL constraint to the salary column.
3 Drop the hire_date column from Employees.
4 Rename the Employees table to Staff.
5 Add a new column bonus with default value 0.
9. Stored Procedures & Functions
1 Write a stored procedure to increase salary by 10% for IT employees.
2 Create a function to calculate annual salary (salary × 12).
3 Write a procedure to insert a new employee safely.
4 Create a trigger to log whenever a new employee is inserted.
5 Write a procedure to delete employees hired before 2015.
10. Real-World Problem Solving (Pro Level)
1 Find employees who earn the same salary (duplicates).
2 Find the highest paid employee in each department.
3 Write a query to find the median salary of employees.
4 Show the top 3 earning employees per project.
5 Find employees who joined in the last 6 months.
6 Write a query to pivot data: show departments as columns with total salaries.
7 Find employees with gaps in their project assignments.
8 Write a query to detect duplicate employee names.
9 Find employees who do not have a manager (self join).
10 Generate a report of employees with department, project count, and total salary.