SQL query Q&A
SQL query Q&A
2) Write a SQL query to get the Nth highest salary from the `Employee` table.
SELECT Salary
FROM Employee
ORDER BY Salary
DESC LIMIT N-1, 1;
5) Write a SQL query to find employees who earn more than their managers.
6) Write a SQL query to find all duplicate email addresses in the Person` table.
SELECT Email
FROM Person
GROUP BY Email
HAVING COUNT(Email) > 1;
Groups rows that have the same values in specified columns into summary rows.
A trigger is a set of SQL statements that automatically "fires" when an event occurs in the
database.
A stored procedure is a prepared SQL code that you can save and reuse.
13) Explain all types of window functions? (Mainly rank, row_num, dense_rank, lead & lag)
Generally, performance is similar. Specific scenarios might Favor one over the other
depending on the database system's optimization.
Ensure the accuracy and reliability of data in a database. Types include PRIMARY KEY,
FOREIGN KEY, UNIQUE, NOT NULL, and CHECK.
In SQL, there are several types of clauses that are commonly used to construct queries
and manipulate data. The major clauses include:
SELECT: Used to specify the columns to be retrieved.
FROM: Specifies the tables from which to retrieve data.
WHERE: Filters records that fulfill a specified condition.
GROUP BY: Groups rows sharing a property so that an aggregate function can be applied
to each group.
HAVING: Filters groups according to some condition (works like a WHERE clause but for
groups).
ORDER BY: Specifies the order in which to return the rows.
JOIN: Combines rows from two or more tables based on a related column between them.
LIMIT (or FETCH FIRST in some systems): Specifies the maximum number of records to
return.
UNION: Combines the result-set of two or more SELECT statements.
INTERSECT: Returns the common records from multiple SELECT statements.
EXCEPT: Returns records from the first SELECT statement that aren't output by the
second SELECT statement.
WITH: Often used to define Common Table Expressions (CTEs).
These clauses allow SQL to be very flexible and powerful in querying relational
databases.
Primary Key uniquely identifies each record, whereas Secondary Key is not the primary
means of identifying records but can still identify them.
SELECT MAX(Salary)
FROM Employees
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employees);
Functions must return a value and cannot modify database state. Stored procedures may
or may not return values and can modify the database.
30) Write a SQL query to find the top 3 departments with the highest average salary.
31) Write a SQL query to find the employees who have the same name and work in the same
department.
SELECT Name, DepartmentID
FROM Employees
GROUP BY Name, DepartmentID
HAVING COUNT(*) > 1;
SELECT DepartmentID
FROM Departments
WHERE DepartmentID NOT IN (SELECT DISTINCT DepartmentID FROM Employees);
Create indexes on columns that are frequently used in WHERE clauses, JOIN conditions,
or as part of an ORDER BY.
34) Write a SQL query to find the employees who have worked for more than 5 years.
Subqueries can be used when the output of one query is needed in another. Joins are
used to combine rows from two or more tables.
36) Write a SQL query to find the top 2 products with the highest sales.
37) How do you use stored procedures to improve SQL query performance?
Precompile complex queries and reuse them, encapsulate logic to maintain consistency.
38) Write a SQL query to find the customers who have placed an order but have not made a
payment.
SELECT CustomerID
FROM Orders
WHERE PaymentDone = 'No';
39) Write a SQL query to find the employees who work in the same department as their
manager.
SELECT e.EmployeeID
FROM Employees e
JOIN Departments d ON e.DepartmentID = d.DepartmentID
WHERE e.ManagerID = d.ManagerID;
Utilize OVER() with PARTITION BY, ORDER BY clauses to perform complex, grouped
calculations over a set of rows.
41) Write a SQL query to find the top 3 products with the highest average price.
42) Write a SQL query to find the employees who have not taken any leave in the last 6
months.