SQL Question Bank
I've compiled all the SQL questions I found on social media, such as LinkedIn, YouTube, Instagram, and Telegram. Let's dive into the database world.
Top Interview Questions SQL
Q1: Explain the difference between RANK(), DENSE_RANK(), and ROW_NUMBER() functions using example.
Q2 – Q4 use Table: employee (EmpID, ManagerID, JoinDate, Dept, Salary)
Q2: Find the nth highest salary from the Employee table.
Q3: You have an employee table with employee ID and manager ID. Find all employees under a specific manager, including their subordinates at any level.
Q4: Write a query to find the cumulative salary of employees department-wise, who have joined the company in the last 30 days.
Q5: Find the top 2 customers with the highest order amount for each product category, handling ties appropriately. Table: Customer (CustomerID, ProductCategory, OrderAmount)
[1] Top N Analysis: Find top N products by sales, top N employees by salaries, or top N items within each category.
[2] Year-Over-Year Growth: Calculate YOY growth, products with higher sales than the previous month, or category-wise growth.
[3] Running Totals: Track running sales over months, rolling N months' sales, especially within categories.
[4] Pivot Operations: Transform rows to columns, such as displaying year-wise sales for each category in separate columns.
[5] Join Analysis: Count the records resulting from different types of joins.
3. Can you explain the differences between UNION and UNION ALL?
4. How do you write a SQL query to find the second highest salary in a table?
5. What are CTEs (Common Table Expressions) and their benefits?
2- Write a SQL query to return the total number of transactions per user for each day. This could involve using GROUP BY and COUNT() functions. This is common in data aggregation tasks.
3- Create a query that selects projects with the highest budget-per-employee ratio from two related tables (projects and employees). This tests your ability to handle complex joins and aggregations.
2. Write a SQL query to find all employees who earn more than their managers.
3. Find the duplicate rows in a table without using GROUP BY.
4. Write a SQL query to find the top 10% of earners in a table.
5. Find the cumulative sum of a column in a table.
6. Write a SQL query to find all employees who have never taken a leave.
7. Find the difference between the current row and the next row in a table.
8. Write a SQL query to find all departments with more than one employee.
9. Find the maximum value of a column for each group without using GROUP BY.
10. Write a SQL query to find all employees who have taken more than 3 leaves in a month.
These questions are designed to test your SQL skills, including your ability to write efficient queries, think creatively, and solve complex problems.
Here are the answers to these questions:
1. SELECT MAX(salary) FROM table WHERE salary NOT IN (SELECT MAX(salary) FROM table)
2. SELECT e1.* FROM employees e1 JOIN employees e2 ON e1.manager_id = (link unavailable) WHERE e1.salary > e2.salary
3. SELECT * FROM table WHERE rowid IN (SELECT rowid FROM table GROUP BY column HAVING COUNT(*) > 1)
4. SELECT * FROM table WHERE salary > (SELECT PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY salary) FROM table)
5. SELECT column, SUM(column) OVER (ORDER BY rowid) FROM table
6. SELECT * FROM employees WHERE id NOT IN (SELECT employee_id FROM leaves)
7. SELECT *, column - LEAD(column) OVER (ORDER BY rowid) FROM table
8. SELECT department FROM employees GROUP BY department HAVING COUNT(*) > 1
9. SELECT MAX(column) FROM table WHERE column NOT IN (SELECT MAX(column) FROM table GROUP BY group_column)
Comments
Post a Comment