SQL practical Questions
Sample Table – Worker
WORKER_ID FIRST_NAME LAST_NAME SALARY JOINING_DAT DEPARTMEN
E T
001 Monika Arora 100000 2021-02-20 HR
09:00:00
002 Niharika Verma 80000 2021-06-11 Admin
09:00:00
003 Vishal Singhal 300000 2021-02-20 HR
09:00:00
004 Amitabh Singh 500000 2021-02-20 Admin
09:00:00
005 Vivek Bhati 500000 2021-06-11 Admin
09:00:00
006 Vipul Diwan 200000 2021-06-11 Account
09:00:00
007 Satish Kumar 75000 2021-01-20 Account
09:00:00
008 Geetika Chauhan 90000 2021-04-11
09:00:00
Bonus
WORKER_REF_ID BONUS_DATE BONUS_AMOUNT
1 2023-02-20 00:00:00 5000
2 2023-06-11 00:00:00 3000
3 2023-02-20 00:00:00 4000
1 2023-02-20 00:00:00 4500
2 2023-06-11 00:00:00 3500
Title
WORKER_REF_ID WORKER_TITLE AFFECTED_FROM
1 Manager 2023-02-20 00:00:00
2 Executive 2023-06-11 00:00:00
8 Executive 2023-06-11 00:00:00
5 Manager 2023-06-11 00:00:00
WORKER_REF_ID WORKER_TITLE AFFECTED_FROM
4 Asst. Manager 2023-06-11 00:00:00
7 Executive 2023-06-11 00:00:00
6 Lead 2023-06-11 00:00:00
3 Lead 2023-06-11 00:00:00
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, '21-02-20
09.00.00', 'HR'),(002, 'Niharika', 'Verma', 80000, '21-06-11
09.00.00', 'Admin'),(003, 'Vishal', 'Singhal', 300000, '21-02-20
09.00.00', 'HR'),(004, 'Amitabh', 'Singh', 500000, '21-02-20
09.00.00', 'Admin'),(005, 'Vivek', 'Bhati', 500000, '21-06-11
09.00.00', 'Admin'),(006, 'Vipul', 'Diwan', 200000, '21-06-11
09.00.00', 'Account'),(007, 'Satish', 'Kumar', 75000, '21-01-20
09.00.00', 'Account'),(008, 'Geetika', 'Chauhan', 90000, '21-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, '23-02-20'),
(002, 3000, '23-06-11'),
(003, 4000, '23-02-20'),
(001, 4500, '23-02-20'),
(002, 3500, '23-06-11');
CREATE TABLE Title(
WORKER_REF_ID INT,
WORKER_TITLE CHAR(25),
AFFECTED_FROM DATETIME,
FOREIGN KEY (WORKER_REF_ID)
REFERENCES Worker(WORKER_ID)
ON DELETE CASCADE
);
INSERT INTO Title (WORKER_REF_ID, WORKER_TITLE, AFFECTED_FROM) VALUES
(001, 'Manager', '2023-02-20 00:00:00'),
(002, 'Executive', '2023-06-11 00:00:00'),
(008, 'Executive', '2023-06-11 00:00:00'),
(005, 'Manager', '2023-06-11 00:00:00'),
(004, 'Asst. Manager', '2023-06-11 00:00:00'),
(007, 'Executive', '2023-06-11 00:00:00'),
(006, 'Lead', '2023-06-11 00:00:00'),
(003, 'Lead', '2023-06-11 00:00:00');
Q-1. Write an SQL query to fetch “FIRST_NAME” from the Worker table using the alias name
<WORKER_NAME>.
Ans.Select FIRST_NAME AS WORKER_NAME from Worker;
Q-2. Write an SQL query to fetch “FIRST_NAME” from the Worker table in upper case.
Ans.Select upper(FIRST_NAME) from Worker;
Q-3. Write an SQL query to fetch unique values of DEPARTMENT from the Worker table.
Ans.Select distinct DEPARTMENT from Worker;
Q-4. Write an SQL query to print the first three characters of FIRST_NAME from the Worker
table.
Ans.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 the Worker table.
Ans.Select INSTR(FIRST_NAME, BINARY'a') from Worker where FIRST_NAME = 'Amitabh';
Notes.
The INSTR does a case-insensitive search.
Using the BINARY operator will make INSTR work as the case-sensitive function.
Q-6. Write an SQL query to print the FIRST_NAME from the Worker table after removing
white spaces from the right side.
Ans.Select RTRIM(FIRST_NAME) from Worker;
Q-7. Write an SQL query to print the DEPARTMENT from the Worker table after removing
white spaces from the left side.
Ans.Select LTRIM(DEPARTMENT) from Worker;
Q-8. Write an SQL query that fetches the unique values of DEPARTMENT from the Worker
table and prints its length.
Ans.Select distinct length(DEPARTMENT) from Worker;
Q-9. Write an SQL query to print the FIRST_NAME from the Worker table after replacing ‘a’
with ‘A’.
Ans.Select REPLACE(FIRST_NAME,'a','A') from Worker;
Q-10. Write an SQL query to print the FIRST_NAME and LAST_NAME from the Worker table
into a single column COMPLETE_NAME. A space char should separate them.
Ans.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.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.Select * from Worker order by FIRST_NAME asc,DEPARTMENT desc;
Q-13. Write an SQL query to print details for Workers with the first names “Vipul” and “Satish”
from the Worker table.
Ans.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 the Worker table.
Ans.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. Select * from Worker where DEPARTMENT like 'Admin%';