TCS Data Analyst Interview Questions
TCS Data Analyst Interview Questions
QUESTIONS
0-3 YOE
0-8 lpa
1. Write a query to find the second highest salary from an employee
table.
Assuming your table is named Employees and it has a column called salary:
SELECT MAX(salary) AS SecondHighestSalary
FROM Employees
WHERE salary < (
SELECT MAX(salary) FROM Employees
);
2. How would you use a JOIN to combine data from two tables: one
with employee information and another with department information?
Assume the following structure:
• Employees(emp_id, emp_name, dept_id, salary)
• Departments(dept_id, dept_name)
You can use an INNER JOIN to combine them:
SELECT e.emp_id, e.emp_name, e.salary, d.dept_name
FROM Employees e
INNER JOIN Departments d ON e.dept_id = d.dept_id;
This query will return only those employees who are assigned to a department.
SELECT *
FROM Employees
WHERE join_date >= DATEADD(DAY, -30, GETDATE());
You can also join with the Departments table to get department names:
SELECT d.dept_name, COUNT(e.emp_id) AS employee_count
FROM Employees e
JOIN Departments d ON e.dept_id = d.dept_id
GROUP BY d.dept_name;
UPDATE Employees
SET salary = salary * 1.10;
This increases each employee’s salary by 10%.
10. Write a query to create a new table with columns for employee ID,
name, and salary
You can use the CREATE TABLE statement to define a new table.
Basic Query:
11. How would you retrieve the top 5 highest-paid employees from an
employee table?
There are different approaches based on the database system you use.
In MySQL / PostgreSQL (using LIMIT)
Additional Insight:
• If salaries have duplicates and you want ties included, consider using DENSE_RANK():
WITH RankedEmployees AS (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY emp_email ORDER BY emp_id) AS rn
FROM Employees
)
DELETE FROM Employees
WHERE emp_id IN (
SELECT emp_id
FROM RankedEmployees
WHERE rn > 1
);
Explanation:
• PARTITION BY emp_email: Groups rows with the same email.
• ROW_NUMBER() assigns a unique number to each row within that group.
• Only the first row is kept (rn = 1); duplicates (rn > 1) are deleted.
Option 2: Using DELETE with NOT IN (MySQL-style workaround)
13. Explain the difference between INNER JOIN, LEFT JOIN, RIGHT
JOIN, and FULL OUTER JOIN
INNER JOIN
• Returns only matching rows from both tables.
SELECT *
FROM Employees e
INNER JOIN Departments d ON e.dept_id = d.dept_id;
• If there's no match, the row is excluded.
LEFT JOIN (or LEFT OUTER JOIN)
• Returns all rows from the left table and matching rows from the right table.
• If there's no match on the right side, you'll get NULLs.
SELECT *
FROM Employees e
LEFT JOIN Departments d ON e.dept_id = d.dept_id;
RIGHT JOIN (or RIGHT OUTER JOIN)
• Opposite of LEFT JOIN.
• Returns all rows from the right table and matching rows from the left.
SELECT *
FROM Employees e
RIGHT JOIN Departments d ON e.dept_id = d.dept_id;
FULL OUTER JOIN
• Returns all rows when there is a match in one of the tables.
• If no match exists on either side, it returns NULLs in place.
SELECT *
FROM Employees e
FULL OUTER JOIN Departments d ON e.dept_id = d.dept_id;
Summary Table:
JOIN Type Rows Returned
INNER JOIN Only matching rows from both tables
LEFT JOIN All rows from the left table + matched from right
RIGHT JOIN All rows from the right table + matched from left
FULL OUTER JOIN All matched and unmatched rows from both tables
14. Write a query to calculate the average salary for each department
Assuming the table is Employees(emp_id, emp_name, dept_id, salary):
15. How do you use the CASE statement in SQL? Provide an example
The CASE statement is used for conditional logic in SQL, similar to if-else.
Syntax:
SELECT emp_name,
salary,
CASE
WHEN salary >= 100000 THEN 'High'
WHEN salary >= 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_band
FROM Employees;
Explanation:
• The CASE evaluates conditions in order and returns the first match.
• Here, we categorize each employee’s salary into High, Medium, or Low.
16. Write a query to find employees who have not been assigned to any
department
Assume:
• Employees(dept_id) is a foreign key referencing Departments(dept_id).
• Employees without a department will have NULL in dept_id, or there might be no match in the
Departments table.
Option 1: Using LEFT JOIN with IS NULL
SELECT e.*
FROM Employees e
LEFT JOIN Departments d ON e.dept_id = d.dept_id
WHERE d.dept_id IS NULL;
Explanation:
• LEFT JOIN returns all employees.
• WHERE d.dept_id IS NULL: filters employees without a matching department.
Option 2: Simple WHERE dept_id IS NULL (if NULLs are used for unassigned depts)
SELECT *
FROM Employees
WHERE dept_id IS NULL;
Use this only if unassigned departments are truly NULL in the Employees table.
17. Explain the concept of a Primary Key and a Foreign Key in SQL
Primary Key:
• A Primary Key uniquely identifies each record in a table.
• It cannot be NULL and must be unique across all rows.
• A table can have only one primary key (which can consist of one or more columns).
Example:
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
salary DECIMAL(10,2)
);
• emp_id is the primary key — no two employees can have the same ID, and it can't be NULL.
Foreign Key:
• A Foreign Key is a column (or set of columns) in one table that refers to the primary key in
another table.
• It creates a relationship between two tables, ensuring referential integrity.
Example:
CREATE TABLE Departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(100)
);
Example:
Add a new column email to the Employees table:
ALTER TABLE Employees
ADD email VARCHAR(100);
Explanation:
• ALTER TABLE Employees: Target the table you want to change.
• ADD email VARCHAR(100): Adds a new column email that can store up to 100 characters.
You can also set a default value:
ALTER TABLE Employees
ADD status VARCHAR(10) DEFAULT 'Active';
This adds a status column with a default value of 'Active' for new rows.