Tech Mahindra SQL Interview Questions for Data Engineer
Tech Mahindra SQL Interview Questions for Data Engineer
---=================
-- 1. Find the top 3 cities with the highest sales per month.
(sales_table):
drop table ##sales
Create table ##sales( sale_id int, city varchar (50), sale_date date,
amount int)
insert into ##sales values
(1,'Mumbai','2024-01-10','5000'),
(2,'Delhi','2024-01-15','7000'),
(3,'Bangalore','2024-01-20','10000'),
(4,'Mangalore','2024-01-20','12000'),
(5,'Chennai','2024-02-05','3000'),
(6,'Mumbai','2024-02-08','4000'),
(7,'Patna','2024-02-08','5000'),
(7,'Mumbai','2024-03-08','5000')
with cte as
(
select city,FORMAT(sale_date,'yyyy-
MM')sale_month,SUM(amount)amt,
ROW_NUMBER()over(partition by FORMAT(sale_date,'yyyy-MM')
order by SUM(amount) desc) as rn
from ##sales
group by city,FORMAT(sale_date,'yyyy-MM')
)select * from cte where rn<=3
--2. Write an SQL query to calculate the running total of sales for
each city. (sales_data):
drop table ##sales
Create table ##sales( sale_id int, city varchar (50), sale_date date,
amount int)
insert into ##sales values
(1,'Mumbai','2024-01-10','5000'),
(2,'Delhi ','2024-01-15','7000'),
(3,'Mumbai','2024-01-20','3000'),
(4,'Delhi ','2024-02-05','6000'),
(5,'Mumbai','2024-02-08','8000')
---method 2
SELECT TOP 1 * FROM ##employees WHERE Salary<(SELECT
MAX(SALARY) FROM ##employees) ORDER BY Salary DESC
---method 3
with cte as
(
SELECT TOP 2 * FROM ##employees order by SALARY desc
) select top 1 * from cte order by SALARY asc
---method 4 sub Query
select * from (
select * ,DENSE_RANK()over(order by salary desc)rn from
##employees
)aa where rn =2
--Method 1
with cte as
(
select *, dense_rank()over (partition by department order by
salary)rn from ##employees
)select * from cte where rn=1
---Method 2
SELECT e1.*
FROM ##employees e1
JOIN ##employees e2
ON e1.department = e2.department
AND e1.salary = e2.salary
AND e1.emp_id <> e2.emp_id
ORDER BY e1.department, e1.salary, e1.emp_id;
with cte as
(
select * ,convert (varchar (3),datename(MONTH,sale_date))as mon
from ##sales_data
) select city,sum([jan]) as [jan],sum([Feb]) as [Feb],sum([Mar]) as
[Mar] from cte
pivot (sum(amount) for mon in([jan],[Feb],[Mar])) as pvt
group by city
SELECT * FROM (
SELECT * ,COUNT(1)OVER(ORDER BY CUSTOMER_ID)RNK FROM
##ORDERS WHERE ORDER_DATE>=DATEADD(MONTH,-
6,GETDATE())
) AA WHERE RNK=3
----9. NORMALIZATION VS. DENORMALIZATION – WHAT ARE
THEY, AND WHEN SHOULD EACH BE USED IN A DATA PIPELINE?