1.
Retrieve the first 10 rows from a table:
SELECT * FROM table_name LIMIT 10;
2. Find the second highest salary from an Employee table:
SELECT MAX(salary) FROM Employee WHERE salary < (SELECT MAX(salary) FROM Employee);
3. List employees who joined in the last month:
SELECT * FROM Employee WHERE JOIN_DATE >= DATEADD(MONTH, -1, GETDATE());
4. Count the number of employees in each department:
SELECT department, COUNT(*) AS num_employees
FROM Employee
GROUP BY department;
5. Retrieve employees who have the highest salary in each department:
SELECT department, MAX(salary) AS max_salary
FROM Employee
GROUP BY department;
6. Find the nth highest salary from an Employee table:
SELECT DISTINCT salary FROM Employee ORDER BY salary DESC LIMIT n-1, 1;
7. List employees who don't have a manager:
SELECT * FROM Employee WHERE manager_id IS NULL;
8. Retrieve employees who earn more than their managers:
SELECT e.*
FROM Employee e
JOIN Employee m ON e.manager_id = m.employee_id
WHERE e.salary > m.salary;
9. Find duplicate records in a table:
SELECT column1, column2, COUNT(*)
FROM table_name
GROUP BY column1, column2
HAVING COUNT(*) > 1;
10. Calculate the total sales for each product:
SELECT product_id, SUM(quantity * price) AS total_sales
FROM Sales
GROUP BY product_id;
11. Retrieve the top 5 customers with the highest total purchase amount:
SELECT customer_id, SUM(amount) AS total_purchase_amount
FROM Orders
GROUP BY customer_id
ORDER BY total_purchase_amount DESC
LIMIT 5;
12. List the names of all customers who have made at least 3 orders:
SELECT customer_name
FROM Customers
WHERE customer_id IN (
SELECT customer_id
FROM Orders
GROUP BY customer_id
HAVING COUNT(*) >= 3
);
13. Calculate the average salary of employees in each department:
SELECT department, AVG(salary) AS average_salary
FROM Employee
GROUP BY department;
14. Find the most common word in a text column:
SELECT word, COUNT(*) AS word_count
FROM (
SELECT regexp_split_to_table(text_column, '\s+') AS word
FROM table_name
) AS words
GROUP BY word
ORDER BY word_count DESC
LIMIT 1;
15. Retrieve the oldest and newest employees in each department:
SELECT
department,
MIN(hire_date) AS oldest_employee_hire_date,
MAX(hire_date) AS newest_employee_hire_date
FROM
Employee
GROUP BY
department;
16. List the top 3 best-selling products in each category:
SELECT category, product_name, total_sales
FROM (
SELECT category, product_name, SUM(quantity_sold) AS total_sales,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY SUM(quantity_sold) DESC) AS rank
FROM Products
GROUP BY category, product_name
) AS ranked_products
WHERE rank <= 3;
17. Calculate the percentage of total sales contributed by each product:
SELECT product_id, (SUM(sales_amount) / (SELECT SUM(sales_amount) FROM Sales)) *
100 AS sales_percentage
FROM Sales
GROUP BY product_id;
18. Find employees who have the same salary as their colleagues:
SELECT
e1.employee_id,
e1.employee_name,
e1.salary
FROM
Employee e1
JOIN
Employee e2 ON e1.salary = e2.salary
AND e1.employee_id <> e2.employee_id
AND e1.department = e2.department
WHERE
e1.employee_id < e2.employee_id;
19. List the employees who have been with the company for more than 5 years:
SELECT * FROM Employee WHERE DATEDIFF(YEAR, join_date, GETDATE()) > 5;
20. Retrieve orders that have not been shipped yet:
SELECT * FROM Orders WHERE ship_date IS NULL;
21. Calculate the total number of orders placed each month:
SELECT EXTRACT(MONTH FROM order_date) AS month, COUNT(*) AS num_orders
FROM Orders
GROUP BY EXTRACT(MONTH FROM order_date);
22. Find the customer who has placed the highest number of orders:
SELECT customer_id, COUNT(*) AS num_orders
FROM Orders
GROUP BY customer_id
ORDER BY num_orders DESC
LIMIT 1;
23. Retrieve the top 10% of highest-paid employees:
SELECT *
FROM Employee
ORDER BY salary DESC
LIMIT (SELECT COUNT(*) * 0.1 FROM Employee);
24. List employees who have the same manager:
SELECT e1.employee_id, e1.employee_name, e1.manager_id
FROM Employee e1
JOIN Employee e2 ON e1.manager_id = e2.manager_id AND e1.employee_id <>
e2.employee_id;
25. Calculate the running total of sales for each month:
SELECT order_date, SUM(amount) OVER (ORDER BY order_date) AS running_total
FROM Orders;
26. Retrieve the latest order placed by each customer:
SELECT DISTINCT ON (customer_id) *
FROM Orders
ORDER BY customer_id, order_date DESC;
27. Find customers who have never placed an order:
SELECT *
FROM Customers
WHERE customer_id NOT IN (SELECT DISTINCT customer_id FROM Orders);
28. List the products that have never been sold:
SELECT *
FROM Products
WHERE product_id NOT IN (SELECT DISTINCT product_id FROM Sales);
29. Retrieve the average time taken to ship orders for each shipping method:
SELECT shipping_method, AVG(DATEDIFF(DAY, order_date, ship_date)) AS
avg_shipping_time
FROM Orders
GROUP BY shipping_method;
30. Find the total number of unique customers who made purchases in each year:
SELECT EXTRACT(YEAR FROM order_date) AS year, COUNT(DISTINCT customer_id) AS
num_customers
FROM Orders
GROUP BY EXTRACT(YEAR FROM order_date);
31. Retrieve the top 3 highest-paid employees in each department:
SELECT department, employee_name, salary
FROM (
SELECT department, employee_name, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank
FROM Employee
) AS ranked_employees
WHERE rank <= 3;
32. Find the average salary difference between employees and their managers:
SELECT AVG(e.salary - m.salary) AS avg_salary_difference
FROM Employee e
JOIN Employee m ON e.manager_id = m.employee_id;
33. List customers who have spent more than the average total amount spent by all
customers:
SELECT customer_id, SUM(amount) AS total_spent
FROM Orders
GROUP BY customer_id
HAVING SUM(amount) > (SELECT AVG(total_amount) FROM (SELECT SUM(amount) AS
total_amount FROM Orders GROUP BY customer_id) AS avg_amount);
34. Retrieve the top 5 categories with the highest average sales amount:
SELECT category, AVG(amount) AS avg_sales_amount
FROM Products
GROUP BY category
ORDER BY avg_sales_amount DESC
LIMIT 5;
35. Calculate the median salary of employees:
SELECT AVG(salary) AS median_salary
FROM (
SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) AS row_num,
COUNT(*) OVER () AS total_rows
FROM Employee
) AS salary_data
WHERE row_num IN ((total_rows + 1) / 2, (total_rows + 2) / 2);
36. Find the employees who have never been managers:
SELECT *
FROM Employee
WHERE employee_id NOT IN (SELECT DISTINCT manager_id FROM Employee WHERE manager_id
IS NOT NULL);
37. Retrieve the top 3 most recent orders for each customer:
SELECT customer_id, order_id, order_date
FROM (
SELECT customer_id, order_id, order_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date DESC) AS rank
FROM Orders
) AS ranked_orders
WHERE rank <= 3;
38. List the customers who have placed orders in all categories:
SELECT customer_id
FROM Orders
GROUP BY customer_id
HAVING COUNT(DISTINCT category_id) = (SELECT COUNT(DISTINCT category_id) FROM
Products);
39. Calculate the percentage of null values in each column of a table:
SELECT column_name,
(COUNT(*) - COUNT(column_name)) / COUNT(*) * 100 AS null_percentage
FROM table_name
GROUP BY column_name;
40. Find the products that have been sold every month for the past year:
SELECT product_id
FROM Sales
GROUP BY product_id
HAVING COUNT(DISTINCT EXTRACT(MONTH FROM sale_date)) = 12;