SQL Questions Guide for QA s 1738694053
SQL Questions Guide for QA s 1738694053
By-
REEHAN NIZAMI SHAZADA
Introduction
This document provides detailed explanations and SQL queries for commonly asked questions in QA
interviews. These queries help in data validation, database testing, and automation scripts for
QA professionals.
FROM employees
Explanation:
SELECT name
FROM employees
WHERE department_id = (
Explanation:
FROM employees
Explanation:
FROM orders
GROUP BY customer_id
Explanation:
FROM orders
GROUP BY customer_id;
Explanation:
Explanation:
FROM orders
GROUP BY product_id;
Explanation:
FROM products p
Explanation:
WHERE id NOT IN (
);
Explanation:
FROM customers c
LIMIT 10;
Explanation:
FROM employees e1
WHERE salary = (SELECT MAX(salary) FROM employees e2 WHERE e1.department =
e2.department);
Explanation:
FROM employees e1
Explanation:
FROM customers c
Explanation:
Here's a super detailed guide explaining each SQL query step by step, as if we're explaining to
someone completely new to SQL. 🎯
📌 1. Find Employees with Salaries Higher than
the Company’s Average
📝 Question:
Write a query to find all employees whose salaries exceed the company's average salary.
🧠 Explanation:
Imagine a classroom where we calculate the average score of all students. Now, we want to list all
students whose scores are above the class average.
👨💻 Query:
SELECT *
FROM employees
🔍 Breakdown:
AVG(salary) → Finds the average salary of all employees.
SELECT * FROM employees → Selects all employees.
WHERE salary > (SELECT AVG(salary) FROM employees) → Filters only those with higher than
average salaries.
📌 2. Find Employees in the Same Department
as ‘John Doe’
📝 Question:
Write a query to retrieve the names of employees who work in the same department as 'John Doe'.
🧠 Explanation:
Imagine a school where students belong to different classes. If we want to find who else is in the
same class as ‘John Doe’, we need:
FROM employees
🔍 Breakdown:
SELECT department_id FROM employees WHERE name = 'John Doe' → Finds John’s
department.
WHERE department_id = (...) → Finds everyone in that department.
AND name <> 'John Doe' → Excludes John Doe from the list.
👨💻 Query:
SELECT MAX(salary)
FROM employees
🔍 Breakdown:
MAX(salary) FROM employees → Finds the highest salary.
WHERE salary < (...) → Picks the second-highest salary.
🧠 Explanation:
Think of a coffee shop loyalty card ☕. If a customer buys coffee more than 5 times, they get a free
one! We need:
👨💻 Query:
SELECT customer_id
FROM orders
GROUP BY customer_id
🔍 Breakdown:
GROUP BY customer_id → Groups orders by customer.
COUNT(order_id) → Counts how many orders each customer placed.
HAVING COUNT(order_id) > 5 → Selects only customers with more than 5 orders.
🧠 Explanation:
Imagine a bakery 🥖 where customers order multiple times. We need:
1. Count how many times each customer ordered.
👨💻 Query:
SELECT customer_id, COUNT(order_id) AS total_orders
FROM orders
GROUP BY customer_id;
🔍 Breakdown:
COUNT(order_id) → Counts the total orders per customer.
GROUP BY customer_id → Groups by customer.
FROM employees
🔍 Breakdown:
CURDATE() → Gets today’s date.
DATE_SUB(..., INTERVAL 6 MONTH) → Subtracts 6 months from today.
WHERE join_date >= ... → Picks employees who joined after that date.
👨💻 Query:
SELECT product_id, SUM(quantity * price) AS total_sales
FROM orders
GROUP BY product_id;
🔍 Breakdown:
SUM(quantity * price) → Adds up the total sales for each product.
GROUP BY product_id → Groups by product.
👨💻 Query:
SELECT *
FROM products
WHERE product_id NOT IN (SELECT DISTINCT product_id FROM orders);
🔍 Breakdown:
SELECT DISTINCT product_id FROM orders → Gets a list of sold products.
NOT IN (...) → Selects products that are not in that list.
👨💻 Query:
DELETE FROM employees
WHERE id NOT IN (
);
🔍 Breakdown:
GROUP BY name, department, salary → Groups duplicates.
MIN(id) → Keeps only the first occurrence.
DELETE WHERE id NOT IN (...) → Removes duplicates.
👨💻 Query:
SELECT customer_id
FROM customers
LIMIT 10;
🔍 Breakdown:
DATE_SUB(CURDATE(), INTERVAL 1 YEAR) → One year ago.
WHERE order_date >= ... → Finds customers who ordered in the past year.
NOT IN (...) → Excludes those who ordered.
LIMIT 10 → Gets only the top 10 customers.
👨💻 Query:
SELECT DISTINCT salary
FROM employees e1
🔍 Breakdown:
The highest salary has 0 salaries greater than it.
The second highest has 1 salary greater than it.
The third highest has 2 salaries greater than it.
COUNT(DISTINCT salary) = 2 → Finds the third highest salary.
👨💻 Query:
SELECT e.name AS employee, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employees e
🔍 Breakdown:
JOIN employees m ON e.manager_id = m.id → Joins employees with their managers.
WHERE e.salary > m.salary → Filters employees earning more than their manager.
👨💻 Query:
SELECT * FROM employees WHERE name LIKE 'A%';
🔍 Breakdown:
LIKE 'A%' → Finds names starting with ‘A’.
👨💻 Query:
SELECT customer_id
FROM orders
);
🔍 Breakdown:
Finds all customers who ordered.
Excludes those who bought Product X.
👨💻 Query:
SELECT product_id, COUNT(*) AS total_purchases
FROM orders
GROUP BY product_id
LIMIT 1;
🔍 Breakdown:
COUNT(*) → Counts how many times each product was purchased.
ORDER BY total_purchases DESC → Sorts by highest count.
LIMIT 1 → Picks the most popular product.
FROM employees
WHERE salary IN (SELECT salary FROM employees GROUP BY salary HAVING COUNT(*) >
1);
🔍 Breakdown:
Groups salaries and finds duplicates (HAVING COUNT(*) > 1).
Filters employees with those duplicate salaries.
👨💻 Query:
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id
🔍 Breakdown:
GROUP BY department_id → Groups employees by department.
HAVING COUNT(*) > 10 → Filters departments with more than 10 employees.
FROM employee_department
GROUP BY employee_id
🔍 Breakdown:
Groups by employee.
Filters employees assigned to more than 1 department.
👨💻 Query:
SELECT *
FROM customers
🔍 Breakdown:
Gets a list of all customers.
Excludes those who placed an order.
👨💻 Query:
SELECT department_id, name, age
FROM employees e1
🔍 Breakdown:
Finds the max age for each department.
Matches it with employee records.
👨💻 Query:
SELECT *
FROM employees
WHERE salary > (SELECT salary FROM employees ORDER BY salary LIMIT 1 OFFSET
(SELECT COUNT(*)/2 FROM employees));
🔍 Breakdown:
ORDER BY salary LIMIT 1 OFFSET (COUNT(*)/2) → Finds the median salary.
Filters employees above the median salary.
🔍 Breakdown:
The * means "select everything" from the table.
Simple! This is the most basic SQL query.
👨💻 Query:
SELECT name, salary FROM employees;
🔍 Breakdown:
Instead of *, we list specific column names.
👨💻 Query:
SELECT * FROM employees WHERE salary > 50000;
🔍 Breakdown:
WHERE filters results based on a condition.
salary > 50000 means only show employees earning more than 50,000.
👨💻 Query:
SELECT * FROM employees WHERE department = 'HR' AND salary > 40000;
🔍 Breakdown:
AND means both conditions must be true.
Use OR if either condition can be true.
👨💻 Query:
SELECT * FROM employees ORDER BY salary DESC;
🔍 Breakdown:
ORDER BY salary → Sorts results by salary.
DESC → Highest salary first (Descending).
Use ASC for lowest salary first.
📌 6. Limit Results
📝 Question:
Write a query to find the top 5 highest-paid employees.
👨💻 Query:
SELECT * FROM employees ORDER BY salary DESC LIMIT 5;
🔍 Breakdown:
LIMIT 5 → Shows only the first 5 results.
👨💻 Query:
SELECT COUNT(*) FROM employees;
🔍 Breakdown:
COUNT(*) counts all rows in the table.
👨💻 Query:
SELECT AVG(salary) FROM employees;
🔍 Breakdown:
AVG(salary) → Calculates average salary.
👨💻 Query:
SELECT MAX(salary) AS highest_salary, MIN(salary) AS lowest_salary FROM employees;
🔍 Breakdown:
MAX(salary) → Finds the highest salary.
MIN(salary) → Finds the lowest salary.
👨💻 Query:
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;
🔍 Breakdown:
GROUP BY department → Groups employees by department.
COUNT(*) → Counts employees in each department.
👨💻 Query:
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
🔍 Breakdown:
HAVING works like WHERE but is used with GROUP BY.
It filters groups based on a condition.
👨💻 Query:
SELECT name, COUNT(*)
FROM employees
GROUP BY name
🔍 Breakdown:
Groups by name and counts occurrences.
HAVING COUNT(*) > 1 → Shows only duplicates.
👨💻 Query:
DELETE FROM employees WHERE salary < 30000;
🔍 Breakdown:
DELETE removes rows that match the condition.
👨💻 Query:
UPDATE employees
🔍 Breakdown:
UPDATE modifies data.
SET changes the salary.
WHERE ensures only IT employees are updated.
👨💻 Query:
SELECT * FROM employees WHERE manager_id IS NULL;
🔍 Breakdown:
IS NULL finds missing values.
👨💻 Query:
SELECT DISTINCT customers.name
FROM customers
🔍 Breakdown:
JOIN combines both tables.
DISTINCT removes duplicate names.
👨💻 Query:
SELECT e.name AS employee, m.name AS manager
FROM employees e
🔍 Breakdown:
LEFT JOIN → Matches employees with their manager.
e.name AS employee, m.name AS manager → Renames columns.
Conclusion
These SQL queries are essential for Software QA roles to validate databases, test automation
results, and verify data consistency. Mastering these will help in database testing and automation
frameworks.