Unveiling SQL Interview Essentials: 15 Crucial Questions for Success
Gurunath Kadam
Strategic Manager of Learning & Development | Subject Matter Expert | Oracle & Microsoft Certified | Educator | Software Developer | Corporate Trainer | Technical Speaker
Elevate your SQL interview preparation with our comprehensive guide, offering an in-depth compilation of the most frequently asked questions. Anticipate encountering 2-3 questions from this curated list during your interview, ensuring you're well-equipped to showcase your SQL proficiency. Get ready to stand out and excel in your SQL interviews.
Let's delve into the questions with a sample table script:
-- Creating the 'emp' table
CREATE TABLE emp (
emp_id INT,
emp_name VARCHAR(20),
department_id INT,
salary INT,
manager_id INT,
emp_age INT
);
-- Inserting data into the 'emp' table
INSERT INTO emp VALUES (1, 'Ankit', 100, 10000, 4, 39);
INSERT INTO emp VALUES (2, 'Mohit', 100, 15000, 5, 48);
INSERT INTO emp VALUES (3, 'Vikas', 100, 10000, 4, 37);
INSERT INTO emp VALUES (4, 'Rohit', 100, 5000, 2, 16);
-- More INSERT statements...
-- Creating the 'department' table
CREATE TABLE department(
dept_id INT,
dept_name VARCHAR(10)
);
-- Inserting data into the 'department' table
INSERT INTO department values(100,'Analytics');
INSERT INTO department values(300,'IT');
-- More INSERT statements...
-- Creating the 'orders' table
CREATE TABLE orders(
customer_name VARCHAR(10),
order_date DATE,
order_amount INT,
customer_gender VARCHAR(6)
);
-- Inserting data into the 'orders' table
INSERT INTO orders values('Shilpa','2020-01-01',10000,'Female');
INSERT INTO orders values('Rahul','2020-01-02',12000,'Male');
-- More INSERT statements...
-- Creating the 'employees' table
CREATE TABLE employees (
emp_id INT,
emp_name VARCHAR(50),
department_id INT,
salary INT,
manager_id INT,
gender VARCHAR(10)
);
-- Inserting data into the 'employees' table
INSERT INTO employees VALUES (1, 'John Doe', 100, 50000, 4, 'Male');
INSERT INTO employees VALUES (2, 'Jane Smith', 100, 60000, 5, 'Female');
INSERT INTO employees VALUES (3, 'Alice Johnson', 200, 55000, 4, 'Female');
INSERT INTO employees VALUES (4, 'Bob Brown', 200, 52000, 2, 'Male');
-- More INSERT statements...
-- Creating the 'customers' table
CREATE TABLE customers (
customer_id INT,
customer_name VARCHAR(50),
gender VARCHAR(10),
email VARCHAR(50),
phone VARCHAR(15)
);
-- Inserting data into the 'customers' table
INSERT INTO customers VALUES (1, 'Rakesh Kumar', 'Male', '[email protected]', '123-456-7890');
INSERT INTO customers VALUES (2, 'Sunita Sharma', 'Female', '[email protected]', '987-654-3210');
INSERT INTO customers VALUES (3, 'Amit Singh', 'Male', '[email protected]', '456-789-0123');
INSERT INTO customers VALUES (4, 'Anita Gupta', 'Female', '[email protected]', '321-654-9870');
-- More INSERT statements...
Let's dive into the questions:
Question 1 — How to find duplicates in a given table?
SELECT
emp_id, COUNT(*)
FROM
emp
GROUP BY emp_id
HAVING COUNT(*) > 1;
Explanation:
Question 2 — How to delete duplicates?
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY emp_id ORDER BY emp_id) AS rn
FROM emp
)
DELETE FROM cte WHERE rn > 1;
Explanation:
Question 3 — Difference between UNION and UNION ALL? Assuming we have two tables employees and customers, and we want to combine the results of two queries:
-- UNION Example
SELECT emp_name FROM employees
UNION
SELECT customer_name FROM customers;
Explanation: The UNION operator combines the results of two SELECT statements and removes duplicate rows from the combined result set. In this example, the query selects names from emp_name in the employees table and customer_name in the customers table, and any duplicate names are removed from the final result set.
-- UNION ALL Example
SELECT emp_name FROM employees
UNION ALL
SELECT customer_name FROM customers;
Explanation: The UNION ALL operator also combines the results of two SELECT statements but does not remove duplicate rows from the combined result set. In this example, all rows from both emp_name in employees and customer_name in customers are included in the final result set, including duplicates.
Question 4 — Difference between RANK, ROW_NUMBER, and DENSE_RANK? Code Example:
-- RANK Example
SELECT
emp_id,
emp_name,
salary,
RANK() OVER(ORDER BY salary DESC) AS rnk
FROM
employees;
-- ROW_NUMBER Example
SELECT
emp_id,
emp_name,
salary,
ROW_NUMBER() OVER(ORDER BY salary DESC) AS rn
FROM
employees;
-- DENSE_RANK Example
SELECT
emp_id,
emp_name,
salary,
DENSE_RANK() OVER(ORDER BY salary DESC) AS rn
FROM
employees;
Explanation:
Question 5 — How to find employees who are not present in the department table? Code Example:
SELECT
领英推荐
*
FROM
employees AS e
LEFT JOIN
departments AS d ON e.department_id = d.dept_id
WHERE d.dept_id IS NULL;
Explanation:
Question 6 — How to find the second highest salary in each department? Code Example:
SELECT *
FROM (
SELECT
emp_name,
salary,
department_id,
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rn
FROM employees
) AS a
WHERE rn = 2;
Explanation:
Question 7 — Find all the transactions done by customers named "RAKESH". Code Example:
SELECT *
FROM orders
WHERE UPPER(customer_name) = 'RAKESH';
Explanation:
Question 8 — How to perform a self join to find cases where the manager's salary is greater than the employee's salary?
Code Example:
SELECT
e1.emp_id,
e1.emp_name,
e2.emp_name AS manager_name,
e1.salary AS employee_salary,
e2.salary AS manager_salary
FROM
employees AS e1
INNER JOIN
employees AS e2 ON e1.manager_id = e2.emp_id
WHERE e2.salary > e1.salary;
Explanation:
Question 9 — How to find all transactions done by female customers?
Code Example:
SELECT *
FROM orders
WHERE UPPER(customer_gender) = 'FEMALE';
Explanation:
Question 10 — How to find employees who are not assigned to any department?
Code Example:
SELECT *
FROM employees AS e
LEFT JOIN departments AS d ON e.department_id = d.dept_id
WHERE d.dept_id IS NULL;
Explanation:
Question 11 — How to calculate the total number of orders placed on each order date?
Code Example:
SELECT
order_date,
COUNT(*) AS total_orders
FROM orders
GROUP BY order_date;
Explanation:
Question 12 — How to identify employees with the highest salary in each department?
Code Example:
SELECT *
FROM (
SELECT
emp_name,
salary,
department_id,
ROW_NUMBER() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rn
FROM employees
) AS a
WHERE rn = 1;
Explanation:
Question 13 — How to find the total salary expenditure for each department?
Code Example:
SELECT
d.dept_name,
SUM(e.salary) AS total_salary_expenditure
FROM
employees e
JOIN
departments d ON e.department_id = d.dept_id
GROUP BY
d.dept_name;
Explanation:
Question 14 — How to identify employees who have the same salary as their managers?
Code Example:
SELECT
e.emp_id,
e.emp_name,
e.salary AS employee_salary,
m.emp_name AS manager_name,
m.salary AS manager_salary
FROM
employees e
JOIN
employees m ON e.manager_id = m.emp_id
WHERE
e.salary = m.salary;
Explanation:
Question 15 — Update query to swap gender?
Code Example:
UPDATE employees
SET gender =
CASE
WHEN gender = 'Male' THEN 'Female'
WHEN gender = 'Female' THEN 'Male'
ELSE gender
END;
Explanation:
In conclusion, mastering SQL interview questions is crucial for individuals aiming to excel in their career endeavors. By delving into the core concepts and practicing key scenarios, you're not only preparing for interviews but also laying a strong foundation for success in real-world projects.
We've explored 15 essential SQL interview questions, ranging from data manipulation to analytical queries, providing you with a comprehensive toolkit for success. Remember, preparation is the key to unlocking your full potential during interviews.
As you continue to refine your SQL skills and deepen your understanding of database management, leverage these questions as springboards for further exploration and practice. Stay curious, stay persistent, and keep honing your expertise.
With dedication and strategic preparation, you can navigate SQL interviews with ease, leaving a lasting impression on prospective employers and positioning yourself as a valuable asset in the competitive landscape of data management and analysis.
Embrace the challenge, embrace the opportunity, and let your SQL prowess shine bright in every interview scenario.
Senior Data Analyst and Visualization Expert at BlueNumber
10 个月https://datalemur.com?referralCode=lSS6lvqh