SQL Questions & Answers
SQL Coding Questions & Answers
Q1: Retrieve all employees’ first and last names
SELECT first_name, last_name
FROM employees;
Q2: Retrieve employees with salary greater than 50000
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 50000;Q3: Retrieve employees in Sales or IT department
SELECT first_name, last_name, department_id
FROM employees
WHERE department_id IN (1, 2); -- assuming 1=Sales, 2=ITQ4: Count the total number of employees
SELECT COUNT(*) AS total_employees
FROM employees;Q5: List employees ordered by salary descending
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC;Q6: Find the average salary per department
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id;Q7: Find employees earning above the average salary
SELECT first_name, last_name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);Q8: Top 3 highest paid employees
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;Q9: Count employees per department with more than 5 employees
SELECT department_id, COUNT(*) AS num_employees
FROM employees
GROUP BY department_id
HAVING COUNT(*) > 5;Q10: Join employees with departments to get department names
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;Q11: Rank employees by salary within their department
SELECT first_name, last_name, department_id, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS dept_rank
FROM employees;Q12: Find employees whose salary is higher than their manager’s salary
SELECT e.first_name, e.last_name, e.salary, m.first_name AS manager_name, m.salary AS manager_salary
FROM employees e
JOIN employees m
ON e.manager_id = m.employee_id
WHERE e.salary > m.salary;Q13: Retrieve employees with the second highest salary
SELECT *
FROM employees
WHERE salary = (
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary) FROM employees
)
);Q14: Cumulative salary per department
SELECT first_name, last_name, department_id, salary,
SUM(salary) OVER (PARTITION BY department_id ORDER BY salary) AS cumulative_salary
FROM employees;Q15: Find employees who do not have a department
SELECT first_name, last_name
FROM employees
WHERE department_id IS NULL;Q16: List departments with total salary expenditure
SELECT d.department_name, SUM(e.salary) AS total_salary
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
GROUP BY d.department_name;Q17: Find employees whose first name starts with ‘B’
SELECT first_name, last_name
FROM employees
WHERE first_name LIKE 'B%';Q18: Delete all employees with salary less than 30000
DELETE FROM employees
WHERE salary < 30000;Q19: Update salaries by 10% for IT department
UPDATE employees
SET salary = salary * 1.10
WHERE department_id = 2; -- assuming 2=ITQ20: Create a view for high earners
CREATE VIEW HighEarners AS
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 70000;
Comments
Post a Comment