Tech Mahindra Data Analyst Interview Questions
Tech Mahindra Data Analyst Interview Questions
ROUND- 1
LEVEL : EASY
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.
Customers
customer_id customer_name
1 Alice
2 Bob
3 Charlie
Orders
101 1 250
102 2 300
Query:
-- INNER JOIN
FROM Customers c
-- LEFT JOIN
FROM Customers c
Output:
INNER JOIN:
customer_name amount
Alice 250
Bob 300
LEFT JOIN:
customer_name amount
Alice 250
Bob 300
Charlie NULL
Input Tables:
Customers
customer_id customer_name
1 Alice
2 Bob
Orders
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.
1 John 50000
2 Maya 60000
3 Lee 70000
Query:
FROM Employees;
Output:
avg_salary
60000.00
1 Alice 9999999999
2 Bob 8888888888
Query:
UPDATE Customers
WHERE customer_id = 2;
1 Alice 9999999999
2 Bob 7777777777
Explanation: UPDATE changes column values for matching rows based on a condition.
Explanation:
Example:
Before Normalization:
After 1NF:
Orders Table
OrderID CustomerName
101 Alice
OrderDetails Table
OrderID Product
101 Shoes
101 Socks
Benefits:
• Easier updates
101 C1 1000
102 C2 2000
103 C3 1500
104 C1 3000
105 C2 500
Query:
SELECT customer_id,
SUM(purchase_amount) AS total_purchase,
FROM Orders
GROUP BY customer_id;
Output:
C1 4000 1
C2 2500 2
C3 1500 3
Explanation:
1 John IT 60000
2 Maya HR 50000
3 Raj IT 80000
Query:
FROM Employees
GROUP BY department
HAVING SUM(salary) = (
SELECT MAX(dept_total)
FROM (
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.
1 Laptop 150000
2 Phone 120000
3 Headphones 30000
4 Monitor 45000
5 Keyboard 10000
6 Mouse 20000
Query:
FROM Sales
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.
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:
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.
Explanation:
Optimization
Description
Technique
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