
SQL Interview Questions: Stepping into the world of data science can be as thrilling as it is daunting. "The most important thing in a data science career is to have a good mentor," echoes across the industry, speaking to the heart of every aspiring data analyst and engineer who has faced the formidable task of prepping for an SQL interview.
Whether you’re a fresh graduate, a seasoned veteran polishing your skills, or somewhere in between, having the right resources can make all the difference. This SQL Interview Questions CHEAT SHEET for 2025 is your secret weapon, crafted to turn the tide in your favor, illuminating complex concepts and common queries you're destined to encounter.
So, let’s embark on this journey together—where each question unveils a fragment of the vast universe that is SQL, preparing you not just to answer with confidence, but to inspire awe with your depth of understanding.
Whether you're a job seeker seeking to apply your SQL skills to new opportunities or a hiring manager preparing to interview candidates for a job opening in your company, it's crucial to be well-versed in common SQL interview questions and their corresponding answers.
1) What does SQL stand for?
Answer: d) Structured Query Language
2) Which SQL statement is used to retrieve data from a database?
Answer: a) SELECT
3) In SQL, what is the purpose of the WHERE clause?
Answer: a) To filter the result set based on conditions
4) Which SQL command is used to insert new data into a database?
Answer: b) INSERT
5) What is the primary key in a relational database?
Answer: c) A unique identifier for a record in a table
6) Which SQL clause is used to sort the result set?
Answer: b) ORDER BY
7) What type of join returns all rows when there is a match in one of the tables?
Answer: a) INNER JOIN
8) Which SQL function is used to find the total number of rows in a table?
Answer: a) COUNT(*)
9) What does the SQL acronym "ACID" stand for in the context of database transactions?
Answer: a) Atomicity, Consistency, Isolation, Durability
10) Which SQL command is used to remove all data from a table without removing the table itself?
Answer: b) TRUNCATE
These multiple-choice questions cover various aspects of SQL and are designed to test your knowledge of fundamental SQL concepts.1) Find the Second Highest Salary:
SELECT MAX(salary) AS SecondHighestSalary FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);2) Get Nth Highest Salary:
SELECT DISTINCT salary FROM employees e1 WHERE N = (SELECT COUNT(DISTINCT salary) FROM employees e2 WHERE e1.salary <= e2.salary);3) Calculate Running Total:
SELECT date, amount, SUM(amount) OVER (ORDER BY date) AS RunningTotal FROM transactions;4) Pivot Table:
SELECT MAX(CASE WHEN product = 'A' THEN revenue END) AS A_Revenue, MAX(CASE WHEN product = 'B' THEN revenue END) AS B_Revenue, MAX(CASE WHEN product = 'C' THEN revenue END) AS C_Revenue FROM sales;5) Find Duplicate Rows:
SELECT column1, column2, COUNT(*) FROM table_name GROUP BY column1, column2 HAVING COUNT(*) > 1;6) Ranking with Ties:
SELECT emp_name, salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS Rank FROM employees;7) Generate Fibonacci Sequence:
SELECT n, ROUND((POWER((1 + SQRT(5)) / 2, n) - POWER((1 - SQRT(5)) / 2, n)) / SQRT(5)) AS Fibonacci FROM numbers;8) Find Missing Number in Sequence:
SELECT DISTINCT (a.id + 1) AS MissingNumber FROM numbers a LEFT JOIN numbers b ON a.id + 1 = b.id WHERE b.id IS NULL; These queries cover a range of SQL concepts and are designed to assess problem-solving skills during interviews.1) What is SQL?
2) Explain the difference between SQL and MySQL.
3) What is a database?
4) What is a primary key?
5) What is the difference between CHAR and VARCHAR data types?
1) Write an SQL query to retrieve all the columns from a table named "employees."
SELECT * FROM employees;2) What is the purpose of the GROUP BY clause in SQL?
3) Explain the difference between INNER JOIN and LEFT JOIN.
4) Write an SQL query to calculate the average salary of employees in a table named "salaries."
SELECT AVG(salary) FROM salaries;5) Explain the purpose of the HAVING clause in SQL.
1) What is a subquery?
2) Write an SQL query to find the second-highest salary from a table named "employees."
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);3) Explain the concept of indexing in databases.
4) What is a stored procedure?
5) Write an SQL query to find the nth highest salary from a table named "salaries."
SELECT salary FROM salaries ORDER BY salary DESC LIMIT 1 OFFSET n-1; (Replace 'n' with the desired rank, e.g., 2nd highest salary)Also Read: Top 50 Maven Interview Questions and Answers for 2025
1) What is MySQL?
MySQL is an open-source relational database management system (RDBMS) that uses SQL (Structured Query Language) for managing and manipulating the data in the database.2) Explain the difference between MyISAM and InnoDB storage engines.
MyISAM is a non-transactional storage engine, while InnoDB is a transactional storage engine. InnoDB supports ACID properties and is more suitable for applications that require transactions and data integrity.3) How do you create a new database in MySQL?
CREATE DATABASE database_name;4) What is the purpose of the MySQL SELECT statement?
The SELECT statement is used to retrieve data from one or more tables in a MySQL database.5) How do you retrieve all columns from a table named "employees"?
SELECT * FROM employees;1) Explain the concept of MySQL Index and its types.
An index in MySQL is a data structure that improves the speed of data retrieval operations on a database table. Types of indexes include PRIMARY KEY, UNIQUE, INDEX, and FULLTEXT.2) Write a MySQL query to update the salary of an employee with the ID of 101.
UPDATE employees SET salary = 60000 WHERE employee_id = 101;3) What is a MySQL Stored Procedure?
A stored procedure is a set of SQL statements that can be stored in the MySQL database and executed with a single call.4) Explain the purpose of the MySQL JOIN clause.
The JOIN clause is used to combine rows from two or more tables based on a related column between them.5) How do you find the total number of rows in a table in MySQL?
SELECT COUNT(*) FROM table_name;1) Explain the difference between INNER JOIN and LEFT JOIN.
INNER JOIN returns only the matching rows between the tables, while LEFT JOIN returns all rows from the left table and the matching rows from the right table.2) What is MySQL partitioning, and when is it used?
Partitioning in MySQL involves splitting large tables into smaller, more manageable pieces. It is used to improve query performance and manageability.3) Write a MySQL query to find the second-highest salary from an "employees" table.
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);4) Explain the purpose of the MySQL TRIGGER.
A trigger in MySQL is a set of instructions that are automatically executed ("triggered") in response to certain events on a particular table or view.5) How do you perform a backup and restore in MySQL?
To perform a backup: mysqldump -u username -p database_name > backup.sql To perform a restore: mysql -u username -p database_name < backup.sql These MySQL interview questions cover both fundamental and advanced concepts that are relevant for candidates with various levels of experience working with MySQL databases.Also Read: 20+ UI Developer Interview Questions and Answers
1) What is normalization, and why is it important in databases?
2) Explain the differences between UNION and UNION ALL in SQL.
3) What is the purpose of the SQL WHERE clause?
4) What is an SQL View?
5) Explain the concept of an SQL Index.
1) Write an SQL query to find the third-highest salary from an "employees" table.
SELECT MAX(salary) FROM employees WHERE salary NOT IN (SELECT MAX(salary) FROM employees UNION SELECT MIN(salary) FROM employees);2) Explain the differences between INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN.
3) What is the purpose of the SQL GROUP_CONCAT function?
4) Explain the ACID properties in the context of database transactions.
5) Write an SQL query to calculate the total number of orders for each customer from an "orders" table.
SELECT customer_id, COUNT(order_id) AS total_orders FROM orders GROUP BY customer_id;1) Explain the purpose of the SQL CASE statement.
2) What is a recursive SQL query, and when is it used?
3) Write an SQL query to find duplicate rows in a table.
SELECT column1, column2, COUNT(*) FROM table_name GROUP BY column1, column2 HAVING COUNT(*) > 1;4) Explain the concept of database sharding.
5) What are SQL Window Functions? Provide an example.