Top SQL Interview Questions With Solutions
Top SQL Interview Questions With Solutions
IN
○ DQL (Data Query Language): SELECT
○ DCL (Data Control Language): GRANT, REVOKE
○ TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
3. Q: What is a primary key?
A primary key is a column or combination of columns that uniquely identifies each row in
a table.
JA
○ It must contain unique values.
○ It cannot contain NULL.
4. Q: What is the difference between WHERE and HAVING?
○ WHERE filters rows before grouping is applied.
○ HAVING filters groups after GROUP BY is applied.
A
5. Q: What are the differences between SQL and NoSQL databases?
○ SQL: Uses structured schema (tables with rows and columns), best for relational
data.
○ NoSQL: Schema-less, supports unstructured data like JSON, ideal for
EH
A foreign key is a column in a table that creates a relationship between two tables by
referencing the primary key in another table.
○ Primary keys uniquely identify rows in a table.
○ Foreign keys establish relationships between tables.
7. Q: What is normalization? Why is it important?
IN
10. Q: What is a stored procedure? How is it different from a function?
○ Stored Procedure: A set of SQL statements executed as a single unit. It may or
may not return a value.
○ Function: Returns a single value or table and is used in SQL queries.
14. Q: What is the difference between INNER JOIN and OUTER JOIN?
○ INNER JOIN: Returns matching rows from both tables.
○ OUTER JOIN: Returns matching rows plus unmatched rows from one or both
tables.
15. Q: How do you prevent SQL injection?
○ Use parameterized queries or prepared statements.
○ Validate user input.
○ Avoid dynamic SQL.
16. Q: How do you fetch the nth highest salary from an employee table?
FROM employees
LIMIT n-1, 1;
IN
17. Q: How do you find duplicate rows in a table?
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
IN
Use views or WHERE clauses with conditions based on user roles or IDs.
redundancy.
27. Q: What is ACID in databases?
JA
26. Q: What is denormalization? Why is it used?
Denormalization combines tables to improve read performance at the cost of increased
SELECT employee_id,
salary,
AS rank
FROM employees;
UNION ALL
FROM employees e
IN
SELECT * FROM hierarchy;
7. Query Optimization
JA
31. Q: What are common causes of slow queries?
○ Lack of proper indexing.
○ Use of SELECT * instead of selecting specific columns.
○ Complex joins without optimization.
A
○ Absence of query limiters like WHERE or LIMIT.
○ Large table scans due to missing filters or inefficient data organization.
32. Q: How do you identify and optimize slow queries?
EH
Left Join: Returns all rows from the left table and matching rows from the right.
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
Right Join: Returns all rows from the right table and matching rows from the left.
SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
IN
Full Outer Join: Combines results of left and right joins.
SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.id =
table2.id;
JA
36. Q: What is a self-join? Provide an example.
A self-join is a join where a table is joined to itself.
SELECT A.id, B.id
student_id INT,
course_id INT,
);
IN
10. Theoretical Concepts
at a time. JA
A cursor is a database object used to retrieve and manipulate rows from a result set one
44. Q: Write a query to get the second highest salary from the employees table.
SELECT MAX(salary)
FROM employees
FROM employees
GROUP BY salary
IN
DELETE FROM table_name
WHERE id NOT IN (
SELECT MIN(id)
FROM table_name
JA
GROUP BY column1, column2, ...);
47. Q: How do you find the first and last record in a table?
SELECT * FROM table_name ORDER BY id ASC LIMIT 1; -- First record
A
SELECT * FROM table_name ORDER BY id DESC LIMIT 1; -- Last record
EH