20 SQL Query Questions and Answers for Practice

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 20 SQL queries that you can use to step up your learning.

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.

Let’s Begin Learning SQL.

20 SQL Query Questions and Answers for Practice

20 SQL Query Questions.

Prepare Sample Data To Practice SQL Skill.

Sample Table – Worker

WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DATE DEPARTMENT
001 Monika Arora 100000 2014-02-20 09:00:00 HR
002 Niharika Verma 80000 2014-06-11 09:00:00 Admin
003 Vishal Singhal 300000 2014-02-20 09:00:00 HR
004 Amitabh Singh 500000 2014-02-20 09:00:00 Admin
005 Vivek Bhati 500000 2014-06-11 09:00:00 Admin
006 Vipul Diwan 200000 2014-06-11 09:00:00 Account
007 Satish Kumar 75000 2014-01-20 09:00:00 Account
008 Geetika Chauhan 90000 2014-04-11 09:00:00 Admin

Sample Table – Bonus

WORKER_REF_ID BONUS_DATE BONUS_AMOUNT
1 2016-02-20 00:00:00 5000
2 2016-06-11 00:00:00 3000
3 2016-02-20 00:00:00 4000
1 2016-02-20 00:00:00 4500
2 2016-06-11 00:00:00 3500

To prepare the sample data, you can run the following queries in your database query executor or on the SQL command line. We’ve tested them with MySQL Server 5.7 and MySQL Workbench 6.3.8 query browser. You can also download these Softwares and install them to carry on the SQL exercise.

SQL Script to Seed Sample Data.

Once above SQL would run, you’ll see a result similar to the one attached below.

SQL Query Questions - Creating Sample Data

Creating Sample Data to Practice SQL Skill.

20 SQL Query Questions and Answers for Practice.

Q-1. Write an SQL query to fetch “FIRST_NAME” from Worker table using the alias name as <WORKER_NAME>.

Ans.

Required query is:

 

Q-2. Write an SQL query to fetch “FIRST_NAME” from Worker table in upper case.

Ans.

Required query is:

 

Q-3. Write an SQL query to fetch unique values of DEPARTMENT from Worker table.

Ans.

Required query is:

 

Q-4. Write an SQL query to print first three characters of  FIRST_NAME from Worker table.

Ans.

Required query is:

 

Q-5. Write an SQL query to find the position of the alphabet (‘a’) in the first name column ‘Amitabh’ from Worker table.

Ans.

Required query is:

Notes.

  • The INSTR method is in case-sensitive by default.
  • Using Binary operator will make INSTR work as the case-sensitive function.

 

Q-6. Write an SQL query to print the FIRST_NAME from Worker table after removing white spaces from the right side.

Ans.

Required query is:

 

Q-7. Write an SQL query to print the DEPARTMENT from Worker table after removing white spaces from the left side.

Ans.

Required query is:

 

Q-8. Write an SQL query that fetches the unique values of DEPARTMENT from Worker table and prints its length.

Ans.

Required query is:

 

Q-9. Write an SQL query to print the FIRST_NAME from Worker table after replacing ‘a’ with ‘A’.

Ans.

Required query is:

 

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.

Required query is:

 

Q-11. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending.

Ans.

Required query is:

 

Q-12. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending and DEPARTMENT Descending.

Ans.

Required query is:

 

Q-13. Write an SQL query to print details for Workers with the first name as “Vipul” and “Satish” from Worker table.

Ans.

Required query is:

 

Q-14. Write an SQL query to print details of workers excluding first names, “Vipul” and “Satish” from Worker table.

Ans.

Required query is:

 

Q-15. Write an SQL query to print details of Workers with DEPARTMENT name as “Admin”.

Ans.

Required query is:

 

Q-16. Write an SQL query to print details of the Workers whose FIRST_NAME contains ‘a’.

Ans.

Required query is:

 

Q-17. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘a’.

Ans.

Required query is:

 

Q-18. Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘h’ and contains six alphabets.

Ans.

Required query is:

 

Q-19. Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000.

Ans.

Required query is:

 

Q-20. Write an SQL query to print details of the Workers who has joined in Feb’2014.

Ans.

Required query is:

Summary – 20 SQL Query Questions and Answers for Practice.

Let us take a halt here. But we’ll resume with more interesting SQL query questions in our next post.

Hope, you’d fun learning through the SQL exercises.

TechBeamers.

Leave a Reply


*