SQL Question
SQL Question
Write an SQL query to fetch “FIRST_NAME” from Worker table using the alias name as
<WORKER_NAME>
Q-2. Write an SQL query to fetch “FIRST_NAME” from Worker table in upper case.
Q-3. Write an SQL query to fetch unique values of DEPARTMENT from Worker table.
Q-4. Write an SQL query to print the first three characters of FIRST_NAME from Worker table.
Q-5. Write an SQL query to find the position of the alphabet (‘a’) in the first name column
‘Amitabh’ from Worker table.
Q-6. Write an SQL query to print the FIRST_NAME from Worker table after removing white spaces
from the right side.
Q-7. Write an SQL query to print the DEPARTMENT from Worker table after removing white spaces
from the left side.
Q-8. Write an SQL query that fetches the unique values of DEPARTMENT from Worker table and
prints its length.
Q-9. Write an SQL query to print the FIRST_NAME from Worker table after replacing ‘a’ with ‘A’.
Q-11. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME
Ascending.
Q-12. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME
Ascending and DEPARTMENT Descending.
Q-13. Write an SQL query to print details for Workers with the first name as “Vipul” and “Satish”
from Worker table.
Q-14. Write an SQL query to print details of workers excluding first names, “Vipul” and “Satish”
from Worker table.
Q-15. Write an SQL query to print details of Workers with DEPARTMENT name as “Admin”.
Q-16. Write an SQL query to print details of the Workers whose FIRST_NAME contains ‘a’.
Q-17. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘a’.
SELECT *
FROM Worker
WHERE FIRST_NAME LIKE '%a';
Q-18. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘h’ and
contains six alphabets.
Q-20. Write an SQL query to print details of the Workers who have joined in Feb’2014.
Q-21. Write an SQL query to fetch the count of employees working in the department ‘Admin’.
Q-22. Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.
Q-23. Write an SQL query to fetch the no. of workers for each department in the descending order.
Q-24. Write an SQL query to print details of the Workers who are also Managers.
SELECT *
FROM Worker
WHERE IS_MANAGER = 1;
Q-25. Write an SQL query to fetch duplicate records having matching data in some fields of a table.
SELECT *
FROM your_table
WHERE (COLUMN1, COLUMN2, COLUMN3) IN (
SELECT COLUMN1, COLUMN2, COLUMN3
FROM your_table
GROUP BY COLUMN1, COLUMN2, COLUMN3
HAVING COUNT(*) > 1
);
Q-26. Write an SQL query to show only odd rows from a table.
SELECT * FROM your_table
WHERE MOD(your_primary_key_column, 2) <> 0;
Q-27. Write an SQL query to show only even rows from a table.
Q-28. Write an SQL query to clone a new table from another table.
Q-30. Write an SQL query to show records from one table that another table does not have.
Q-31. Write an SQL query to show the current date and time.
Q-32. Write an SQL query to show the top n (say 10) records of a table.
SELECT *
FROM your_table
LIMIT 10;
Q-33. Write an SQL query to determine the nth (say n=5) highest salary from a table.
Q-34. Write an SQL query to determine the 5th highest salary without using TOP or limit method.
Q-36. Write an SQL query to show the second highest salary from a table.
Q-37. Write an SQL query to show one row twice in results from a table.
Q-39. Write an SQL query to fetch the first 50% records from a table.
Q-40. Write an SQL query to fetch the departments that have less than five people in it.
Q-41. Write an SQL query to show all departments along with the number of people in there.
Q-42. Write an SQL query to show the last record from a table.
SELECT *
FROM your_table
ORDER BY your_primary_key_column DESC
LIMIT 1;
Q-43. Write an SQL query to fetch the first row of a table. B
Q-44. Write an SQL query to fetch the last five records from a table.
Q-45. Write an SQL query to print the name of employees having the highest salary in each
department.
WITH RankedSalaries AS (
SELECT
EmployeeName,
DEPARTMENT,
SALARY,
ROW_NUMBER() OVER (PARTITION BY DEPARTMENT ORDER BY SALARY DESC) AS SalaryRank
FROM your_table
)
SELECT EmployeeName, DEPARTMENT, SALARY
FROM RankedSalaries
WHERE SalaryRank = 1;
Q-46. Write an SQL query to fetch three max salaries from a table.
Q-47. Write an SQL query to fetch three min salaries from a table.
SELECT DISTINCT Salary
FROM your_table
ORDER BY Salary
LIMIT 3;
Q-48. Write an SQL query to fetch nth max salaries from a table.
Q-50. Write an SQL query to fetch the names of workers who earn the highest salary.
SELECT WorkerName
FROM your_table
ORDER BY Salary DESC
LIMIT 1;