Top 30 SQL Interview Questions and Answers

0
80
SQL Interview Questions

Are you seriously preparing for data related roles and wants to ace interview?. Then this blog really helps you to prepare highly asked Language in such roles that is SQL. Whether you are applying for a Data Analyst, Database Administrator or Software Developer roles, SQL requires to basically testing your skills.

In this blog we will be talking about the top 30 SQL interview questions and answers in detail so that you will fully prepared for your next interview. These questions varies and starting for the beginners and goes for the experienced too. So, Let us begin this blog to start with your interview journey.

What is SQL?

SQL stands for Structured Query Language. SQL queries used by many databases and it is helpful in performing several tasks such as updating, deletion, retrieve or many more CRUD operations. There are different databases like MySQL, PostgreSQL, Oracle and Microsoft SQL Server.

SQL Interview Questions

Top 30 SQL Interview Questions and Answers

In this section we will see different SQL interview questions that is categorized into beginner -level, intermediate-level and advanced-level SQL interview questions.

Beginner-Level SQL Interview Questions

What is the difference between SQL and MySQL?

SQL is a language used to manage and query relational databases. MySQL is an open-source RDBMS that uses SQL as its standard language.

SQL Interview Questions

What are the different types of SQL Commands?

DDL (Data Definition Language): CREATE, DROP, ALTER

DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE

DCL (Data Control Language): GRANT, REVOKE

TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT

What is a primary key?

A primary key uniquely identifies each record in a table. It cannot contain NULL values and must contain unique values.

What is a foreign key?

A foreign key is a field in one table that refers to the primary key in another table, establishing a relationship between the two.

What is the difference between WHERE and HAVING clause?

WHERE filters rows before grouping.

HAVING filters groups after the aggregation is done.

What is a JOIN? Name different types.

A JOIN is used to combine rows from two or more tables based on a related column.

  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • CROSS JOIN

What is the difference between INNER JOIN and LEFT JOIN?

INNER JOIN returns only matching rows.

LEFT JOIN returns all rows from the left table and matched rows from the right.

What is normalization?

Normalization is the process of organizing data to minimize redundancy and improve data integrity.

What is denormalization?

Denormalization is the process of adding redundant data to improve read performance.

What are the different normal forms?

1NF: Atomic columns

2NF: No partial dependency

3NF: No transitive dependency

BCNF: Enhanced 3NF

Intermediate-Level SQL Interview Questions

employee_idnamedepartment_idsalaryemailjoining_date
101Alice Johnson175000alice.johnson@abc.com2024-09-12
102Bob Smith260000bob.smith@abc.com2023-12-01
103Charlie Lee180000charlie.lee@abc.com2025-02-15
104Diana Prince390000diana.prince@abc.com2022-07-21
105Edward Cullen260000edward.cullen@abc.com2025-03-05
106Fiona Watson185000fiona.watson@abc.com2025-04-10
107George Martin395000george.martin@abc.com2024-06-25
108Helen Carter260000helen.carter@abc.com2023-11-10
109Ivan Wright391000ivan.wright@abc.com2025-01-12
110Jane Doe180000jane.doe@abc.com2025-05-20

Write a query to find the second highest salary from the Employee table.

SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary < (SELECT MAX(salary) FROM Employee);

Write a query to display each department’s name with the total number of employees in it.

SELECT d.department_name, COUNT(e.employee_id) AS total_employees
FROM Employee e
JOIN Department d ON e.department_id = d.department_id
GROUP BY d.department_name;

Write a query to find employees who joined in the last 3 months.

SELECT *
FROM Employee
WHERE joining_date >= DATE_SUB(CURDATE(), INTERVAL 3 MONTH);

Write a query to fetch duplicate email records from the Users table.

SELECT email, COUNT(*) AS count
FROM Users
GROUP BY email
HAVING COUNT(*) > 1;

Write a query to find the top 3 departments with the highest average salary.

SELECT d.department_name, AVG(e.salary) AS avg_salary
FROM Employee e
JOIN Department d ON e.department_id = d.department_id
GROUP BY d.department_name
ORDER BY avg_salary DESC
LIMIT 3;

Write a query to fetch employee names that start and end with a vowel.

SELECT name
FROM Employee
WHERE name REGEXP '^[aeiouAEIOU].*[aeiouAEIOU]$';

Write a query to calculate the running total of sales per day.

SELECT sale_date, amount,
       SUM(amount) OVER (ORDER BY sale_date) AS running_total
FROM Sales;

Write a query to find customers who placed more than two orders in the last 30 days.

SELECT customer_id, COUNT(order_id) AS order_count
FROM Orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
GROUP BY customer_id
HAVING COUNT(order_id) > 2;

Write a query to pivot monthly sales data into columns (Jan, Feb, Mar, …).

SELECT product_id,
       SUM(CASE WHEN MONTH(sale_date) = 1 THEN amount ELSE 0 END) AS Jan,
       SUM(CASE WHEN MONTH(sale_date) = 2 THEN amount ELSE 0 END) AS Feb,
       SUM(CASE WHEN MONTH(sale_date) = 3 THEN amount ELSE 0 END) AS Mar
FROM Sales
GROUP BY product_id;

Write a query to find employees earning more than the average salary in their department.

SELECT e.employee_id, e.name, e.salary, e.department_id
FROM Employee e
WHERE salary > (
    SELECT AVG(salary)
    FROM Employee
    WHERE department_id = e.department_id
);

Advanced-Level SQL Interview Questions

No.Advanced SQL Interview Question
1How do you optimize a slow-running SQL query in a large dataset?
2Write a query using a recursive Common Table Expression (CTE) to find the factorial of a number.
3How would you write a query to detect gaps in a sequence of IDs in a table?
4Explain and demonstrate the use of window functions like LEAD() and LAG() with an example.
5How do you pivot and unpivot data in SQL without using built-in PIVOT/UNPIVOT operators?
6Write a query to rank items within categories and retrieve the top 2 in each group.
7How do you handle performance tuning in SQL when working with joins on large tables?
8Write a query to compare two tables and find mismatched records across all columns.
9What are materialized views and how do they differ from regular views? When would you use them?
10How do you implement Slowly Changing Dimensions (Type 2) in SQL for a data warehouse system?

Why SQL Interview Questions Are Important for Data Roles?

SQL is essential in any data-focused position, whether you are a data analyst, data engineer, business intelligence developer, or data scientist. SQL questions in interviews aim to evaluate your proficiency in efficiently retrieving, manipulating, and analyzing data through practical business scenarios.

How Non-Tech People Prepare For SQL Interview Questions?

Non-technical professionals can successfully prepare for SQL interview questions by starting with the basics and using real-world examples to grasp core concepts. Since they may not have a programming background, it’s best to begin with understanding what databases are, how tables are structured, and how data is retrieved using simple SELECT statements.

Free platforms and YouTube tutorials offer beginner-friendly SQL lessons. Once comfortable, they can move on to practice platforms like LeetCode (Easy level), HackerRank.

Conclusion

SQL is an important skill for anyone working with data, whether you are aiming for a role as a data analyst, engineer, or even a non-tech professional entering to the data field. So as to mastering SQL interview questions not only improves your ability to work with databases but also boosts your confidence during technical interviews. From basic queries to advanced concepts like window functions and performance tuning, consistent practice and real world problem solving are key for anyone. No matter your background is with the right preparation and mindset you can crack any SQL interview and take a step closer to your data career goals.