SQL – Interview Question & Answers


SQL – Interview Question & Answers

Table Of Contents:

  1. Write A SQL Query To Retrieve All Columns From A Table Named “Employees”.
  2. Write A SQL Query To Retrieve The Names Of Employees Who Earn More Than $50,000 Per Year.
  3. Write A SQL Query To Retrieve The Total Number Of Employees In Each Department.
  4. Write A SQL Query To Retrieve Highest Salary From Salaries Table.
  5. Write A SQL Query To Rederive  Names Of Employees Who Joined In 2021.
  6. Write A SQL Query To Calculate The Average Salary Of Employees.
  7. Write A SQL Query To Retrieve Top 5 Highest Paid Employees.
  8. Write A SQL Query To Find The Number Of Orders Placed By Each Customer.
  9. Write A SQL Query To Retrieve Names Of The Employee Whose Names Starts With ‘J’.
  10. Write A SQL Query To Find Out The Total Sales Amount For Each Month In The Year 2022.
  11. Write A SQL Query To Find The 2nd Highest Salary From An Employee Table.
  12. Write A SQL Query To Find Employees Who Have Joined After The Employee With Employee ID 101.
  13. Write A SQL Query To Calculate The Total Number Of Order Placed By Each Customer.
  14. Write A SQL Query To Find The Top 3 Most Sold Products Along With Their Total Sales Quantity.
  15. Write A SQL Query To Find The Average Salary Of Employees In Each Departments.

(1) Retrieve All Columns From a Table Named “Employees”

SELECT * FROM Employees

(2) Retrieve The Names Of Employees Who Earn More Than $50,000 Per Year.

SELECT names
FROM Employees
WHERE salary > 50000

(3) Retrieve the Total Number of Employees in Each Department

SELECT department_id , COUNT(*) AS total_employees
FROM Employees 
GROUP BY department_id

(4) Retrieve Highest Salary From Salaries Table

SELECT MAX(salary) AS highest_salary
FROM Salaries

(5) Retrieve Names of Employees Who Joined in 2021

SELECT name
FROM Employees
WHERE YEAR(joining_date) = 2021

(6) Calculate the Average Salary of Employees

SELECT AVG(salary) AS average_salary
FROM Employees

(7) Retrieve Top 5 Highest Paid Employees

SELECT * 
FROM Employees
ORDER BY salary DESC
LIMIT 5

(8) Find the Number of Orders Placed By Each Customer

SELECT customer_id,COUNT(*) as total_orders
FROM Orders 
GROUP BY customer_id

(9) Retrieve Names of Employees Whose Names Start With ‘J’

SELECT name 
FROM Employees
WHERE name LIKE 'J'

(10) Find Out the Total Sales Amount for Each Month in the Year 2022

SELECT MONTH(order_date) AS month , SUM(sales_amount) AS total_sales 
FROM Sales 
WHERE YEAR(order_date) = 2022
GROUP BY MONTH(order_date)
ORDER BY month

(11) Find the 2nd Highest Salary From an Employee Table

SELECT MAX(salary) as second_highest_salary
FROM Employees
WHERE salary < (SELECT MAX(salary) FROM Employee)

(12)Find Employees Who Have Joined After the Employee With Employee ID 101

SELECT * 
FROM Employees
WHERE joining_date > (
      SELECT joining_date 
      FROM Employees
      WHERE employee_id = 101)

(13) Calculate the Total Number of Orders Placed By Each Customer

SELECT customer_id , COUNT(*) 
FROM Orders
GROUP BY customer_id

(14)Find the Top 3 Most Sold Products Along With Their Total Sales Quantity.

SELECT product_id, SUM(quantaty) AS total_quantaty
FROM OrderDetails
GROUP BY product_id
ORDER BY total_quantaty DESC
LIMIT 3

(15) Find the Average Salary of Employees in Each Department

SELECT department_id, AVG(salary) 
FROM Employees
GROUP BY department_id

Leave a Reply

Your email address will not be published. Required fields are marked *