0% found this document useful (0 votes)
4 views58 pages

SQL Last Min Rivision

The document contains SQL interview practice questions and answers, organized into sections covering table operations, data manipulation, and join operations. It includes various SQL commands for creating tables, modifying data, and performing queries, along with explanations of concepts like primary keys, foreign keys, and indexes. Additionally, it features examples of different types of joins and subqueries, providing a comprehensive resource for SQL interview preparation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views58 pages

SQL Last Min Rivision

The document contains SQL interview practice questions and answers, organized into sections covering table operations, data manipulation, and join operations. It includes various SQL commands for creating tables, modifying data, and performing queries, along with explanations of concepts like primary keys, foreign keys, and indexes. Additionally, it features examples of different types of joins and subqueries, providing a comprehensive resource for SQL interview preparation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQL - LAST MINUTE PRACTICE

SQL Interview Practice — Questions with


Answers

Section A — Table Operations


Q1. Create a table named employees with the following
columns:
 emp_id → INT
 emp_name → VARCHAR(100)
 department → VARCHAR(50)
 salary → INT

CREATE TABLE employees (


emp_id INT,
emp_name VARCHAR(100),
department VARCHAR(50),
salary INT
);

Q2. Write a query to display all tables in the database.


SHOW TABLES;

Q3. Describe the structure of the products table.


DESCRIBE products;

Q4. Rename the table employees to staff_members.


RENAME TABLE employees TO staff_members;

Q5. Add a new column phone_number of type


VARCHAR(15) to the customers table.
ALTER TABLE customers
ADD phone_number VARCHAR(15);
Q6. Remove the column stock from the products table.
ALTER TABLE products
DROP COLUMN stock;

Q7. Modify the datatype of price in products table from


INT to DECIMAL(10,2).
ALTER TABLE products
MODIFY price DECIMAL(10,2);

Q8. Rename the column customer_name to full_name in


the customers table.
ALTER TABLE customers
CHANGE customer_name full_name VARCHAR(100);

Q9. Add a primary key on product_id in the products


table.
ALTER TABLE products
ADD PRIMARY KEY (product_id);

Q10. Remove the primary key from the products table.


ALTER TABLE products
DROP PRIMARY KEY;

Q11. Add a unique constraint on the email column in the


customers table.
ALTER TABLE customers
ADD UNIQUE (email);

Q12. Add a foreign key in orders table for customer_id


referencing customers(customer_id).
ALTER TABLE orders
ADD CONSTRAINT fk_customer
FOREIGN KEY (customer_id)
REFERENCES customers(customer_id);

Q13. Drop the foreign key named fk_customer from the


orders table.
ALTER TABLE orders
DROP FOREIGN KEY fk_customer;

Q14. Create an index named idx_category on the category


column of products.
CREATE INDEX idx_category
ON products(category);

Q15. Drop the index idx_category from the products table.


DROP INDEX idx_category
ON products;

Q16. Remove all rows from the orders table without


deleting the table structure.
TRUNCATE TABLE orders;

Q17. Delete the table staff_members.


DROP TABLE staff_members;

Section B — Data Manipulation


Q18. Insert one row into the customers table.
INSERT INTO customers
(customer_id, customer_name, email, city, age)
VALUES
(1, 'Rahul', 'rahul@[Link]', 'Delhi', 25);
Q19. Insert three different products into the products
table.
INSERT INTO products
(product_id, product_name, category, price, stock)
VALUES
(101, 'Laptop', 'Electronics', 55000, 10),
(102, 'Phone', 'Electronics', 30000, 20),
(103, 'Chair', 'Furniture', 5000, 15);

Q20. Update the city of a customer whose customer_id = 5


to Hyderabad.
UPDATE customers
SET city = 'Hyderabad'
WHERE customer_id = 5;

Q21. Increase the price of all products in category


Electronics by 500.
UPDATE products
SET price = price + 500
WHERE category = 'Electronics';

Q22. Delete all customers whose age is less than 18.


DELETE FROM customers
WHERE age < 18;

Q23. Delete the product whose product_id = 101.


DELETE FROM products
WHERE product_id = 101;

Q24. Display all rows from the customers table.


SELECT * FROM customers;

Q25. Display only customers from the city Mumbai.


SELECT * FROM customers
WHERE city = 'Mumbai';
Q26. Display all products ordered by price in descending
order.
SELECT * FROM products
ORDER BY price DESC;

Q27. Display distinct categories available in the products


table.
SELECT DISTINCT category
FROM products;

Q28. Count total number of customers.


SELECT COUNT(*)
FROM customers;

Q29. Find the total stock available in the products table.


SELECT SUM(stock)
FROM products;

Q30. Find the average price of products.


SELECT AVG(price)
FROM products;

Q31. Display first 5 rows from the orders table.


SELECT * FROM orders
LIMIT 5;

Q32. Display 5 rows after skipping first 10 rows from the


orders table.
SELECT * FROM orders
LIMIT 5 OFFSET 10;
Q33. Show number of products in each category.
SELECT category, COUNT(*)
FROM products
GROUP BY category;

Q34. Display categories having more than 3 products.


SELECT category, COUNT(*)
FROM products
GROUP BY category
HAVING COUNT(*) > 3;

Q35. Display customer_id and total orders placed by each


customer.
SELECT customer_id, COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id;

Q36. Find customers who placed more than 2 orders.


SELECT customer_id, COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 2;

Section C — Mixed Interview Style


Questions
Q37. Create a table named departments with:
 department_id as primary key
 department_name as unique

CREATE TABLE departments (


department_id INT PRIMARY KEY,
department_name VARCHAR(100) UNIQUE
);

Q38. Add a new column joining_date to the employees


table.
ALTER TABLE employees
ADD joining_date DATE;

Q39. Write a query to remove duplicate email entries


using DISTINCT.
SELECT DISTINCT email
FROM customers;

Q40. Display the second highest priced product using


ORDER BY and LIMIT.
SELECT * FROM products
ORDER BY price DESC
LIMIT 1 OFFSET 1;

Q41. Write a query to show top 3 most expensive


products.
SELECT * FROM products
ORDER BY price DESC
LIMIT 3;

Q42. Display all customers whose names start with letter


A.
SELECT * FROM customers
WHERE customer_name LIKE 'A%';

Q43. Display all products where price is between 1000 and


5000.
SELECT * FROM products
WHERE price BETWEEN 1000 AND 5000;

Q44. Display all orders placed after 2025-01-01.


SELECT * FROM orders
WHERE order_date > '2025-01-01';
Q45. Find total quantity ordered for each product.
SELECT product_id, SUM(quantity) AS total_quantity
FROM orders
GROUP BY product_id;

Q46. Find average age of customers city-wise.


SELECT city, AVG(age) AS average_age
FROM customers
GROUP BY city;

Q47. Display cities having more than 5 customers.


SELECT city, COUNT(*) AS total_customers
FROM customers
GROUP BY city
HAVING COUNT(*) > 5;

Q48. Delete all records from customers but keep the table
structure.
TRUNCATE TABLE customers;

Q49. Drop the orders table completely.


DROP TABLE orders;

Q50. Create an index on both product_name and category.


CREATE INDEX idx_product_category
ON products(product_name, category);

Rapid Fire Round


Q51. Difference between DELETE, TRUNCATE, DROP
Command Meaning
DELETE Removes selected rows
TRUNCATE Removes all rows quickly
DROP Deletes complete table structure
Q52. Difference between PRIMARY KEY and UNIQUE
KEY
PRIMARY KEY UNIQUE KEY
Cannot contain NULL Can contain NULL
Only one allowed Multiple allowed
Identifies each row uniquely Prevents duplicate values

Q53. What happens if you insert duplicate values into a


UNIQUE column?
Duplicate values are not allowed. SQL throws an error.

Q54. Why do we use indexes?


Indexes improve query performance and make searching faster.

Q55. What is a foreign key and why is it used?


A foreign key connects two tables and maintains referential integrity between them.

SQL JOINs & Subqueries — Questions


with Answers

Section A — JOIN Practice

Tables
1. employees
emp_id emp_name dept_id manager_id salary

1 Amit 101 NULL 50000

2 Sneha 102 1 60000

3 Rahul 101 1 45000

4 Priya 103 2 70000

5 Arjun NULL 2 40000

2. departments
dept_id dept_name

101 HR

102 IT

103 Finance

104 Marketing

3. projects
project_id project_name emp_id

201 Website 2

202 Payroll 1

203 Banking App 4

204 CRM 6

4. bonuses
emp_id bonus

1 5000

2 7000

4 9000
emp_id bonus

Q1. Display employee names along with their department


names using INNER JOIN.
SELECT e.emp_name, d.dept_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;

Q2. Display employee names and their assigned project


names.
SELECT e.emp_name, p.project_name
FROM employees e
INNER JOIN projects p
ON e.emp_id = p.emp_id;

Q3. Display employees who received bonuses along with


bonus amount.
SELECT e.emp_name, [Link]
FROM employees e
INNER JOIN bonuses b
ON e.emp_id = b.emp_id;

Q4. Display employee name, department name, and


salary.
SELECT e.emp_name, d.dept_name, [Link]
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;

Q5. Find all employees working in the IT department


using INNER JOIN.
SELECT e.emp_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id
WHERE d.dept_name = 'IT';

LEFT JOIN Questions

Q6. Display all employees and their department names,


including employees without departments.
SELECT e.emp_name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;

Q7. Display all employees and their project names,


including employees without projects.
SELECT e.emp_name, p.project_name
FROM employees e
LEFT JOIN projects p
ON e.emp_id = p.emp_id;

Q8. Display all departments and employees working in


them using LEFT JOIN.
SELECT d.dept_name, e.emp_name
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id;

Q9. Find employees who do not have any projects


assigned.
SELECT e.emp_name
FROM employees e
LEFT JOIN projects p
ON e.emp_id = p.emp_id
WHERE p.project_id IS NULL;

Q10. Find employees who did not receive bonuses.


SELECT e.emp_name
FROM employees e
LEFT JOIN bonuses b
ON e.emp_id = b.emp_id
WHERE [Link] IS NULL;

RIGHT JOIN Questions

Q11. Display all departments and matching employees


using RIGHT JOIN.
SELECT e.emp_name, d.dept_name
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id;

Q12. Display all projects and matching employee names


using RIGHT JOIN.
SELECT e.emp_name, p.project_name
FROM employees e
RIGHT JOIN projects p
ON e.emp_id = p.emp_id;

Q13. Find departments that have no employees.


SELECT d.dept_name
FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id
WHERE e.emp_id IS NULL;

Q14. Find projects that are not assigned to any valid


employee.
SELECT p.project_name
FROM employees e
RIGHT JOIN projects p
ON e.emp_id = p.emp_id
WHERE e.emp_id IS NULL;
FULL JOIN Questions

Q15. Display all employees and all departments, including


unmatched rows from both tables.
SELECT e.emp_name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id

UNION

SELECT e.emp_name, d.dept_name


FROM employees e
RIGHT JOIN departments d
ON e.dept_id = d.dept_id;

Q16. Display all employees and all projects including


unmatched records.
SELECT e.emp_name, p.project_name
FROM employees e
LEFT JOIN projects p
ON e.emp_id = p.emp_id

UNION

SELECT e.emp_name, p.project_name


FROM employees e
RIGHT JOIN projects p
ON e.emp_id = p.emp_id;

CROSS JOIN Questions

Q17. Generate all possible combinations of employees and


departments.
SELECT *
FROM employees
CROSS JOIN departments;

Q18. Generate all combinations of employees and bonuses.


SELECT *
FROM employees
CROSS JOIN bonuses;

SELF JOIN Questions

Q19. Display employee names along with their manager


names.
SELECT a.emp_name AS employee,
b.emp_name AS manager
FROM employees a
JOIN employees b
ON a.manager_id = b.emp_id;

Q20. Find employees who work under manager "Amit".


SELECT a.emp_name
FROM employees a
JOIN employees b
ON a.manager_id = b.emp_id
WHERE b.emp_name = 'Amit';

Q21. Display employee-manager salary comparison.


SELECT a.emp_name AS employee,
[Link] AS employee_salary,
b.emp_name AS manager,
[Link] AS manager_salary
FROM employees a
JOIN employees b
ON a.manager_id = b.emp_id;

NATURAL JOIN Questions

Q22. Use NATURAL JOIN between employees and


bonuses.
SELECT *
FROM employees
NATURAL JOIN bonuses;
Q23. Use NATURAL JOIN between employees and
departments.
SELECT *
FROM employees
NATURAL JOIN departments;

Mixed JOIN Questions

Q24. Display employee name, department name, project


name, and bonus.
SELECT e.emp_name,
d.dept_name,
p.project_name,
[Link]
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id
LEFT JOIN projects p
ON e.emp_id = p.emp_id
LEFT JOIN bonuses b
ON e.emp_id = b.emp_id;

Q25. Find employees who belong to a department but are


not assigned any project.
SELECT e.emp_name
FROM employees e
LEFT JOIN projects p
ON e.emp_id = p.emp_id
WHERE e.dept_id IS NOT NULL
AND p.project_id IS NULL;

Q26. Find employees who have both projects and bonuses.


SELECT e.emp_name
FROM employees e
INNER JOIN projects p
ON e.emp_id = p.emp_id
INNER JOIN bonuses b
ON e.emp_id = b.emp_id;
Q27. Find employees whose department exists but bonus
does not exist.
SELECT e.emp_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id
LEFT JOIN bonuses b
ON e.emp_id = b.emp_id
WHERE b.emp_id IS NULL;

Q28. Display all departments with total number of


employees.
SELECT d.dept_name,
COUNT(e.emp_id) AS total_employees
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id
GROUP BY d.dept_name;

Q29. Display department names with average salary.


SELECT d.dept_name,
AVG([Link]) AS avg_salary
FROM departments d
INNER JOIN employees e
ON d.dept_id = e.dept_id
GROUP BY d.dept_name;

Q30. Find the department with highest average salary.


SELECT d.dept_name,
AVG([Link]) AS avg_salary
FROM departments d
INNER JOIN employees e
ON d.dept_id = e.dept_id
GROUP BY d.dept_name
ORDER BY avg_salary DESC
LIMIT 1;

Section B — Subquery Practice


Q31. Find employees whose salary is greater than the
average salary of all employees.
SELECT emp_name
FROM employees
WHERE salary >
(
SELECT AVG(salary)
FROM employees
);

Q32. Find the employee with the maximum salary.


SELECT emp_name
FROM employees
WHERE salary =
(
SELECT MAX(salary)
FROM employees
);

Q33. Find employees earning less than the minimum


salary in IT department.
SELECT emp_name
FROM employees
WHERE salary <
(
SELECT MIN([Link])
FROM employees e
JOIN departments d
ON e.dept_id = d.dept_id
WHERE d.dept_name = 'IT'
);

Q34. Display employees whose salary equals the highest


salary.
SELECT *
FROM employees
WHERE salary =
(
SELECT MAX(salary)
FROM employees
);
Q35. Find employee details whose (dept_id, salary)
matches another selected row.
SELECT *
FROM employees
WHERE (dept_id, salary) =
(
SELECT dept_id, salary
FROM employees
WHERE emp_id = 2
);

Q36. Find employees whose (dept_id, manager_id)


matches employee with emp_id = 2.
SELECT *
FROM employees
WHERE (dept_id, manager_id) =
(
SELECT dept_id, manager_id
FROM employees
WHERE emp_id = 2
);

Q37. Find employees who belong to departments listed in


the departments table.
SELECT emp_name
FROM employees
WHERE dept_id IN
(
SELECT dept_id
FROM departments
);

Q38. Find employees whose emp_id exists in projects


table.
SELECT emp_name
FROM employees
WHERE emp_id IN
(
SELECT emp_id
FROM projects
);

Q39. Find employees who received bonuses.


SELECT emp_name
FROM employees
WHERE emp_id IN
(
SELECT emp_id
FROM bonuses
);

Q40. Find employees who are not assigned to any project.


SELECT emp_name
FROM employees
WHERE emp_id NOT IN
(
SELECT emp_id
FROM projects
);

Q41. Display all records from a subquery containing


employees with salary greater than 50000.
SELECT *
FROM
(
SELECT *
FROM employees
WHERE salary > 50000
) AS high_salary;

Q42. Create a derived table showing employee names and


salaries above average salary.
SELECT *
FROM
(
SELECT emp_name, salary
FROM employees
WHERE salary >
(
SELECT AVG(salary)
FROM employees
)
) AS avg_salary_table;

Q43. Find departments whose average salary is greater


than 55000 using table subquery.
SELECT *
FROM
(
SELECT dept_id,
AVG(salary) AS avg_salary
FROM employees
GROUP BY dept_id
) AS dept_avg
WHERE avg_salary > 55000;

Q44. Find employees earning more than the average


salary of their own department.
SELECT emp_name, salary
FROM employees e1
WHERE salary >
(
SELECT AVG(salary)
FROM employees e2
WHERE e1.dept_id = e2.dept_id
);

Q45. Find employees whose salary is the highest in their


department.
SELECT emp_name, dept_id, salary
FROM employees e1
WHERE salary =
(
SELECT MAX(salary)
FROM employees e2
WHERE e1.dept_id = e2.dept_id
);

Q46. Find departments where at least one employee earns


more than 65000.
SELECT dept_name
FROM departments d
WHERE EXISTS
(
SELECT *
FROM employees e
WHERE e.dept_id = d.dept_id
AND [Link] > 65000
);
Q47. Find employees whose bonus is greater than average
bonus.
SELECT e.emp_name
FROM employees e
JOIN bonuses b
ON e.emp_id = b.emp_id
WHERE [Link] >
(
SELECT AVG(bonus)
FROM bonuses
);

Q48. Find employees for whom project records exist.


SELECT emp_name
FROM employees e
WHERE EXISTS
(
SELECT *
FROM projects p
WHERE e.emp_id = p.emp_id
);

Q49. Find departments where employees exist.


SELECT dept_name
FROM departments d
WHERE EXISTS
(
SELECT *
FROM employees e
WHERE d.dept_id = e.dept_id
);

Q50. Find employees who received bonuses using EXISTS.


SELECT emp_name
FROM employees e
WHERE EXISTS
(
SELECT *
FROM bonuses b
WHERE e.emp_id = b.emp_id
);

Q51. Find employees working on projects using EXISTS.


SELECT emp_name
FROM employees e
WHERE EXISTS
(
SELECT *
FROM projects p
WHERE e.emp_id = p.emp_id
);

Q52. Find employees who do not have projects.


SELECT emp_name
FROM employees e
WHERE NOT EXISTS
(
SELECT *
FROM projects p
WHERE e.emp_id = p.emp_id
);

Q53. Find departments with no employees.


SELECT dept_name
FROM departments d
WHERE NOT EXISTS
(
SELECT *
FROM employees e
WHERE d.dept_id = e.dept_id
);

Q54. Find employees who did not receive bonuses.


SELECT emp_name
FROM employees e
WHERE NOT EXISTS
(
SELECT *
FROM bonuses b
WHERE e.emp_id = b.emp_id
);

Q55. Find projects that are not assigned to any employee.


SELECT project_name
FROM projects p
WHERE NOT EXISTS
(
SELECT *
FROM employees e
WHERE e.emp_id = p.emp_id
);

Interview-Level Mixed Questions

Q56. Find the second highest salary using subquery.


SELECT MAX(salary)
FROM employees
WHERE salary <
(
SELECT MAX(salary)
FROM employees
);

Q57. Find duplicate department assignments.


SELECT dept_id, COUNT(*)
FROM employees
GROUP BY dept_id
HAVING COUNT(*) > 1;

Q58. Find employees earning more than their managers.


SELECT a.emp_name
FROM employees a
JOIN employees b
ON a.manager_id = b.emp_id
WHERE [Link] > [Link];

Q59. Find departments with more than 2 employees.


SELECT dept_id, COUNT(*)
FROM employees
GROUP BY dept_id
HAVING COUNT(*) > 2;

Q60. Find managers who manage more than one


employee.
SELECT manager_id, COUNT(*)
FROM employees
GROUP BY manager_id
HAVING COUNT(*) > 1;

Section A — Text & String Functions

Q1. Display full names of employees by combining


first_name and last_name.
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;

Q2. Display customer name and city together in one


column.
SELECT CONCAT(customer_name, ' - ', city) AS customer_city
FROM customers;

Q3. Create an employee label like: EMP-1 Amit


SELECT CONCAT('EMP-', emp_id, ' ', first_name) AS employee_label
FROM employees;

Q4. Display all employee first names in uppercase.


SELECT UPPER(first_name)
FROM employees;

Q5. Display all customer names in lowercase.


SELECT LOWER(customer_name)
FROM customers;

Q6. Display department names in uppercase.


SELECT UPPER(department)
FROM employees;
Q7. Display first 3 characters of employee first names.
SELECT SUBSTRING(first_name, 1, 3)
FROM employees;

Q8. Display first 5 characters of customer emails.


SELECT SUBSTRING(email, 1, 5)
FROM customers;

Q9. Extract domain names from customer emails.


SELECT SUBSTRING(email, INSTR(email, '@') + 1)
FROM customers;

Q10. Replace department name IT with Technology.


SELECT REPLACE(department, 'IT', 'Technology')
FROM employees;

Q11. Replace .com with .org in customer emails.


SELECT REPLACE(email, '.com', '.org')
FROM customers;

Q12. Replace spaces in customer names with underscores.


SELECT REPLACE(customer_name, ' ', '_')
FROM customers;

Q13. Find length of each employee first name.


SELECT first_name, LENGTH(first_name)
FROM employees;

Q14. Display customer names with their character count.


SELECT customer_name, LENGTH(customer_name)
FROM customers;
Q15. Find employees whose first name length is greater
than 5.
SELECT *
FROM employees
WHERE LENGTH(first_name) > 5;

Q16. Remove extra spaces from customer names.


SELECT TRIM(customer_name)
FROM customers;

Q17. Trim spaces from department column values.


SELECT TRIM(department)
FROM employees;

Q18. Find position of letter a in employee first names.


SELECT first_name, INSTR(first_name, 'a')
FROM employees;

Q19. Find position of @ symbol in customer emails.


SELECT email, INSTR(email, '@')
FROM customers;

Q20. Find customers whose email contains gmail.


SELECT *
FROM customers
WHERE email LIKE '%gmail%';

Q21. Display employee names in format: AMIT_sharma


SELECT CONCAT(UPPER(first_name), '_', LOWER(last_name))
FROM employees;

Q22. Display first 2 letters of department names in


lowercase.
SELECT LOWER(SUBSTRING(department, 1, 2))
FROM employees;

Q23. Display employees whose first name starts with A.


SELECT *
FROM employees
WHERE first_name LIKE 'A%';

Q24. Display customers whose names end with Sharma.


SELECT *
FROM customers
WHERE customer_name LIKE '%Sharma';

Q25. Display employees whose names contain the letter r.


SELECT *
FROM employees
WHERE first_name LIKE '%r%';

Section B — Numeric & Date Functions

Q26. Round employee salaries to 0 decimal places.


SELECT ROUND(salary, 0)
FROM employees;

Q27. Round employee salaries to 1 decimal place.


SELECT ROUND(salary, 1)
FROM employees;

Q28. Display rounded average salary.


SELECT ROUND(AVG(salary), 0)
FROM employees;

Q29. Display current date.


SELECT CURDATE();

Q30. Display current time.


SELECT CURTIME();

Q31. Display current date and time together.


SELECT NOW();

Q32. Extract joining year of each employee.


SELECT first_name, YEAR(joining_date)
FROM employees;

Q33. Extract joining month of employees.


SELECT first_name, MONTH(joining_date)
FROM employees;

Q34. Find employees who joined in year 2021.


SELECT *
FROM employees
WHERE YEAR(joining_date) = 2021;

Q35. Find employees who joined in January.


SELECT *
FROM employees
WHERE MONTH(joining_date) = 1;

Q36. Find number of days each employee has worked till


today.
SELECT first_name,
DATEDIFF(CURDATE(), joining_date) AS working_days
FROM employees;
Q37. Find difference between today and employee joining
date.
SELECT DATEDIFF(CURDATE(), joining_date)
FROM employees;

Q38. Find employees who joined more than 1000 days ago.
SELECT *
FROM employees
WHERE DATEDIFF(CURDATE(), joining_date) > 1000;

Q39. Add 30 days to joining_date.


SELECT DATE_ADD(joining_date, INTERVAL 30 DAY)
FROM employees;

Q40. Display employee probation ending date after 90


days from joining_date.
SELECT first_name,
DATE_ADD(joining_date, INTERVAL 90 DAY) AS probation_end
FROM employees;

Q41. Add 1 year to employee joining_date.


SELECT DATE_ADD(joining_date, INTERVAL 1 YEAR)
FROM employees;

Q42. Display joining_date in format: DD-MM-YYYY


SELECT DATE_FORMAT(joining_date, '%d-%m-%Y')
FROM employees;

Q43. Display joining_date in format: Month DD, YYYY


SELECT DATE_FORMAT(joining_date, '%M %d, %Y')
FROM employees;

Q44. Display only month name from joining_date.


SELECT DATE_FORMAT(joining_date, '%M')
FROM employees;

Q45. Display employee names and years worked in


company.
SELECT first_name,
YEAR(CURDATE()) - YEAR(joining_date) AS years_worked
FROM employees;

Q46. Find employees whose salary rounded value is


greater than 50000.
SELECT *
FROM employees
WHERE ROUND(salary, 0) > 50000;

Q47. Find employees who joined in the current year.


SELECT *
FROM employees
WHERE YEAR(joining_date) = YEAR(CURDATE());

Q48. Display employee name with joining month name.


SELECT first_name,
DATE_FORMAT(joining_date, '%M') AS joining_month
FROM employees;

Section C — Set Operations

Q49. Display all customer names from old_customers and


new_customers without duplicates.
SELECT customer_name
FROM old_customers

UNION

SELECT customer_name
FROM new_customers;
Q50. Display all cities from customers table and another
table of your choice using UNION.
SELECT city
FROM customers

UNION

SELECT department
FROM employees;

Q51. Combine employee first names and customer names


into one result.
SELECT first_name AS names
FROM employees

UNION

SELECT customer_name
FROM customers;

Q52. Display all customer names from old_customers and


new_customers including duplicates.
SELECT customer_name
FROM old_customers

UNION ALL

SELECT customer_name
FROM new_customers;

Q53. Combine all employee departments using UNION


ALL from two queries.
SELECT department
FROM employees
WHERE salary > 50000

UNION ALL

SELECT department
FROM employees
WHERE salary <= 50000;
Q54. Display all employee and customer names including
repeated values.
SELECT first_name
FROM employees

UNION ALL

SELECT customer_name
FROM customers;

Q55. Find customers present in old_customers but not in


new_customers.
SELECT customer_name
FROM old_customers
WHERE NOT EXISTS
(
SELECT *
FROM new_customers
WHERE old_customers.customer_name = new_customers.customer_name
);

Q56. Find customers present in new_customers but not in


old_customers.
SELECT customer_name
FROM new_customers
WHERE NOT EXISTS
(
SELECT *
FROM old_customers
WHERE new_customers.customer_name = old_customers.customer_name
);

Q57. Find employees whose department does not exist in


another query result.
SELECT *
FROM employees e
WHERE NOT EXISTS
(
SELECT *
FROM employees
WHERE department = 'HR'
AND [Link] = department
);
Q58. Find customer names that are unique to
old_customers.
SELECT customer_name
FROM old_customers
WHERE customer_name NOT IN
(
SELECT customer_name
FROM new_customers
);

Mixed Interview Questions

Q59. Display employee initials. Example: A.S


SELECT CONCAT(
SUBSTRING(first_name, 1, 1),
'.',
SUBSTRING(last_name, 1, 1)
) AS initials
FROM employees;

Q60. Display employee email format: amit@[Link]


SELECT CONCAT(LOWER(first_name), '@[Link]') AS email
FROM employees;

Q61. Find employees whose names contain exactly 5


characters.
SELECT *
FROM employees
WHERE LENGTH(first_name) = 5;

Q62. Display employee names in reverse order


alphabetically.
SELECT first_name
FROM employees
ORDER BY first_name DESC;
Q63. Find employees who joined before 2022.
SELECT *
FROM employees
WHERE joining_date < '2022-01-01';

Q64. Display employees with salary rounded to nearest


thousand.
SELECT ROUND(salary, -3)
FROM employees;

Q65. Display all unique names from employees and


customers.
SELECT first_name AS names
FROM employees

UNION

SELECT customer_name
FROM customers;

SQL Aggregate Functions, Window


Functions & CTE/Stored Procedure
Practice Test
Tables
1. employees
emp_id emp_name department salary joining_date

1 Amit HR 45000 2022-01-15

2 Sneha IT 60000 2021-06-20

3 Rahul Finance 52000 2023-03-10

4 Priya IT 75000 2020-09-12

5 Arjun Marketing 40000 2024-01-05


emp_id emp_name department salary joining_date

6 Neha IT 60000 2021-11-25

7 Vikram HR 45000 2022-05-18

2. sales
sale_id emp_id region sales_amount sale_date

101 1 North 5000 2024-01-01

102 2 South 7000 2024-01-03

103 3 East 6500 2024-01-04

104 4 West 9000 2024-01-06

105 2 South 8500 2024-01-10

106 6 North 7200 2024-01-12

107 1 East 4000 2024-01-14

Section A — Aggregate Functions

Q1. Find the minimum salary in employees table.


SELECT MIN(salary)
FROM employees;

Q2. Find the maximum salary in employees table.


SELECT MAX(salary)
FROM employees;

Q3. Find the minimum sales amount.


SELECT MIN(sales_amount)
FROM sales;
Q4. Find the highest sales amount by region.
SELECT region, MAX(sales_amount)
FROM sales
GROUP BY region;

Q5. Find average employee salary.


SELECT AVG(salary)
FROM employees;

Q6. Find average sales amount.


SELECT AVG(sales_amount)
FROM sales;

Q7. Find average salary department-wise.


SELECT department, AVG(salary)
FROM employees
GROUP BY department;

Q8. Find average sales region-wise.


SELECT region, AVG(sales_amount)
FROM sales
GROUP BY region;

Q9. Find standard deviation of employee salaries.


SELECT STDDEV(salary)
FROM employees;

Q10. Find standard deviation of sales_amount.


SELECT STDDEV(sales_amount)
FROM sales;

Q11. Find department-wise salary standard deviation.


SELECT department, STDDEV(salary)
FROM employees
GROUP BY department;

Q12. Find variance of employee salaries.


SELECT VARIANCE(salary)
FROM employees;

Q13. Find variance of sales_amount.


SELECT VARIANCE(sales_amount)
FROM sales;

Q14. Find region-wise sales variance.


SELECT region, VARIANCE(sales_amount)
FROM sales
GROUP BY region;

Q15. Display all employee names department-wise in a


single row.
SELECT department,
GROUP_CONCAT(emp_name SEPARATOR ', ')
FROM employees
GROUP BY department;

Q16. Display all regions handled by each employee.


SELECT emp_id,
GROUP_CONCAT(region SEPARATOR ', ')
FROM sales
GROUP BY emp_id;

Q17. Display all employee names separated by commas.


SELECT GROUP_CONCAT(emp_name SEPARATOR ', ')
FROM employees;

Q18. Display all departments separated using |.


SELECT GROUP_CONCAT(department SEPARATOR ' | ')
FROM employees;

Q19. Display running total of salaries ordered by emp_id.


SELECT emp_id,
salary,
SUM(salary) OVER (ORDER BY emp_id) AS running_total
FROM employees;

Q20. Display cumulative sales by sale_date.


SELECT sale_date,
sales_amount,
SUM(sales_amount) OVER (ORDER BY sale_date) AS cumulative_sales
FROM sales;

Q21. Display department-wise cumulative salary.


SELECT department,
emp_name,
salary,
SUM(salary) OVER (PARTITION BY department ORDER BY salary) AS
cumulative_salary
FROM employees;

Q22. Display cumulative sales region-wise.


SELECT region,
sales_amount,
SUM(sales_amount) OVER (PARTITION BY region ORDER BY sales_amount)
AS cumulative_sales
FROM sales;

Q23. Find departments where average salary is greater


than 50000.
SELECT department, AVG(salary)
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;

Q24. Find regions where total sales exceed 10000.


SELECT region, SUM(sales_amount)
FROM sales
GROUP BY region
HAVING SUM(sales_amount) > 10000;

Q25. Find department with highest total salary.


SELECT department,
SUM(salary) AS total_salary
FROM employees
GROUP BY department
ORDER BY total_salary DESC
LIMIT 1;

Q26. Find employee count department-wise.


SELECT department,
COUNT(*) AS employee_count
FROM employees
GROUP BY department;

Q27. Find regions with more than 1 sale.


SELECT region,
COUNT(*) AS total_sales
FROM sales
GROUP BY region
HAVING COUNT(*) > 1;

Q28. Display department names along with max and min


salary.
SELECT department,
MAX(salary) AS max_salary,
MIN(salary) AS min_salary
FROM employees
GROUP BY department;

Q29. Find total sales done by each employee.


SELECT emp_id,
SUM(sales_amount) AS total_sales
FROM sales
GROUP BY emp_id;
Q30. Find employee whose sales amount is maximum.
SELECT emp_id, sales_amount
FROM sales
WHERE sales_amount =
(
SELECT MAX(sales_amount)
FROM sales
);

Section B — Window Functions

Q31. Assign row numbers to employees based on salary.


SELECT emp_name,
salary,
ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num
FROM employees;

Q32. Assign row numbers department-wise based on


salary.
SELECT emp_name,
department,
salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS
row_num
FROM employees;

Q33. Display latest sales with row numbers ordered by


sale_date.
SELECT sale_id,
sale_date,
ROW_NUMBER() OVER (ORDER BY sale_date DESC) AS row_num
FROM sales;

Q34. Rank employees based on salary.


SELECT emp_name,
salary,
RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees;
Q35. Rank employees within each department.
SELECT emp_name,
department,
salary,
RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS
dept_rank
FROM employees;

Q36. Rank sales_amount from highest to lowest.


SELECT sales_amount,
RANK() OVER (ORDER BY sales_amount DESC) AS sales_rank
FROM sales;

Q37. Assign dense rank to employees based on salary.


SELECT emp_name,
salary,
DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rank
FROM employees;

Q38. Assign dense rank region-wise based on


sales_amount.
SELECT region,
sales_amount,
DENSE_RANK() OVER (PARTITION BY region ORDER BY sales_amount DESC)
AS dense_rank
FROM sales;

Q39. Divide employees into 4 salary groups using NTILE.


SELECT emp_name,
salary,
NTILE(4) OVER (ORDER BY salary DESC) AS salary_group
FROM employees;

Q40. Divide sales records into 3 buckets.


SELECT sale_id,
sales_amount,
NTILE(3) OVER (ORDER BY sales_amount DESC) AS bucket
FROM sales;
Q41. Display previous employee salary using LAG.
SELECT emp_name,
salary,
LAG(salary, 1, 0) OVER (ORDER BY salary) AS previous_salary
FROM employees;

Q42. Display previous sales amount based on sale_date.


SELECT sale_date,
sales_amount,
LAG(sales_amount, 1, 0) OVER (ORDER BY sale_date) AS previous_sales
FROM sales;

Q43. Find salary difference between current and previous


employee.
SELECT emp_name,
salary,
salary - LAG(salary, 1, 0) OVER (ORDER BY salary) AS
salary_difference
FROM employees;

Q44. Display next employee salary using LEAD.


SELECT emp_name,
salary,
LEAD(salary, 1, 0) OVER (ORDER BY salary) AS next_salary
FROM employees;

Q45. Display next sales amount.


SELECT sales_amount,
LEAD(sales_amount, 1, 0) OVER (ORDER BY sale_date) AS next_sales
FROM sales;

Q46. Find future sales trend using LEAD.


SELECT sale_date,
sales_amount,
LEAD(sales_amount, 1, 0) OVER (ORDER BY sale_date) AS future_sales
FROM sales;

Q47. Display first salary value in salary order.


SELECT emp_name,
salary,
FIRST_VALUE(salary) OVER (ORDER BY salary DESC) AS highest_salary
FROM employees;

Q48. Display first sales amount by region.


SELECT region,
sales_amount,
FIRST_VALUE(sales_amount) OVER (PARTITION BY region ORDER BY
sales_amount DESC) AS highest_sales
FROM sales;

Q49. Display last salary value.


SELECT emp_name,
salary,
LAST_VALUE(salary) OVER (
ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS last_salary
FROM employees;

Q50. Display last sales value region-wise.


SELECT region,
sales_amount,
LAST_VALUE(sales_amount) OVER (
PARTITION BY region
ORDER BY sales_amount
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS last_sales
FROM sales;

Q51. Calculate cumulative distribution of employee


salaries.
SELECT emp_name,
salary,
CUME_DIST() OVER (ORDER BY salary) AS cumulative_distribution
FROM employees;

Q52. Calculate cumulative distribution of sales_amount.


SELECT sales_amount,
CUME_DIST() OVER (ORDER BY sales_amount) AS cumulative_distribution
FROM sales;
Q53. Find percent rank of employee salaries.
SELECT emp_name,
salary,
PERCENT_RANK() OVER (ORDER BY salary) AS percent_rank
FROM employees;

Q54. Find percent rank of sales amounts.


SELECT sales_amount,
PERCENT_RANK() OVER (ORDER BY sales_amount) AS percent_rank
FROM sales;

Q55. Find median salary using PERCENTILE_CONT.


SELECT PERCENTILE_CONT(0.5)
WITHIN GROUP (ORDER BY salary)
FROM employees;

Q56. Find median sales_amount using


PERCENTILE_DISC.
SELECT PERCENTILE_DISC(0.5)
WITHIN GROUP (ORDER BY sales_amount)
FROM sales;

Q57. Display 2nd highest salary using NTH_VALUE.


SELECT emp_name,
salary,
NTH_VALUE(salary, 2) OVER (
ORDER BY salary DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS second_highest
FROM employees;

Q58. Display 3rd sales amount value.


SELECT sale_id,
sales_amount,
NTH_VALUE(sales_amount, 3) OVER (
ORDER BY sales_amount DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS third_sales
FROM sales;

Q59. Find top 3 highest paid employees using RANK.


SELECT *
FROM
(
SELECT emp_name,
salary,
RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees
) ranked
WHERE salary_rank <= 3;

Q60. Find lowest sales record in each region.


SELECT *
FROM
(
SELECT region,
sales_amount,
ROW_NUMBER() OVER (
PARTITION BY region
ORDER BY sales_amount
) AS rn
FROM sales
) ranked
WHERE rn = 1;

Q61. Find employees earning more than previous


employee salary.
SELECT emp_name,
salary
FROM
(
SELECT emp_name,
salary,
LAG(salary) OVER (ORDER BY salary) AS prev_salary
FROM employees
) t
WHERE salary > prev_salary;

Q62. Display running total sales by region.


SELECT region,
sales_amount,
SUM(sales_amount) OVER (
PARTITION BY region
ORDER BY sale_date
) AS running_total
FROM sales;

Section C — Stored Procedures

Q63. Create a stored procedure to display all employees.


CREATE PROCEDURE GetEmployees()
BEGIN
SELECT * FROM employees;
END;

Q64. Create a stored procedure to fetch employees by


department.
CREATE PROCEDURE GetEmployeesByDepartment(IN dept_name VARCHAR(50))
BEGIN
SELECT *
FROM employees
WHERE department = dept_name;
END;

Q65. Create a stored procedure to insert a new employee.


CREATE PROCEDURE AddEmployee(
IN p_emp_id INT,
IN p_emp_name VARCHAR(100),
IN p_department VARCHAR(50),
IN p_salary INT
)
BEGIN
INSERT INTO employees(emp_id, emp_name, department, salary)
VALUES(p_emp_id, p_emp_name, p_department, p_salary);
END;

Q66. Create a stored procedure to display average salary.


CREATE PROCEDURE AverageSalary()
BEGIN
SELECT AVG(salary) AS avg_salary
FROM employees;
END;
Q67. Create a stored procedure to display total sales by
employee.
CREATE PROCEDURE TotalSalesByEmployee()
BEGIN
SELECT emp_id,
SUM(sales_amount) AS total_sales
FROM sales
GROUP BY emp_id;
END;

Section D — CTE (Common Table


Expressions)

Q68. Create a CTE to display employees with salary


greater than 50000.
WITH high_salary AS
(
SELECT *
FROM employees
WHERE salary > 50000
)
SELECT *
FROM high_salary;

Q69. Create a CTE for department-wise average salary.


WITH dept_avg AS
(
SELECT department,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department
)
SELECT *
FROM dept_avg;

Q70. Use CTE to find highest paid employee.


WITH ranked_salary AS
(
SELECT emp_name,
salary,
RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
)
SELECT *
FROM ranked_salary
WHERE rnk = 1;

Q71. Use CTE to rank employees by salary.


WITH employee_rank AS
(
SELECT emp_name,
salary,
RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM employees
)
SELECT *
FROM employee_rank;

Q72. Use CTE to display total sales by region.


WITH region_sales AS
(
SELECT region,
SUM(sales_amount) AS total_sales
FROM sales
GROUP BY region
)
SELECT *
FROM region_sales;

Q73. Use CTE with ROW_NUMBER to find latest sales


record.
WITH latest_sales AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY sale_date DESC) AS rn
FROM sales
)
SELECT *
FROM latest_sales
WHERE rn = 1;

Q74. Use recursive CTE to generate numbers from 1 to 10.


WITH RECURSIVE numbers AS
(
SELECT 1 AS num

UNION ALL
SELECT num + 1
FROM numbers
WHERE num < 10
)
SELECT *
FROM numbers;

Q75. Use CTE to find employees whose salary is above


department average.
WITH dept_avg AS
(
SELECT department,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department
)
SELECT e.emp_name,
[Link],
[Link]
FROM employees e
JOIN dept_avg d
ON [Link] = [Link]
WHERE [Link] > d.avg_salary;

Bonus Interview Theory Answers

Q76. Difference between RANK and DENSE_RANK


RANK DENSE_RANK

Skips rank numbers on ties Does not skip ranks

Q77. Difference between ROW_NUMBER and RANK


ROW_NUMBER RANK

Gives unique number to each row Same values get same rank

Q78. What is a window function?


Window functions perform calculations across a set of rows related to the current row
without collapsing rows.

Q79. What is the purpose of PARTITION BY?


PARTITION BY divides data into groups for window function calculations.

Q80. Difference between CTE and Subquery


CTE Subquery

More readable and reusable Written inside main query

SQL Practice Test Paper — Conditional


Expressions, Users, Permissions & Backup
Database Scenario
You are working in a company HR + payroll system.

Use the following tables for all questions.

Tables
1. employees
emp_id emp_name salary bonus department status

1 Amit 45000 5000 HR NULL

2 Sneha 60000 NULL IT Active

3 Rahul 52000 4000 Finance Active

4 Priya 75000 8000 IT NULL

5 Arjun 40000 NULL Marketing Inactive


2. users
user_id username email city

101 admin admin@[Link] Delhi

102 analyst NULL Mumbai

103 hr_user hr@[Link] NULL

Section A — Conditional Expressions

Q1. High/Low Salary using IF


SELECT emp_name,
IF(salary > 50000, 'High Salary', 'Low Salary') AS salary_status
FROM employees;

Q2. Bonus status using IF


SELECT emp_name,
IF(bonus IS NOT NULL, 'Has Bonus', 'No Bonus') AS bonus_status
FROM employees;

Q3. Active / Inactive using IF


SELECT emp_name,
IF(status = 'Active', 'Active', 'Inactive') AS emp_status
FROM employees;

Q4. Simple CASE — salary category


SELECT emp_name,
CASE
WHEN salary > 70000 THEN 'Top Performer'
ELSE 'Normal'
END AS category
FROM employees;

Q5. Simple CASE — department mapping


SELECT emp_name,
CASE department
WHEN 'IT' THEN 'Tech'
WHEN 'HR' THEN 'Human Resource'
ELSE 'Other'
END AS dept_category
FROM employees;

Q6. Searched CASE — grading


SELECT emp_name,
CASE
WHEN salary >= 70000 THEN 'A Grade'
WHEN salary >= 50000 THEN 'B Grade'
ELSE 'C Grade'
END AS grade
FROM employees;

Q7. Bonus classification


SELECT emp_name,
CASE
WHEN bonus > 5000 THEN 'High Bonus'
WHEN bonus BETWEEN 1000 AND 5000 THEN 'Medium Bonus'
ELSE 'No Bonus'
END AS bonus_type
FROM employees;

Q8. Income risk level


SELECT emp_name,
CASE
WHEN salary < 45000 THEN 'Low Income'
WHEN salary BETWEEN 45000 AND 65000 THEN 'Mid Income'
ELSE 'High Income'
END AS income_level
FROM employees;

Q9. COALESCE — replace NULL bonus


SELECT emp_name,
COALESCE(bonus, 0) AS bonus
FROM employees;

Q10. COALESCE — email default


SELECT username,
COALESCE(email, 'noemail@[Link]') AS email
FROM users;

Q11. COALESCE — status default


SELECT emp_name,
COALESCE(status, 'Unknown') AS status
FROM employees;

Q12. IFNULL — bonus default


SELECT emp_name,
IFNULL(bonus, 1000) AS bonus
FROM employees;

Q13. IFNULL — email default


SELECT username,
IFNULL(email, 'missing@[Link]') AS email
FROM users;

Q14. IFNULL — department default


SELECT emp_name,
IFNULL(department, 'Unassigned') AS department
FROM employees;

Q15. NULLIF — salary 40000 becomes NULL


SELECT emp_name,
NULLIF(salary, 40000) AS salary
FROM employees;

Q16. NULLIF — department HR becomes NULL


SELECT emp_name,
NULLIF(department, 'HR') AS department
FROM employees;

Q17. NULLIF — bonus = 0 becomes NULL


SELECT emp_name,
NULLIF(bonus, 0) AS bonus
FROM employees;

Q18. Replace NULL bonus with 0


SELECT emp_name,
IFNULL(bonus, 0) AS bonus
FROM employees;

Q19. Email or fallback to city


SELECT username,
COALESCE(email, city) AS contact
FROM users;

Q20. Salary = 0 → NULL


SELECT emp_name,
NULLIF(salary, 0) AS salary
FROM employees;

Section B — User & Permissions

Q21. Create user


CREATE USER 'analyst'@'localhost' IDENTIFIED BY '1234';

Q22. Grant privileges


GRANT ALL PRIVILEGES ON company_db.* TO 'analyst'@'localhost';

Q23. Revoke privileges


REVOKE ALL PRIVILEGES ON company_db.* FROM 'analyst'@'localhost';

Q24. Change password


ALTER USER 'analyst'@'localhost' IDENTIFIED BY 'newpass123';
Q25. Show grants
SHOW GRANTS FOR 'analyst'@'localhost';

Q26. Drop user


DROP USER 'analyst'@'localhost';

Q27. Flush privileges


FLUSH PRIVILEGES;

Section C — Backup & Recovery

Q28. Backup database


mysqldump -u username -p company_db > [Link]

Q29. Restore database


mysql -u username -p company_db < [Link]

Q30. What does mysqldump do?


It creates a backup of a database in SQL format that can be restored later.

Q31. Why backup is important?


To prevent data loss due to crashes, corruption, or accidental deletion.

Section D — Theory Answers


Q32. IF vs CASE
IF CASE

Simple condition Multiple conditions

MySQL specific Standard SQL

Q33. COALESCE vs IFNULL


COALESCE IFNULL

Multiple values Only 2 values

Standard SQL MySQL specific

Q34. If all COALESCE values are NULL


Result will be NULL.

Q35. Why NULLIF is used?


To return NULL when two values are equal.

Q36. What is privilege?


Permission given to a user to perform actions on database.

Q37. GRANT vs REVOKE


GRANT REVOKE

Gives permission Removes permission


Q38. Can we restore without backup?
No, backup is required for recovery.

Q39. FLUSH PRIVILEGES purpose


Reloads privilege tables so changes take effect immediately.

Q40. IF vs CASE which is better?

You might also like