0% found this document useful (0 votes)
115 views11 pages

Tech Mahindra Data Analyst Interview Questions

DATA Analyst interview questions

Uploaded by

Såmarth Tyagi
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)
115 views11 pages

Tech Mahindra Data Analyst Interview Questions

DATA Analyst interview questions

Uploaded by

Såmarth Tyagi
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/ 11

TECH MAHINDRA SQL INTERVIEW QUESTIONS

ROUND- 1
LEVEL : EASY

1. Query Basics: Explain the difference between INNER JOIN


and LEFT JOIN.
Difference between INNER JOIN and LEFT JOIN

Explanation:

• INNER JOIN: Returns records that have matching values in both tables.

• LEFT JOIN: Returns all records from the left table, and matched records from the
right table. If no match, the result is NULL on the right side.

Example Input Tables:

Customers
customer_id customer_name

1 Alice

2 Bob

3 Charlie

Orders

order_id customer_id amount

101 1 250

102 2 300

Query:

-- INNER JOIN

SELECT c.customer_name, o.amount

FROM Customers c

INNER JOIN Orders o ON c.customer_id = o.customer_id;

-- LEFT JOIN

SELECT c.customer_name, o.amount

FROM Customers c

LEFT JOIN Orders o ON c.customer_id = o.customer_id;

Output:

INNER JOIN:

customer_name amount

Alice 250

Bob 300

LEFT JOIN:
customer_name amount

Alice 250

Bob 300

Charlie NULL

2. Subqueries: Provide an example of how you'd use a subquery


to retrieve data from multiple tables.
Subqueries: Example Using Multiple Tables

Scenario: Get names of customers who placed orders over ₹200.

Input Tables:

Customers

customer_id customer_name

1 Alice

2 Bob

Orders

order_id customer_id amount

101 1 250

102 2 150

Query:

SELECT customer_name

FROM Customers

WHERE customer_id IN (

SELECT customer_id

FROM Orders
WHERE amount > 200

);

Output:

customer_name

Alice

Explanation: The subquery fetches customer IDs with orders > ₹200, and the outer query
gets their names.

3.Aggregation: Demonstrate how to calculate the average salary


of employees from the "Employees" table.
Average Salary of Employees

Input Table: Employees

emp_id emp_name salary

1 John 50000

2 Maya 60000

3 Lee 70000

Query:

SELECT AVG(salary) AS avg_salary

FROM Employees;

Output:

avg_salary

60000.00

Explanation: AVG() is used to calculate the average value of a numeric column.


4.Data Manipulation: Write an SQL statement to update the
contact details of a specific customer.
Data Manipulation: Update Customer Contact Details

Scenario: Update contact number of customer with ID = 2.

Input Table: Customers

customer_id customer_name contact

1 Alice 9999999999

2 Bob 8888888888

Query:

UPDATE Customers

SET contact = '7777777777'

WHERE customer_id = 2;

Result (after update):

customer_id customer_name contact

1 Alice 9999999999

2 Bob 7777777777

Explanation: UPDATE changes column values for matching rows based on a condition.

5.Normalization: Discuss the concept of database normalization


and its importance in data analysis.
Normalization: Concept and Importance

Explanation:

• Normalization is the process of organizing data in a database to reduce data


redundancy and improve data integrity.
• It breaks down large tables into smaller ones and defines relationships among
them.

Common Normal Forms:

• 1NF: Atomic values (no repeating groups).

• 2NF: 1NF + no partial dependency on a composite key.

• 3NF: 2NF + no transitive dependency.

Example:

Before Normalization:

OrderID CustomerName Product1 Product2

101 Alice Shoes Socks

After 1NF:

Orders Table

OrderID CustomerName

101 Alice

OrderDetails Table

OrderID Product

101 Shoes

101 Socks

Benefits:

• Avoids duplicate data

• Easier updates

• Better scalability and clarity in analytics

6.Window Functions: Show how to rank customers based on


their purchase amounts using window functions.
Window Functions: Rank Customers by Purchase Amounts

Input Table: Orders

order_id customer_id purchase_amount

101 C1 1000

102 C2 2000

103 C3 1500

104 C1 3000

105 C2 500

Objective: Rank customers based on their total purchase amount.

Query:

SELECT customer_id,

SUM(purchase_amount) AS total_purchase,

RANK() OVER (ORDER BY SUM(purchase_amount) DESC) AS purchase_rank

FROM Orders

GROUP BY customer_id;

Output:

customer_id total_purchase purchase_rank

C1 4000 1

C2 2500 2

C3 1500 3

Explanation:

• SUM(purchase_amount) groups the total for each customer.

• RANK() assigns ranks based on descending total purchase.


7.Grouping and Filtering: How can you find the highest-earning
department using GROUP BY and HAVING?
Grouping & Filtering: Find Highest-Earning Department

Input Table: Employees

emp_id emp_name department salary

1 John IT 60000

2 Maya HR 50000

3 Raj IT 80000

4 Lily Finance 30000

Query:

SELECT department, SUM(salary) AS total_salary

FROM Employees

GROUP BY department

HAVING SUM(salary) = (

SELECT MAX(dept_total)

FROM (

SELECT department, SUM(salary) AS dept_total

FROM Employees

GROUP BY department

) AS temp

);

Output:

department total_salary

IT 140000
Explanation: The inner subquery calculates total salaries per department, and the outer
query filters the department(s) with the highest total using HAVING.

8.Data Extraction: Retrieve the top 5 products that contribute to


the most revenue.
Data Extraction: Top 5 Revenue-Generating Products

Input Table: Sales

product_id product_name revenue

1 Laptop 150000

2 Phone 120000

3 Headphones 30000

4 Monitor 45000

5 Keyboard 10000

6 Mouse 20000

Query:

SELECT product_name, revenue

FROM Sales

ORDER BY revenue DESC

LIMIT 5;

Output:

product_name revenue

Laptop 150000

Phone 120000

Monitor 45000
product_name revenue

Headphones 30000

Mouse 20000

Explanation: ORDER BY revenue DESC sorts by revenue, and LIMIT 5 picks the top 5
products.

9.Date Functions: Illustrate how to extract the month and year


from a date column.
Date Functions: Extract Month and Year from a Date Column

Input Table: Orders

order_id order_date

1 2023-01-10

2 2023-04-15

3 2024-01-20

Query (MySQL):

SELECT order_id,

order_date,

MONTH(order_date) AS order_month,

YEAR(order_date) AS order_year

FROM Orders;

Output:

order_id order_date order_month order_year

1 2023-01-10 1 2023

2 2023-04-15 4 2023
order_id order_date order_month order_year

3 2024-01-20 1 2024

Explanation: MONTH() and YEAR() functions extract respective parts from a date.

10.Performance Optimization: Explain techniques to optimize


SQL queries for faster execution
Performance Optimization: Techniques for Faster SQL Execution

Explanation:

Below are several ways to optimize SQL queries:

Optimization
Description
Technique

Speeds up data retrieval from large tables. Ensure indexes on


Use Indexing
columns used in JOIN, WHERE, and ORDER BY.

**Avoid SELECT *** Fetch only required columns to reduce I/O.

Use EXISTS
For correlated subqueries, EXISTS is usually faster.
instead of IN

Use proper JOINs Ensure you’re not doing unnecessary full joins or Cartesian products.

Use LIMIT To restrict data scan when only top rows are needed.

Analyze Query
Use EXPLAIN (MySQL/PostgreSQL) to see query execution strategy.
Plans

Denormalize For performance in read-heavy environments, denormalizing can


cautiously help.

You might also like