SQL Practice
SQL Practice
SQL Practice
SHOW DATABASES;
USE ORG;
Q-2. Write An SQL Query To Fetch �FIRST_NAME� From Worker Table In Upper Case.
Ans.
Q-3. Write An SQL Query To Fetch Unique Values Of DEPARTMENT From Worker Table.
Ans.
Q-4. Write An SQL Query To Print The First Three Characters Of FIRST_NAME From
Worker Table.
Ans.
Q-5. Write An SQL Query To Find The Position Of The Alphabet (�A�) In The First
Name Column �Amitabh� From Worker Table.
Ans.
Q-6. Write An SQL Query To Print The FIRST_NAME From Worker Table After Removing
White Spaces From The Right Side.
Ans.
Q-7. Write An SQL Query To Print The DEPARTMENT From Worker Table After Removing
White Spaces From The Left Side.
Ans.
Q-8. Write An SQL Query That Fetches The Unique Values Of DEPARTMENT From Worker
Table And Prints Its Length.
Ans.
Q-9. Write An SQL Query To Print The FIRST_NAME From Worker Table After Replacing
�A� With �A�.
Ans.
Q-10. Write An SQL Query To Print The FIRST_NAME And LAST_NAME From Worker Table
Into A Single Column COMPLETE_NAME. A Space Char Should Separate Them.
Ans.
Q-11. Write An SQL Query To Print All Worker Details From The Worker Table Order By
FIRST_NAME Ascending.
Ans.
Q-12. Write An SQL Query To Print All Worker Details From The Worker Table Order By
FIRST_NAME Ascending And DEPARTMENT Descending.
Ans.
Q-13. Write An SQL Query To Print Details For Workers With The First Name As
�Vipul� And �Satish� From Worker Table.
Ans.
Q-14. Write An SQL Query To Print Details Of Workers Excluding First Names, �Vipul�
And �Satish� From Worker Table.
Ans.
Q-15. Write An SQL Query To Print Details Of Workers With DEPARTMENT Name As
�Admin�.
Ans.
Q-16. Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Contains
�A�.
Ans.
Q-17. Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With
�A�.
Ans.
Q-18. Write An SQL Query To Print Details Of The Workers Whose FIRST_NAME Ends With
�H� And Contains Six Alphabets.
Ans.
Q-19. Write An SQL Query To Print Details Of The Workers Whose SALARY Lies Between
100000 And 500000.
Ans.
Q-21. Write An SQL Query To Fetch The Count Of Employees Working In The Department
�Admin�.
Ans.
Q-22. Write An SQL Query To Fetch Worker Names With Salaries >= 50000 And <=
100000.
Ans.
Q-25. Write An SQL Query To Fetch Duplicate Records Having Matching Data In Some
Fields Of A Table.
Ans.
Q-27. Write An SQL Query To Show Only Even Rows From A Table.
Ans.
Q-30. Write An SQL Query To Show Records From One Table That Another Table Does Not
Have.
Ans.
Q-31. Write An SQL Query To Show The Current Date And Time.
Ans.
SELECT CURDATE();
Following MySQL query returns the current date and time:
SELECT NOW();
Following SQL Server query returns the current date and time:
SELECT getdate();
Following Oracle query returns the current date and time:
Q-32. Write An SQL Query To Show The Top N (Say 10) Records Of A Table.
Ans.
Following MySQL query will return the top n records using the LIMIT method:
Q-33. Write An SQL Query To Determine The Nth (Say N=5) Highest Salary From A
Table.
Ans.
Q-34. Write An SQL Query To Determine The 5th Highest Salary Without Using TOP Or
Limit Method.
Ans.
The following query is using the correlated subquery to return the 5th highest
salary:
SELECT Salary
FROM Worker W1
WHERE 4 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Use the following generic method to find nth highest salary without using TOP or
limit.
SELECT Salary
FROM Worker W1
WHERE n-1 = (
SELECT COUNT( DISTINCT ( W2.Salary ) )
FROM Worker W2
WHERE W2.Salary >= W1.Salary
);
Q-35. Write An SQL Query To Fetch The List Of Employees With The Same Salary.
Ans.
Q-36. Write An SQL Query To Show The Second Highest Salary From A Table.
Ans.
Q-37. Write An SQL Query To Show One Row Twice In Results From A Table.
Ans.
Q-39. Write An SQL Query To Fetch The First 50% Records From A Table.
Ans.
SELECT *
FROM WORKER
WHERE WORKER_ID <= (SELECT count(WORKER_ID)/2 from Worker);
Q-40. Write An SQL Query To Fetch The Departments That Have Less Than Five People
In It.
Ans.
Q-41. Write An SQL Query To Show All Departments Along With The Number Of People In
There.
Ans.
Q-42. Write An SQL Query To Show The Last Record From A Table.
Ans.
The following query will return the last record from the Worker table:
Q-44. Write An SQL Query To Fetch The Last Five Records From A Table.
Ans.
Q-45. Write An SQL Query To Print The Name Of Employees Having The Highest Salary
In Each Department.
Ans.
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary) from
worker b WHERE a.Salary <= b.Salary) order by a.Salary desc;
Q-47. Write An SQL Query To Fetch Three Min Salaries From A Table.
Ans.
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary) from
worker b WHERE a.Salary >= b.Salary) order by a.Salary desc;
Q-48. Write An SQL Query To Fetch Nth Max Salaries From A Table.
Ans.
SELECT distinct Salary from worker a WHERE n >= (SELECT count(distinct Salary) from
worker b WHERE a.Salary <= b.Salary) order by a.Salary desc;
Q-49. Write An SQL Query To Fetch Departments Along With The Total Salaries Paid
For Each Of Them.
Ans.
Q-50. Write An SQL Query To Fetch The Names Of Workers Who Earn The Highest Salary.
Ans.