Top 30 SQL Query Interview Questions: Updated On Sep 19, 2023 17:55 IST
Top 30 SQL Query Interview Questions: Updated On Sep 19, 2023 17:55 IST
Shiksha Online
Updated on Sep 19, 2023 17:55 IST
Structured Query Language or most commonly known as SQL is used on a daily
basis to handle, manipulate and analyze relational databases.
So, it is very important for all of us to understand how to write queries in SQL to
generate meaningful insights. This article will cover the top 30 SQL query interview
questions which you must practice before attending an interview. In this article, I am
going to consider the following tables to explain to you the most asked SQL query
interview questions.
Patients T able
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Patient Patient Postal
Sex Age Address State Country RegDate
ID Name Code
Flat no 201,
Vasavi
01 Sheela F 23 500023 T elangana India 03/03/2020
Heights,
Yakutapura
Building no
02 Rehan M 21 560063 Karnataka India 13/11/2020
2, Yelahanka
H No 1,
03 Anay M 56 132140 Haryana India 12/12/2021
Panipat
House no
04 Mahira F 42 12, 382421 Gujarat India 28/01/2022
Gandhinagar
Sunf lower
05 Nishant M 12 Heights, 400080 Maharashtra India 05/01/2022
T hane
PatientsCheckup T able
01 121/80 67 300
02 142/76 78 400
03 151/75 55 300
04 160/81 61 550
05 143/67 78 700
Let us get started with the top interview questions about SQL.
Q1. Writ e an SQL query t o f et ch t he current dat e-t ime f rom t he syst em.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Copy code
Q3. Find t he Nt h highest consult at ion f ees f rom t he Pat ient sCheckup t able
wit h and wit hout using t he TOP/LIMIT keywords.
Nth highest consultation fees from the PatientsCheckup table with using the TOP
keywords
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
The Nth highest consultation fees from the PatientsCheckup table using the LIMIT
keywords.
Copy code
Nth highest consultation fees from the PatientsCheckup table without using the
TOP/LIMIT keywords.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Copy code
Q5. Writ e a SQL query t o creat e a t able where t he st ruct ure is copied f rom
ot her t able.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Copy code
Copy code
If you have an auto-increment field like PatientID then you can use the MOD()
function:
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Copy code
In case there are no auto-increment fields then you can use the Row_number in
SQL Server or a user-defined variable in MySQL. Then, check the remainder when
divided by 2.
Copy code
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER(ORDER BY Pat ient Id) AS RowNumber
FROM Pat ient s
)P
WHERE P.RowNumber % 2 = 0;
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER(ORDER BY Pat ient Id) AS RowNumber
FROM Pat ient s
)P
WHERE P.RowNumber % 2 = 0;
SELECT * FROM (
SELECT *, @rowNumber := @rowNumber+ 1 rn
FROM Pat ient s
JOIN (SELECT @rowNumber:= 0) r
)p
WHERE rn % 2 = 0;
In case you wish to find the odd rows, then the remainder when divided by 2 should
be 1.
Q7. Writ e an SQL query t o f et ch duplicat e records f rom Pat ient s, wit hout
considering t he primary key.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Q8. Writ e a query t o f et ch t he number of pat ient s whose weight is great er
t han 68.
Copy code
SELECT COUNT (*) FROM Pat ient sCheckup WHERE Weight > '68';
Q9. Writ e a query t o ret rieve t he list of pat ient s f rom t he same st at e.
Copy code
SELECT DIST INCT P.Pat ient ID, P.Pat ient Name, P.St at e
FROM Pat ient s P, Pat ient P1
WHERE P.St at e = P1.St at e AND P.Pat ient ID != P1.Pat ient ID;
Q10. Writ e a query t o ret rieve t wo minimum and maximum consult at ion f ees
f rom t he Pat ient sCheckup Table.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Q11. Writ e a query t o f et ch pat ient det ails along wit h t he weight f ees, even if
t he det ails are missing.
Copy code
Q12. Writ e a SQL query t o f et ch doct or wise count of pat ient s sort ed by t he
doct ors.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Q13. Writ e a SQL query t o f et ch t he f irst and last record of t he Pat ient s t able.
Copy code
Q14. Writ e a SQL query t o f et ch consult at ion f ees – wise count and sort t hem
in descending order.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Q15. Writ e a SQL query t o ret rieve pat ient det ails f rom t he Pat ient s t able who
have a weight in t he Pat ient sCheckup t able.
Copy code
Q16. Writ e a SQL query t o ret rieve t he last 2 records f rom t he Pat ient s t able.
Copy code
Q17. Writ e a SQL query t o f ind all t he pat ient s who joined in t he year 2022.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
– USING YEAR
SELECT * FROM Pat ient s WHERE YEAR(RegDat e ) = '2021';
Q18. Writ e a SQL query t o f et ch 50% records f rom t he Pat ient sCheckup t able.
Copy code
SELECT *
FROM Pat ient sCheckup WHERE
Pat ient ID <= (SELECT COUNT (Pat ient D)/2 FROM Pat ient sCheckup);
Q19. Writ e a query t o f ind t hose pat ient s who have paid consult at ion f ees
bet ween 400 t o 700.
Copy code
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Q20. Writ e a query t o updat e t he pat ient names by removing t he leading and
t railing spaces.
Copy code
Q21. Writ e a query t o add email validat ion t o your dat abase.
Copy code
Q22. Writ e a query t o f ind all pat ient names whose name:
Begin with A
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
SELECT * FROM Pat ient s WHERE Pat ient Name LIKE 'A%';
SELECT * FROM Pat ient s WHERE Pat ient Name LIKE '___S';
SELECT * FROM Pat ient s WHERE St at e LIKE 'T elangana%’;
Q23. Writ e a SQL query t o f et ch det ails of all pat ient s excluding pat ient s wit h
name “Sheela” and “Anay”.
Copy code
SELECT * FROM Pat ient s WHERE Pat ient Name NOT IN ('Sheela','Anay');
Copy code
Q25. Writ e a query t o ret rieve t he f irst t hree charact ers of Pat ient Name f rom
t he Pat ient s t able.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Q26. Writ e a query t o f et ch only t he Address (st ring bef ore space).
Copy code
Copy code
Q28. Writ e a query t o f et ch Pat ient IDs which are present in:
Both tables
One of the table. Let us say, patients present in Patients and not in the PatientsCheckup
table.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
Q29. Writ e a query t o f ind t he number of pat ient s whose RegDat e is bet ween
01/04/2021 t o 31/12/2022 and are grouped according t o st at e.
Copy code
SELECT COUNT (*), St at e FROM Pat ient s WHERE RegDat e BET WEEN '01/04/2021'
Q30. Writ e a query t o f et ch all records f rom t he Pat ient s t able; ordered by
Pat ient Name in ascending order, St at e in descending order.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Copy code
SELECT * FROM Pat ient s ORDER BY Pat ient Name ASC, St at e DESC;
With this, we end this article on the top 30 SQL query interview questions. We hope
you found it informative.
Relat ed Reads
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
Delet e St at ement in SQL
Delete statement is very impo rtant statement in SQL.This article co vers delete
statement with co nditio n and witho ut co nditio n( with pro per syntax and
examples)
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.
FOREIGN KEY IN SQL
Fo reign key co ncept is very impo rtamt co ncept in SQL. This article explains
creatio n,additio n and deletio n o f fo reign key.
Disclaim e r: This PDF is auto -generated based o n the info rmatio n available o n Shiksha as
o n 0 1-No v-20 23.