Unlock your potential in data analytics. Discover the essential SQL Interview Questions asked by Swiggy and Zomato for 2+ years of experience in roles, with competitive 18-22 LPA packages. Get practical insights and actionable tips to confidently navigate your next interview. Read More
Schema Design
1. Restaurants Table
Store details of each restaurant:
CREATE TABLE Restaurants (
restaurant_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
address TEXT NOT NULL,
phone VARCHAR(20),
cuisine_type VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
2. Customers Table
Store customer details:
CREATE TABLE Customers (
customer_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
phone VARCHAR(20) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
3. Menu Table
Store restaurant menus with their items:
CREATE TABLE Menu (
menu_id INT PRIMARY KEY AUTO_INCREMENT,
restaurant_id INT NOT NULL,
item_name VARCHAR(255) NOT NULL,
price DECIMAL(10,2) NOT NULL,
category VARCHAR(100),
is_available BOOLEAN DEFAULT TRUE,
FOREIGN KEY (restaurant_id) REFERENCES Restaurants(restaurant_id) ON DELETE CASCADE
);
4. Orders Table
Store orders placed by customers:
CREATE TABLE Orders (
order_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
restaurant_id INT NOT NULL,
order_status ENUM('Pending', 'Processing', 'Completed', 'Cancelled') DEFAULT 'Pending',
order_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_amount DECIMAL(10,2) NOT NULL,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id) ON DELETE CASCADE,
FOREIGN KEY (restaurant_id) REFERENCES Restaurants(restaurant_id) ON DELETE CASCADE
);
5. Order_Items Table
Store items within an order:
CREATE TABLE Order_Items (
order_item_id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT NOT NULL,
menu_id INT NOT NULL,
quantity INT CHECK (quantity > 0),
price DECIMAL(10,2) NOT NULL,
FOREIGN KEY (order_id) REFERENCES Orders(order_id) ON DELETE CASCADE,
FOREIGN KEY (menu_id) REFERENCES Menu(menu_id) ON DELETE CASCADE
);
6. Payments Table
Store payment details for orders:
CREATE TABLE Payments (
payment_id INT PRIMARY KEY AUTO_INCREMENT,
order_id INT NOT NULL UNIQUE,
payment_method ENUM('Credit Card', 'Debit Card', 'Cash', 'UPI') NOT NULL,
payment_status ENUM('Pending', 'Completed', 'Failed') DEFAULT 'Pending',
transaction_id VARCHAR(50) UNIQUE,
payment_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (order_id) REFERENCES Orders(order_id) ON DELETE CASCADE
);
7. Reviews Table
Store customer reviews and ratings for restaurants:
CREATE TABLE Reviews (
review_id INT PRIMARY KEY AUTO_INCREMENT,
customer_id INT NOT NULL,
restaurant_id INT NOT NULL,
rating INT CHECK (rating BETWEEN 1 AND 5),
review_text TEXT,
review_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (customer_id) REFERENCES Customers(customer_id) ON DELETE CASCADE,
FOREIGN KEY (restaurant_id) REFERENCES Restaurants(restaurant_id) ON DELETE CASCADE
);
✅ Normalization: Reduces redundancy (separate Orders
, Order_Items
, Menu
).
✅ Referential Integrity: Enforced via foreign keys (CASCADE
deletion ensures consistency).
✅ Scalability: Easy to extend with additional features (e.g., promotions, delivery tracking).
✅ Efficiency: Indexed primary keys ensure fast lookups; ENUM
types for status fields optimize storage.
WITH salesum AS (
SELECT
YEAR(purchase_date) AS year,
customer_id,
SUM(purchase_amount) AS total_purchase
FROM Purchases
GROUP BY YEAR(purchase_date), customer_id
),
saleagg AS (
SELECT
s1.customer_id,
s1.year AS prev_year,
s1.total_purchase AS prev_year_sale,
s2.year AS curr_year,
s2.total_purchase AS current_year_sale
FROM salesum s1
INNER JOIN salesum s2
ON s1.customer_id = s2.customer_id
AND s1.year + 1 = s2.year
)
SELECT *,
ROUND(((current_year_sale - prev_year_sale) / prev_year_sale) * 100, 2) AS Y_on_Y_growth
FROM saleagg;
https://www.db-fiddle.com/f/u4LKSXpHVTy6KSXgKHkZ6q/0
First, I focused on understanding the requirement: calculating the year-over-year (YoY) growth in customer spending on Zomato. To do this, I needed to analyze the available data and extract the necessary information. The key table for this calculation was the Purchases table since it contained all the relevant timestamps and spending details.
Step 1: Extracting Customer-Wise Yearly Spend
I created a Common Table Expression (CTE) where:
purchase_date
.customer_id
.Step 2: Aggregating Year-on-Year Sales
Next, I created another CTE that aggregated the previous and current year’s spending by:
customer_id
, ensuring the second year was always the next year of the first.Step 3: Calculating Year-on-Year Growth
In the final query:
This gave me the year-over-year growth in customer spending in a structured and optimized way. 🚀
SELECT
DATE_FORMAT(review_date, '%Y-%m') AS month,
restaurant_id,
ROUND(AVG(rating),2) AS avg_rating,
COUNT(rating) AS num_rating
FROM Reviews
GROUP BY restaurant_id, month
HAVING num_rating >= 2;
https://onecompiler.com/mysql/438j75sbb
My approach to solving this was to use the Reviews table because it contains all the necessary data: review date, restaurant ID, and rating—everything needed to compute the required result.
review_date
using DATE_FORMAT(review_date, '%Y-%m')
.AVG(rating)
, giving the monthly average rating per restaurant.COUNT(rating)
to get the number of reviews per restaurant per month.HAVING
clause to filter out restaurants with less than 2 reviews.By following this approach, I successfully retrieved the monthly average rating for each restaurant, ensuring that only restaurants with two or more reviews per month were considered.
Normalization organizes data to reduce redundancy and improve integrity by dividing tables into smaller, related ones. This minimizes anomalies in insertion, update, and deletion while improving data consistency.
A stored procedure is a precompiled collection of SQL statements that can be executed as a single unit. It is stored in the database and can accept input parameters, process data, and return results.
✅ Modularity – Encapsulates SQL logic for reuse.
✅ Performance – Reduces parsing and compilation overhead.
✅ Security – Can restrict direct table access by granting permission only on procedures.
✅ Maintainability – Simplifies complex queries by breaking them into manageable blocks.
When explaining a complex technical concept to a non-technical stakeholder, my approach is clarity, relevance, and simplicity. Here’s how I do it:
1️⃣ Understand Their Perspective – Focus on what matters to them and avoid jargon.
2️⃣ Use Analogies – Relate concepts to everyday examples (e.g., a database index is like a phone book).
3️⃣ Explain the ‘Why’ First – Show the impact before diving into details.
4️⃣ Keep It Visual & Concise – Use diagrams or simple explanations.
5️⃣ Encourage Questions – Verify understanding and adjust if needed.
Example: Instead of saying “We optimize queries with indexing,” I’d say “Indexes help find data faster, like using a phone book instead of searching every page.”
Clear, relatable explanations lead to better collaboration!
1. What are different types of SQL joins?
A. SQL joins are used to combine data from two or more tables based on a related column. The different types of joins are,
2. How do you find duplicate records in a table?
A. To find duplicate records in a table, we can used GROUP BY
clause along with HAVING COUNT(*)>1
.
3. What is a window function in SQL? Give examples?
A. A window function in SQL performs calculations across a set of table rows related to the current row, without collapsing the result set like aggregate functions do, it “slides” over a window of rows and allows you to compute running totals, rankings and other calculations while retaining individual row details.
For example,
4. How do you optimize a slow SQL query?
A. There are many techniques to optimize a slow running query, some of which are, using indexing, selecting only necessary columns, filtering early, using appropriate joins, partitioning large table, and using EXPLAIN ANALYSE to identify bottlenecks in query execution.
5. Explain the difference between a CTE and a subquery.
A. A Common Table Expression (CTE) is a temporary result set defined using with
and can be referenced multiple times in a query, making it more readable and efficient for complex operations. A subquery, on the other hand, is a nested query inside SELECT
, FROM
, or `WHERE, and executes once per use, which can sometimes impact performance.
6. How would you design a normalized database schema?
A. To design a normalized database schema, I follow normalization principles to minimize redundancy and improve data integrity. The key steps are:”
For example, in an e-commerce system, I would design tables like Customers (customer_id, name), Orders (order_id, customer_id, order_date), and Order_Items (order_item_id, order_id, product_id, quantity), ensuring all relationships are properly defined.
7. What is the use of indexing in SQL?
A. Indexing in SQL improves query performance by allowing the database engine to locate data faster instead of scanning entire tables. Indexes act like a roadmap, helping retrieve records efficiently.”
Key benefits:
“However, indexes add overhead on INSERT, UPDATE, and DELETE operations, so they should be used wisely.
We hope this article has provided valuable information about SQL Interview Questions. We’re here to support you on your data analyst journey. Make sure you share this article with your friends or on social media.
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Unlock the secret to lightning-fast SQL queries with covering indexes. Learn how these powerful indexes…
Why are multi-column indexes essential for SQL database performance? This guide answers that question by…
Sharpen your Python while loop skills with 20 carefully curated practice questions. This article provides…
These questions were asked in a recent interview Flipkart Business Analyst, these uncovered questions are…
The delete statement in SQL is used to remove existing records from a table, you…
SQL Interview Question at Zomato: These questions were recently asked in interview at Zomato, you…