SQL (Structured Query Language) is a powerful tool for managing and manipulating databases. Whether you’re a beginner just starting out or an advanced user looking to brush up on your skills, here are the top 50 SQL interview questions and answers to help you prepare for your next interview.
What is SQL?
What are the different types of SQL commands?
CREATE
, ALTER
, DROP
INSERT
, UPDATE
, DELETE
GRANT
, REVOKE
COMMIT
, ROLLBACK
What is a primary key?
CREATE TABLE Employees (ID INT PRIMARY KEY, Name VARCHAR(50));
What is a foreign key?
CREATE TABLE Orders (OrderID INT, CustomerID INT, FOREIGN KEY (CustomerID) REFERENCES Customers(ID));
What is a JOIN?
SELECT Customers.Name, Orders.OrderID FROM Customers INNER JOIN Orders ON Customers.ID = Orders.CustomerID;
What are the different types of JOINs?
What is a SELECT statement?
SELECT * FROM Customers;
How do you filter records in SQL?
SELECT * FROM Employees WHERE Department = 'Sales';
What is the difference between WHERE and HAVING?
SELECT Department, COUNT(*) FROM Employees GROUP BY Department HAVING COUNT(*) > 10;
What is an alias in SQL?
SELECT Name AS EmployeeName FROM Employees;
How do you sort data in SQL?
SELECT * FROM Customers ORDER BY Name ASC;
What is a subquery?
SELECT * FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees);
What is a UNION?
SELECT Name FROM Customers UNION SELECT Name FROM Suppliers;
What is the difference between UNION and UNION ALL?
SELECT Name FROM Customers UNION ALL SELECT Name FROM Suppliers;
What is a VIEW?
CREATE VIEW EmployeeView AS SELECT Name, Department FROM Employees;
What is a stored procedure?
CREATE PROCEDURE GetEmployeeData AS SELECT * FROM Employees;
What is a function in SQL?
CREATE FUNCTION GetEmployeeCount() RETURNS INT AS BEGIN RETURN (SELECT COUNT(*) FROM Employees); END;
What is the difference between a stored procedure and a function?
What is normalization?
What is denormalization?
What is a transaction in SQL?
BEGIN TRANSACTION; UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 1; COMMIT;
What is ACID property?
How do you implement error handling in SQL?
BEGIN TRY; -- SQL statements END TRY BEGIN CATCH; -- Error handling END CATCH;
What is a trigger in SQL?
CREATE TRIGGER trgAfterInsert ON Employees AFTER INSERT AS BEGIN PRINT 'Record Inserted'; END;
What are indexes in SQL?
CREATE INDEX idx_name ON Employees (Name);
What is a clustered index?
CREATE CLUSTERED INDEX idx_EmployeeID ON Employees (EmployeeID);
What is a non-clustered index?
CREATE INDEX idx_Department ON Employees (Department);
How do you optimize a SQL query?
What is an execution plan?
EXPLAIN SELECT * FROM Employees;
What is a composite key?
CREATE TABLE Orders (OrderID INT, ProductID INT, PRIMARY KEY (OrderID, ProductID));
What is the difference between DELETE and TRUNCATE?
What is a cursor in SQL?
DECLARE cursor_name CURSOR FOR SELECT Name FROM Employees;
How do you create a recursive query?
WITH RECURSIVE EmployeeCTE AS (SELECT EmployeeID, ManagerID FROM Employees WHERE ManagerID IS NULL UNION ALL SELECT e.EmployeeID, e.ManagerID FROM Employees e INNER JOIN EmployeeCTE ecte ON e.ManagerID = ecte.EmployeeID) SELECT * FROM EmployeeCTE;
What is a window function?
SELECT Name, Salary, RANK() OVER (ORDER BY Salary DESC) FROM Employees;
What is the difference between RANK() and DENSE_RANK()?
SELECT Name, Salary, RANK() OVER (ORDER BY Salary DESC) AS Rank, DENSE_RANK() OVER (ORDER BY Salary DESC) AS DenseRank FROM Employees;
What is the COALESCE function?
SELECT COALESCE(NULL, 'First', 'Second');
What is a CASE statement in SQL?
SELECT Name, Salary, CASE WHEN Salary < 50000 THEN 'Low' WHEN Salary BETWEEN 50000 AND 100000 THEN 'Medium' ELSE 'High' END AS SalaryRange FROM Employees;
What is the difference between IN and EXISTS?
SELECT Name FROM Employees WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Location = 'New York');
What is the difference between a temporary table and a table variable?
CREATE TABLE #TempTable (ID INT, Name VARCHAR(50)); DECLARE @TableVar TABLE (ID INT, Name VARCHAR(50));
What is a CTE (Common Table Expression)?
WITH EmployeeCTE AS (SELECT EmployeeID, Name FROM Employees WHERE DepartmentID = 1) SELECT * FROM EmployeeCTE;
How do you handle NULL values in SQL?
SELECT * FROM Employees WHERE ManagerID IS NULL;
What is the difference between CHAR and VARCHAR?
CREATE TABLE Test (FixedChar CHAR(10), VariableChar VARCHAR(10));
What is the GROUP BY clause used for?
SELECT Department, COUNT(*) FROM Employees GROUP BY Department;
What is the difference between HAVING and WHERE?
SELECT Department, COUNT(*) FROM Employees GROUP BY Department HAVING COUNT(*) > 10;
What is a self-join?
SELECT A.Name AS Employee, B.Name AS Manager FROM Employees A INNER JOIN Employees B ON A.ManagerID = B.EmployeeID;
What is the difference between a LEFT JOIN and a RIGHT JOIN?
SELECT * FROM Employees LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
What are aggregate functions?
SELECT AVG(Salary), COUNT(*), MAX(Salary), MIN(Salary), SUM(Salary) FROM Employees;
What is the difference between COUNT(*) and COUNT(column)?
SELECT COUNT(*) FROM Employees; SELECT COUNT(Salary) FROM Employees;
What is the difference between an INNER JOIN and an OUTER JOIN?
SELECT * FROM Employees INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
What is the difference between ROW_NUMBER() and RANK()?
SELECT Name, Salary, ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNumber, RANK() OVER (ORDER BY Salary DESC) AS Rank FROM Employees;
These questions cover a broad range of SQL topics, from basic concepts to more advanced techniques. Whether you’re preparing for an interview or looking to deepen your SQL knowledge, these questions and answers provide a solid foundation. Happy studying!