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_IDFIRST_NAMELAST_NAMESALARYJOINING_DATEDEPARTMENT
001MonikaArora1000002014-02-20 09:00:00HR
002NiharikaVerma800002014-06-11 09:00:00Admin
003VishalSinghal3000002014-02-20 09:00:00HR
004AmitabhSingh5000002014-02-20 09:00:00Admin
005VivekBhati5000002014-06-11 09:00:00Admin
006VipulDiwan2000002014-06-11 09:00:00Account
007SatishKumar750002014-01-20 09:00:00Account
008GeetikaChauhan900002014-04-11 09:00:00Admin

Sample Table – Bonus

WORKER_REF_IDBONUS_DATEBONUS_AMOUNT
12016-02-20 00:00:005000
22016-06-11 00:00:003000
32016-02-20 00:00:004000
12016-02-20 00:00:004500
22016-06-11 00:00:003500

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.

CREATE DATABASE ORG;
SHOW DATABASES;
USE ORG;

CREATE TABLE Worker (
	WORKER_ID INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
	FIRST_NAME CHAR(25),
	LAST_NAME CHAR(25),
	SALARY INT(15),
	JOINING_DATE DATETIME,
	DEPARTMENT CHAR(25)
);

INSERT INTO Worker 
	(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');

CREATE TABLE Bonus (
	WORKER_REF_ID INT,
	BONUS_AMOUNT INT(10),
	BONUS_DATE DATETIME,
	FOREIGN KEY (WORKER_REF_ID)
		REFERENCES Worker(WORKER_ID)
        ON DELETE CASCADE
);

INSERT INTO Bonus 
	(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.

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:

Select FIRST_NAME AS WORKER_NAME from Worker;

 

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

Ans.

Required query is:

Select upper(FIRST_NAME) from Worker;

 

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

Ans.

Required query is:

Select distinct DEPARTMENT from Worker;

 

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

Ans.

Required query is:

Select substring(FIRST_NAME,1,3) from Worker;

 

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:

Select INSTR(FIRST_NAME, BINARY'a') from Worker where FIRST_NAME = 'Amitabh';

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:

Select RTRIM(FIRST_NAME) from Worker;

 

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:

Select LTRIM(DEPARTMENT) from Worker;

 

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

Ans.

Required query is:

Select distinct length(DEPARTMENT) from Worker;

 

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

Ans.

Required query is:

Select REPLACE(FIRST_NAME,'a','A') from Worker;

 

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:

Select CONCAT(FIRST_NAME, ' ', LAST_NAME) AS 'COMPLETE_NAME' from Worker;

 

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

Ans.

Required query is:

Select * from Worker order by FIRST_NAME asc;

 

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:

Select * from Worker order by FIRST_NAME asc,DEPARTMENT desc;

 

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:

Select * from Worker where FIRST_NAME in ('Vipul','Satish');

 

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

Ans.

Required query is:

Select * from Worker where FIRST_NAME not in ('Vipul','Satish');

 

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

Ans.

Required query is:

Select * from Worker where DEPARTMENT like 'Admin%';

 

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

Ans.

Required query is:

Select * from Worker where FIRST_NAME like '%a%';

 

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

Ans.

Required query is:

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.

Ans.

Required query is:

Select * from Worker where FIRST_NAME like '_____h';

 

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

Ans.

Required query is:

Select * from Worker where SALARY between 100000 and 500000;

 

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

Ans.

Required query is:

Select * from Worker where year(JOINING_DATE) = 2014 and month(JOINING_DATE) = 2

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.

4 Comments

  1. deepika Reply
    • Meenakshi Agarwal Reply
  2. Vinod Gadkar Reply
    • Meenakshi Agarwal Reply