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.

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.

MySQL

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

CREATEDATABASEORG;

SHOWDATABASES;

USEORG;

CREATETABLEWorker(

WORKER_IDINTNOT NULLPRIMARY KEYAUTO_INCREMENT,

FIRST_NAMECHAR(25),

LAST_NAMECHAR(25),

SALARYINT(15),

JOINING_DATEDATETIME,

DEPARTMENTCHAR(25)

);

INSERTINTOWorker

(WORKER_ID,FIRST_NAME,LAST_NAME,SALARY,JOINING_DATE,DEPARTMENT)VALUES

(001,'Monika','Arora',100000,'14-02-20 09.00.00','HR'),

(002,'Niharika','Verma',80000,'14-06-11 09.00.00','Admin'),

(003,'Vishal','Singhal',300000,'14-02-20 09.00.00','HR'),

(004,'Amitabh','Singh',500000,'14-02-20 09.00.00','Admin'),

(005,'Vivek','Bhati',500000,'14-06-11 09.00.00','Admin'),

(006,'Vipul','Diwan',200000,'14-06-11 09.00.00','Account'),

(007,'Satish','Kumar',75000,'14-01-20 09.00.00','Account'),

(008,'Geetika','Chauhan',90000,'14-04-11 09.00.00','Admin');

CREATETABLEBonus(

WORKER_REF_IDINT,

BONUS_AMOUNTINT(10),

BONUS_DATEDATETIME,

FOREIGN KEY(WORKER_REF_ID)

REFERENCESWorker(WORKER_ID)

ON DELETECASCADE

);

INSERTINTOBonus

(WORKER_REF_ID,BONUS_AMOUNT,BONUS_DATE)VALUES

(001,5000,'16-02-20'),

(002,3000,'16-06-11'),

(003,4000,'16-02-20'),

(001,4500,'16-02-20'),

(002,3500,'16-06-11');

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

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>.