DEV Community

Er. Bhupendra
Er. Bhupendra

Posted on

SQL Queries Asked In Interview

🧠 MASTER SQL INTERVIEW PATTERN

Almost all queries follow this one mental flow:

SELECT β†’ FROM β†’ WHERE β†’ GROUP BY β†’ HAVING β†’ ORDER BY β†’ LIMIT

Enter fullscreen mode Exit fullscreen mode

SELECT β†’ columns / functions (SUM, COUNT, AVG)
FROM β†’ table
WHERE β†’ row filtering (LIKE, BETWEEN, IN, =)
GROUP BY β†’ grouping
HAVING β†’ group filtering
ORDER BY β†’ sorting
LIMIT β†’ restrict rows

Remember this sentence:

β€œSmart Friends Wear Green Hats On Lunch.”

S β†’ SELECT
F β†’ FROM
W β†’ WHERE
G β†’ GROUP BY
H β†’ HAVING
O β†’ ORDER BY
L β†’ LIMIT
Enter fullscreen mode Exit fullscreen mode

Most interview queries are just variations of this pipeline.


πŸ”₯ PATTERN 1 β€” RANKING / TOP VALUES

Questions:

1️⃣ Nth highest salary
2️⃣ Second highest salary
12️⃣ Top N records
16️⃣ Max salary employee

Pattern

ORDER BY column DESC
LIMIT N
Enter fullscreen mode Exit fullscreen mode

Example

SELECT * 
FROM employees
ORDER BY salary DESC
LIMIT 3;
Enter fullscreen mode Exit fullscreen mode

Memory trick

Ranking β†’ ORDER BY + LIMIT


πŸ”₯ PATTERN 2 β€” DUPLICATE DETECTION

Questions:

3️⃣ duplicate names
11️⃣ duplicate records

Pattern

GROUP BY column
HAVING COUNT(*) > 1
Enter fullscreen mode Exit fullscreen mode

Example

SELECT name, COUNT(*)
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;
Enter fullscreen mode Exit fullscreen mode

Memory trick

Duplicates β†’ GROUP BY + HAVING


πŸ”₯ PATTERN 3 β€” FILTERING DATA

Questions:

7️⃣ names starting with A
9️⃣ name ending with A
10️⃣ salary between range

Pattern

WHERE condition
Enter fullscreen mode Exit fullscreen mode

Examples

WHERE name LIKE 'A%'
WHERE salary BETWEEN 10000 AND 50000
Enter fullscreen mode Exit fullscreen mode

Memory trick

Filtering β†’ WHERE


πŸ”₯ PATTERN 4 β€” COUNT / AGGREGATION

Questions:

8️⃣ number of employees in department

Pattern

SELECT COUNT(*)
FROM table
WHERE condition
Enter fullscreen mode Exit fullscreen mode

Example

SELECT COUNT(*)
FROM employees
WHERE department_name='ABC';
Enter fullscreen mode Exit fullscreen mode

Memory trick

Counting β†’ COUNT + WHERE


πŸ”₯ PATTERN 5 β€” TABLE COPYING

Questions:

5️⃣ create empty table
21️⃣ same structure table

Pattern

SELECT * INTO new_table
FROM old_table
WHERE 1=0
Enter fullscreen mode Exit fullscreen mode

Memory trick

Copy structure β†’ WHERE FALSE


πŸ”₯ PATTERN 6 β€” JOINS

Questions:

13️⃣ employees under manager
22️⃣ users with fewer orders
23️⃣ salary > 15000 using 2 tables

Pattern

SELECT columns
FROM table1
JOIN table2
ON condition
Enter fullscreen mode Exit fullscreen mode

Example

SELECT e.name, s.salary
FROM employees e
JOIN salaries s
ON e.employee_id = s.employee_id;
Enter fullscreen mode Exit fullscreen mode

Memory trick

Multiple tables β†’ JOIN


πŸ”₯ PATTERN 7 β€” SET OPERATIONS

Questions:

19️⃣ UNION
20️⃣ INTERSECT

Pattern

SELECT ...
UNION
SELECT ...
Enter fullscreen mode Exit fullscreen mode

Memory trick

Combining results β†’ UNION


πŸ”₯ PATTERN 8 β€” DATE QUERIES

Question:

15️⃣ employees hired in last 8 months

Pattern

WHERE date >= CURRENT_DATE - INTERVAL
Enter fullscreen mode Exit fullscreen mode

Example

WHERE hire_date >= CURDATE() - INTERVAL 8 MONTH;
Enter fullscreen mode Exit fullscreen mode

Memory trick

Time filtering β†’ INTERVAL


🧠 THE 7 SQL SUPER PATTERNS

If you remember this table, you can solve 90% SQL interview questions.

Pattern SQL Keyword Used For
Ranking ORDER BY + LIMIT highest salary
Duplicate detection GROUP BY + HAVING duplicates
Filtering WHERE conditions
Aggregation COUNT / SUM totals
Table copy SELECT INTO structure
Multi-table JOIN relations
Result merge UNION combine

⭐ INTERVIEW RECALL FRAMEWORK

When interviewer asks a query, think in this order:

1 What data? β†’ SELECT
2 From where? β†’ FROM
3 Any filter? β†’ WHERE
4 Any grouping? β†’ GROUP BY
5 Any group filter? β†’ HAVING
6 Any sorting? β†’ ORDER BY
7 Any limit? β†’ LIMIT
Enter fullscreen mode Exit fullscreen mode

This reconstructs the query mentally.


πŸš€ ULTIMATE 10-SECOND SQL RECALL METHOD

Remember FROGS-HL:

F β†’ FROM
R β†’ WHERE
O β†’ ORDER BY
G β†’ GROUP BY
S β†’ SELECT
H β†’ HAVING
L β†’ LIMIT
Enter fullscreen mode Exit fullscreen mode

⭐ FINAL INSIGHT (Important)

After analyzing your images:

Out of 23 questions

Category Count
Filtering 6
Ranking 4
Aggregation 3
Joins 3
Duplicates 2
Table copy 2
Set operations 2
Date queries 1

So interviewers mostly test:

Filtering + Ranking + Aggregation


Top comments (0)