SQL Interview Question at Zomato for a Data analyst Position (0-3 Years) – | Shared By An Experienced Data Analyst

SQL Interview Question at Zomato: These questions were recently asked in interview at Zomato, you must master these. read more

SQL Interview Question from Zomato: These questions were recently asked in an interview.

SQL Interview Question at Zomato

1. Write a SQL query to find the top 5 customers with the highest total order value in the last month.

https://onecompiler.com/mysql/438qdug79

2. How do you remove duplicate records from a table?

https://onecompiler.com/mysql/438tfb979

For this question, I am using the Reviews table. I define a duplicate as a case where a customer has more than one review for the same restaurant on the same order date. Any subsequent review beyond the first one will be considered a duplicate.

To identify duplicates, I used a ROW_NUMBER() function inside a CTE. This assigns a unique row number to each review, grouped by customer_id, restaurant_id, and order_date. If a row has a row number greater than 1, it means it’s a duplicate.

In the DELETE statement, I removed these duplicates by filtering out records where the review_id exists in the list of duplicate records identified in the CTE.


3. Write a query to calculate the average order value per restaurant.

https://onecompiler.com/mysql/438wdrdeq


4. Retrieve the top 3 most ordered dishes from the past 6 months.

https://onecompiler.com/mysql/438we2vy2


5. Explain the difference between LEFT JOIN, RIGHT JOIN, and INNER JOIN with examples.

The join in SQL are used to combine data from two or more tables, based on the requirement we can use left, right or inner join.

LEFT JOIN – Returns all data from left table and matching data from right table unmatched rows will from left table will be included with NULLs.

RIGHT JOIN – Returns all data from RIGHT table with matching data from LEFT table and unmatched rows from Right table will be included with NULLs.

INNER JOIN – Returns all matching data from both tables.

example scenario: Imagin you two tables Customers and Orders

Customers Table

customer_idcustomer_name
1Alice
2Bob
3Charlie
4David

Orders Table

order_idcustomer_idorder_dateamount
10112024-01-10500
10222024-01-12750
10312024-02-01300
10432024-02-15450

Left Join

Result:

customer_idcustomer_nameorder_idamount
1Alice101500
1Alice103300
2Bob102750
3Charlie104450
4DavidNULLNULL

It includes all rows from customers table and matching records from orders table.

Right Join

Result:

customer_idcustomer_nameorder_idamount
1Alice101500
1Alice103300
2Bob102750
3Charlie104450

It includes all rows from order table and matching records from customers table.

Inner Join

Result:

customer_idcustomer_nameorder_idamount
1Alice101500
1Alice103300
2Bob102750
3Charlie104450

It includes all matching rows from both customers and orders table.


6. How do you identify and handle missing values in SQL?

Missing values are represented as NULL in SQL, to find missing value s we can use ISNULL or COUNT() functions. Once identified we can eitgher remove the rows with missing value order we can ignore them in aggregations or we can fill them using a defaut value or using windows functions.


7. Write a query to rank restaurants based on total revenue using window functions.

https://onecompiler.com/mysql/438wjxzvu


8. Explain GROUP BY vs PARTITION BY in SQL.

GROUP BY groups data into single rows based on the specified columns while performing aggregation.
PARTITION BY performs the aggregation across partitions without collapsing the rows, retaining the original dataset structure.

AspectGROUP BYPARTITION BY
PurposeAggregates data into grouped rows.Divides data into partitions while retaining individual rows.
Result StructureReduces the result set to one row per group.Maintains all rows while applying calculations within partitions.
Use CaseSummarizing data (e.g., total sales per region).Performing window calculations (e.g., running totals or ranks).
Clause TypeUsed with aggregate functions and SELECT.Used with window functions like ROW_NUMBER(), RANK(), SUM().
VisibilityAppears directly in the query result.Works behind the scenes without altering row count.


9. How would you find customers who have placed orders in January but not in February?

we group by customer_id and apply the SUM(MONTH(order_date) = x) logic.

The expression MONTH(order_date) = 1 returns 1 if TRUE, else 0.

customer_idJan Orders (SUM(MONTH(order_date)=1))Feb Orders (SUM(MONTH(order_date)=2))
111
211
310
410
510

This query counts the orders for each customer in January and February. It selects the customer_id when:

  1. January Orders:SUM(MONTH(order_date) = 1) > 0
    • This ensures the customer placed at least one order in January.
  2. February Orders:SUM(MONTH(order_date) = 2) = 0
    • This ensures the customer placed no orders in February.

So, if the sum for January is greater than 0, it indicates orders were placed in January.
If the sum for February is 0, it indicates no orders were placed in February.

The result is a list of customers who ordered in January but not in February.


10. How do you optimize a slow SQL query?

There are many things that can be done to optimize a slow query, some common options are using index, avoid selecting unnecessary columns, filtering early like using where instead of having, using limit clause, use of CTEs and temp tables.

For More SQL Interview Question at Zomato Follow Us On:


Thank you for reading! We hope this article has been helpful in your SQL Interview Questions preparation. If you found it valuable, please share it with your network.

Also Read:-

SQL Interview Question at Zomato, SQL Interview Question at Zomato, SQL Interview Question at Zomato

Spread the love