0% found this document useful (0 votes)
19 views20 pages

Top 30 SQL Query Interview Questions: Updated On Sep 19, 2023 17:55 IST

Uploaded by

pushpakrk15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views20 pages

Top 30 SQL Query Interview Questions: Updated On Sep 19, 2023 17:55 IST

Uploaded by

pushpakrk15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Top 30 SQL Query Interview Questions

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

Patient ID BP Weight Consultation Fees

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

Top SQL Interview Questions(Query)

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

T O f et ch t he CURRENT DAT E IN SQL Server


SELECT GET DAT E();
T O f et ch t he CURRENT DAT E IN MYSQL
SELECT NOW();
T O f et ch t he CURRENT DAT E IN Oracle
SELECT SYSDAT E FROM DUAL();

Q2. Writ e a SQL query t o f et ch t he Pat ient Name in uppercase and st at e as


lowercase. Also use t he ALIAS name f or t he result -set as Pat Name and
NewSt at e.

Copy code

T O f et ch t he CURRENT DAT E IN SQL Server


SELECT GET DAT E();
T O f et ch t he CURRENT DAT E IN MYSQL
SELECT NOW();
T O f et ch t he CURRENT DAT E IN Oracle
SELECT SYSDAT E FROM DUAL();

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

SELECT T OP 1 Consult at ionFees


FROM(
SELECT T OP N Consult at ionFees
FROM Pat ient sCheckup
ORDER BY Consult at ionFees DESC) AS FEES
ORDER BY Consult at ionFees ASC;

The Nth highest consultation fees from the PatientsCheckup table using the LIMIT
keywords.

Copy code

SELECT Consult at ionFees


FROM Pat ient sCheckup
ORDER BY Consult at ionFees DESC LIMIT N-1,1;

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

SELECT Consult at ionFees


FROM Pat ient sCheckup F1
WHERE N-1 = (
SELECT COUNT ( DIST INCT ( F2.Consult at ionFees ) )
FROM Pat ient sCheckup F2
WHERE F2.Consult at ionFees > F1.Consult at ionFees );

Q4. Writ e a query t o f et ch t op N records using t he TOP/LIMIT, ordered by


Consult at ionFees.

TOP Command – SQL Server

Copy code

SELECT T OP N * FROM Pat ient sCheckup ORDER BY Consult at ionFees DESC;

LIMIT Command - MySQL


SELECT * FROM Pat ient sCheckup ORDER BY Consult at ionFees DESC LIMIT N;

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.

Create an Empty T able

Create a table consisting data

To create an Empty 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

CREAT E T ABLE NewPat ient sT able


SELECT * FROM Pat ient s WHERE 1=0;

Create a table consisting of data

Using SELECT command

Copy code

SELECT * INT O NewPat ient sT able FROM Pat ient s WHERE 1 = 0;

Using CREAT E command in MySQL

Copy code

CREAT E T ABLE NewPat ient sT able AS SELECT * FROM Pat ient s;

Q6. Writ e a query t o f et ch even and odd rows f rom a t able.

If you have an auto-increment field like PatientID then you can use the MOD()
function:

Fetch even rows using 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

SELECT * FROM Pat ient s WHERE MOD(Pat ient ID,2)=0;

Fetch odd rows using MOD() function:

Copy code

SELECT * FROM Pat ient s WHERE MOD(Pat ient ID,2)=1;

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.

Fetch even rows in SQL Server

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;

Fetch even rows in SQL Server

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;

Fet ch even rows in MySQL

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

SELECT Pat ient Name, Doct orID, RegDat e, St at e, COUNT (*)


FROM Pat ient s
GROUP BY Pat ient Name, Doct orID, RegDat e, St at e
HAVING COUNT (*) > 1;

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

– T WO MINIMUM CONSULT AT ION FEES


SELECT DIST INCT Consult at ionFees FROM Pat ient sCheckup P1
WHERE 2 >= (SELECT COUNT (DIST INCT Consult at ionFees)FROM Pat ient sCheckup P2
WHERE P1.Consult at ionFees >= P2.Consult at ionFees) ORDER BY P1.SConsult at ionFees

– T WO MAXIMUM CONSULT AT ION FEES


SELECT DIST INCT Consult at ionFees FROM Pat ient sCheckup P1
WHERE 2 >= (SELECT COUNT (DIST INCT Consult at ionFees)FROM Pat ient sCheckup P2
WHERE P1.Consult at ionFees <= P2.Consult at ionFees) ORDER BY P1.Consult at ionFees

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

SELECT P.Pat ient Name, C.Consult at ionFees


FROM Pat ient s P
LEFT JOIN
Pat ient sCheckup C
ON P.Pat ient Id = C.Pat ient Id;

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

SELECT Doct orID, COUNT (Pat ient ID) AS DocPat


FROM Pat ient s GROUP BY Doct orID
ORDER BY DocPat ;

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

–FET CH FIRST RECORD


SELECT * FROM Pat ient s WHERE Pat ient ID = (SELECT MIN(Pat ient ID) FROM Pat ient s

–FET CH LAST RECORD


SELECT * FROM Pat ient s WHERE Pat ient ID = (SELECT MAX(Pat ient ID) FROM Pat ient s

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

SELECT Consult at ionFees, COUNT (Pat ient Id) CFCount


FROM Pat ient sCheckup
GROUP BY Consult at ionFees
ORDER BY CFCount DESC;

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

SELECT * FROM Pat ient s P


WHERE EXIST S
(SELECT * FROM Pat ient sCheckup C WHERE P.Pat ient ID = C.Pat ient ID);

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

SELECT * FROM Pat ient s WHERE


Pat ient ID <=2 UNION SELECT * FROM
(SELECT * FROM Pat ient s P ORDER BY P.Pat ient ID DESC)
AS P1 WHERE P1.Pat ient ID <=2;

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 BET WEEN


SELECT * FROM Pat ient s
WHERE RegDat e BET WEEN '2021/01/01' AND '2021/12/31';

– 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

SELECT * FROM Pat ient s WHERE Pat ient ID IN


(SELECT Pat ient ID FROM Pat ient sCheckup WHERE Consult at ionFees BET WEEN '400'

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

UPDAT E Pat ient s


SET Pat ient Name = LT RIM(RT RIM(Pat ient Name));

Q21. Writ e a query t o add email validat ion t o your dat abase.

Copy code

SELECT email FROM Pat ient s WHERE NOT REGEXP_LIKE(email, ‘[A-Z0-9._%+-]+@

Q22. Writ e a query t o f ind all pat ient names whose name:

Begin with A

Ends with S and contains 3 alphabets

Staying in the state T elangana

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');

Q24. Writ e a query t o f et ch t he t ot al count of occurrences of a part icular


charact er – ‘x’ in t he Pat ient Name.

Copy code

SELECT Pat ient Name, Pat ient ID


LENGT H(Pat ient Name) - LENGT H(REPLACE(Pat ient Name, 'x', ''))
FROM Pat ient s;

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

SELECT SUBST RING(Pat ient Name, 1, 3) FROM Pat ient s;

Q26. Writ e a query t o f et ch only t he Address (st ring bef ore space).

Copy code

USING t he MID FUNCT ION IN MySQL


SELECT MID(Address, 0, LOCAT E(' ',Address)) FROM Pat ient s;

USING SUBST RING


SELECT SUBST RING(Address, 1, CHARINDEX(' ',Address)) FROM Pat ient s;

Q27. Writ e a query t o combine Address and st at e int o a new column –


NewAddress.

Copy code

SELECT CONCAT (Address, ' ', St at e) AS 'NewAddress' FROM Pat ient s;

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

–Present IN BOT H T ABLES


SELECT Pat ient Id FROM Pat ient s
WHERE Pat ient Id IN
(SELECT Pat ient Id FROM Pat ient sCheckup);

– Present IN One OF t he T ABLE


SELECT Pat ient Id FROM Pat ient s
WHERE Pat ient Id NOT IN
(SELECT Pat ient Id FROM Pat ient sCheckup);

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

What is t he Dif f erence Bet ween SQL and MySQL?


SQL is query pro gramming language to manage RDBMS while MySQL is
RDBMS that uses SQL.
In this article, we will discuss the key differences between SQL (Structured Query
Language) and...re ad m o re

Dif f erence bet ween SQL and NoSQL


This article designed fo r beginners will teach yo u difference between SQL and
No SQL. The tuto rial includes co ncepts, such as what is a SQL, what is No SQL,
and mo re.

Dif f erence Bet ween DELETE and TRUNCATE


Yo u will be asked in mo st o f the interviews abo ut the difference between
DELETE and TRUNCATE. So let’s understand what exactly are the differences
with their use cases.

How t o use TRUNCATE command in SQL?


SQL TRUNCATE TABLE co mmand is used to remo ve the reco rd fro m the table.
Co nfused, with the SQL DELETE statement, it also remo ves the reco rd fro m the
table.
So , why use...re ad m o re

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)

SQL ALTER TABLE : ADD, DROP, MODIFY, RENAME


ALTER TABLE in SQL is used to change the structure o f the existing table. In this
article, we will briefly discuss ho w to add, mo dify, dro p, rename co lumns,
co nstraints, and...re ad m o re

How t o use GROUP BY in SQL?


The GROUP BY clause in sql is used to gro up the ro ws that have identical
values by o ne o r mo re co lumns. In this article, we will briefly discuss ho w
GROUP...re ad m o re

How t o use DROP command in SQL?


DROP co mmand in SQL is a DDL co mmand that permanently remo ves the data
(o r existing table) fro m the database and free the space fro m the memo ry. In this
article, we...re ad m o re

SQL RIGHT JOIN – Example and Synt ax


Right Jo in in the sql returns all the ro ws fro m the right table and the matching
entries fro m bo th tables. In this article, we will discuss ho w to use right...re ad
m o re

SQL Tut orial : Basic t o Advance


Thro ugh the SQL tuto rial, we will try to explain the to pic fro m basic to advanced.
To pics are co vered in co mplete detail and explained with the help o f different
examples that...re ad m o re

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.

Cross Join in SQL


SQL Cro ss Jo in co mbines each ro w o f o ne table with each ro w o f ano ther table
and the number o f ro ws in the result set is the pro duct o f number o f...re ad
m o re

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy