1. Write a query to find all employees who earn more than the average salary.2. Write a query to retrieve the names of employees who work in the same department as ‘John’.3. Write a query to display the second highest salary from the Employee table.4. Write a query to find all customers who have made more than five orders.5. Write a query to count the number of orders placed by each customer.6. Write a query to retrieve the list of employees who joined in the last 6 months.7. Write a query to find the total sales amount per product.8. Write a query to list all products that have never been sold.9. Write a query to update the salary of employees based on their performance rating.10. Write a query to delete duplicate rows from a table.11. Write a query to find the second highest salary in a company without using the MAX function twice.12. Write a query to identify the top 10 customers who have not placed an order in the last year.13. Create a query to compute the year-over-year growth rate of revenue for each product category.14. Write a query to join three tables and filter the results to show only records that exist in exactly two of the tables.15. Construct a query to find the median sales amount for each region.16. Write a query to calculate the retention rate of customers over a given time period.17. Write a query to find duplicate records in a table and count the number of duplicates for each unique record.18. Create a query to find employees who have been with the company for more than 5 years but have never been promoted.
Question
- Write a query to find all employees who earn more than the average salary.2. Write a query to retrieve the names of employees who work in the same department as ‘John’.3. Write a query to display the second highest salary from the Employee table.4. Write a query to find all customers who have made more than five orders.5. Write a query to count the number of orders placed by each customer.6. Write a query to retrieve the list of employees who joined in the last 6 months.7. Write a query to find the total sales amount per product.8. Write a query to list all products that have never been sold.9. Write a query to update the salary of employees based on their performance rating.10. Write a query to delete duplicate rows from a table.11. Write a query to find the second highest salary in a company without using the MAX function twice.12. Write a query to identify the top 10 customers who have not placed an order in the last year.13. Create a query to compute the year-over-year growth rate of revenue for each product category.14. Write a query to join three tables and filter the results to show only records that exist in exactly two of the tables.15. Construct a query to find the median sales amount for each region.16. Write a query to calculate the retention rate of customers over a given time period.17. Write a query to find duplicate records in a table and count the number of duplicates for each unique record.18. Create a query to find employees who have been with the company for more than 5 years but have never been promoted.
Solution
Sure, here are the SQL queries for each of your requests:
SELECT * FROM Employees
WHERE Salary > (SELECT AVG(Salary) FROM Employees);
SELECT e.Name FROM Employees e
JOIN Departments d ON e.DepartmentId = d.Id
WHERE d.Name = (SELECT DepartmentId FROM Employees WHERE Name = 'John');
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee);
SELECT CustomerId FROM Orders
GROUP BY CustomerId
HAVING COUNT(OrderId) > 5;
SELECT CustomerId, COUNT(OrderId) FROM Orders
GROUP BY CustomerId;
SELECT * FROM Employees
WHERE JoiningDate > DATE_SUB(CURDATE(), INTERVAL 6 MONTH);
SELECT ProductId, SUM(SalesAmount) FROM Sales
GROUP BY ProductId;
SELECT * FROM Products
WHERE ProductId NOT IN (SELECT DISTINCT ProductId FROM Sales);
UPDATE Employees
SET Salary = CASE
WHEN PerformanceRating = 'Excellent' THEN Salary * 1.1
WHEN PerformanceRating = 'Good' THEN Salary * 1.05
ELSE Salary
END;
DELETE FROM table
WHERE rowid NOT IN
(SELECT MIN(rowid) FROM table GROUP BY column);
SELECT MAX(Salary) FROM Employee
WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee);
SELECT CustomerId FROM Customers
WHERE CustomerId NOT IN
(SELECT DISTINCT CustomerId FROM Orders WHERE OrderDate > DATE_SUB(CURDATE(), INTERVAL 1 YEAR))
LIMIT 10;
SELECT ProductCategory,
(SUM(Revenue) - LAG(SUM(Revenue), 1) OVER (PARTITION BY ProductCategory ORDER BY Year)) / LAG(SUM(Revenue), 1) OVER (PARTITION BY ProductCategory ORDER BY Year) AS YoY_Growth
FROM Sales
GROUP BY Year, ProductCategory;
SELECT * FROM Table1
JOIN Table2 ON Table1.Id = Table2.Id
JOIN Table3 ON Table1.Id = Table3.Id
WHERE (Table1.Id IS NULL AND Table2.Id IS NOT NULL AND Table3.Id IS NOT NULL)
OR (Table1.Id IS NOT NULL AND Table2.Id IS NULL AND Table3.Id IS NOT NULL)
OR (Table1.Id IS NOT NULL AND Table2.Id IS NOT NULL AND Table3.Id IS NULL);
SELECT Region, AVG(SalesAmount) AS MedianSales
FROM (SELECT Region, SalesAmount, COUNT(*) OVER (PARTITION BY Region) AS Total, ROW_NUMBER() OVER (PARTITION BY Region ORDER BY SalesAmount) AS Rank
FROM Sales) sub
WHERE Rank = Total / 2 OR Rank = Total / 2 + 1
GROUP BY Region;
SELECT COUNT(DISTINCT CustomerId) / (SELECT COUNT(DISTINCT CustomerId) FROM Customers WHERE JoinDate < DATE_SUB(CURDATE(), INTERVAL period YEAR)) AS RetentionRate
FROM Orders
WHERE OrderDate > DATE_SUB(CURDATE(), INTERVAL period YEAR);
SELECT column, COUNT(*)
FROM table
GROUP BY column
HAVING COUNT(*) > 1;
SELECT * FROM Employees
WHERE JoinDate < DATE_SUB(CURDATE(), INTERVAL 5 YEAR)
AND EmployeeId NOT IN (SELECT DISTINCT EmployeeId FROM Promotions);
Please replace table, column, period, Employee, Employees, Departments, Orders, Sales, Products, Customers, Promotions with your actual table names and Salary, Name, DepartmentId, JoiningDate, PerformanceRating, ProductId, SalesAmount, CustomerId, OrderId, OrderDate, ProductCategory, Revenue, Year, Region, JoinDate, EmployeeId with your actual column names.
Similar Questions
Write SQL QueriesEmployee TableDepartment Table1. Get the list of employees whose salary is more than 20K and are in HR department.2. Get the salary being paid to each Department.3. Get the employees with the third highest salary.4. Name and designation of the employees who don't have any manager
Write a query to display the ename of employees working in 'HR' department and who draw an annual salary greater than 1000000. [Consider employees who work in multiple departments too]. Display the records sorted in ascending order based on employee ename.Note:1. Display all the ename of employees working in 'HR' department and having annual salary greater than 1000000.2. Use Joins. Tables involved are employee, works and department3. Order by is mandatory.
Write an SQL query to find the employees who earn more than their managers.
From the given employee table, write a query to retrieve the total salary and count of employees who have the highest total salary, where the total salary is calculated as the product of months worked and the salary per month.Table: Employee
To display the names of employees who earns more than the average salary of allemployees.SELECT last_name, first_nameFROMemployeeWHEREsalary > AVG(salary);Which change should you make to achieve the desired results?Select one:a.Use a subquery in the WHERE clause to compare the average salary value.b.Change the function in the WHERE clause.c.Move the function to the SELECT clause and add a GROUP BY clause.d.Move the function to the SELECT clause and add a GROUP BY clause and a HAVING clause.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.