50 SQL Query Questions You Should Practice For Interview PDF
50 SQL Query Questions You Should Practice For Interview PDF
50 SQL Query Questions and Answers for Practice Example: Python sockets
SPONSORED SEARCHES
50 sql queries
If you want to improve SQL skills, then install a SQL package like MySQL and start practicing with it. To
get you started, we’ve outlined a few SQL query questions in this post.
Solving practice questions is the fastest way to learn any subject. That’s why we’ve selected a set of 50
SQL queries that you can use to step up your learning. We’ve also given SQL scripts to create the test
data. So, you can use them to create a test database and tables.
Most of the SQL query questions we’ve filtered out of interviews held by top IT MNC like Flipkart and
Amazon. So you’ll gain real-time experience by going through them.
Also, we recommend that you first try to form queries by yourself rather than just reading them from the
post. Try to find answers on your own.
But you can’t start until the required sample data is not in place. You can check out the tables below
that we’ve provided for practice. So first of all, you need to create the test data in your database
software.
By the way, we have a bunch of other posts available for SQL interview preparation. So if you are
interested, then follow the link given below.
Once above SQL would run, you’ll see a result similar to the one attached below.
Creating Sample Data to Practice SQL Skill.
Q-1. Write An SQL Query To Fetch “FIRST_NAME” From Worker Table Using The Alias
Name As <WORKER_NAME>.
Ans.
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.
Notes.
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’
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.
The required query is:
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-20. Write An SQL Query To Print Details Of The Workers Who Have Joined In Feb’2014.
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-23. Write An SQL Query To Fetch The No. Of Workers For Each Department In The
Descending Order.
Ans.
Q-24. Write An SQL Query To Print Details Of The Workers Who Are Also Managers.
Ans.
Q-25. Write An SQL Query To Fetch Duplicate Records Having Matching Data In Some
Fields Of A Table.
Ans.
Q-26. Write An SQL Query To Show Only Odd Rows From A Table.
Ans.
Q-27. Write An SQL Query To Show Only Even Rows From A Table.
Ans.
Q-28. Write An SQL Query To Clone A New Table From Another Table.
Ans.
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();
SELECT NOW();
Following SQL Server query returns the current date and time:
SELECT getdate();
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:
Following SQL Server query will return the top n records using the TOP command:
Following Oracle query will return the top n records with the help of ROWNUM:
Q-33. Write An SQL Query To Determine The Nth (Say N=5) Highest Salary From A Table.
Ans.
The following SQL Server query returns the nth highest salary:
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.
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:
Ans.
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.
Q-46. Write An SQL Query To Fetch Three Max Salaries From A Table.
Ans.
SELECT distinct Salary from worker a WHERE 3 >= (SELECT count(distinct Salary)
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)
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)
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.
SELECT FIRST_NAME, SALARY from Worker WHERE SALARY=(SELECT max(SALARY) from Wor
If you find something new to learn today, then do share it with others. And, follow us on our social media
(Facebook/Twitter) accounts to see more of this.
Best,
TechBeamers
RELATED
Top 30 PL SQL Interview Top SQL Interview Questions One
Questions And Answers For Should Know Beforehand
Freshers
Python Tips & Tricks Linux Interview Questions PHP Interview Questions-2
© 2019 TechBeamers.com