Commonly asked SQL interview & practical questions

1. What is SQL?

Answer:
SQL (Structured Query Language) is used to store, retrieve, update, and delete data from relational databases.

Example:

SELECT * FROM students;

2. What is a database?

Answer:
A database is an organized collection of data stored electronically.

Example:
A school database containing tables like students, teachers, classes.


3. What is a table?

Answer:
A table stores data in rows and columns.

Example:

students(id, name, age, class)

4. What is a primary key?

Answer:
A primary key uniquely identifies each record in a table.

Example:

id INT PRIMARY KEY

5. What is a foreign key?

Answer:
A foreign key connects one table to another.

Example:

student_id REFERENCES students(id)

6. What is the SELECT statement?

Answer:
Used to retrieve data from a table.

Example:

SELECT name, age FROM students;

7. What is the WHERE clause?

Answer:
Filters records based on a condition.

Example:

SELECT * FROM students WHERE age > 15;

8. What is the INSERT statement?

Answer:
Adds new records to a table.

Example:

INSERT INTO students VALUES (1, 'Rahim', 16, '10');

9. What is the UPDATE statement?

Answer:
Modifies existing data.

Example:

UPDATE students SET age = 17 WHERE id = 1;

10. What is the DELETE statement?

Answer:
Removes records from a table.

Example:

DELETE FROM students WHERE id = 1;

11. What is the difference between DELETE and TRUNCATE?

Answer:

DELETETRUNCATE
Removes specific rowsRemoves all rows
Can use WHERECannot use WHERE
Can rollbackCannot rollback

12. What is the ORDER BY clause?

Answer:
Sorts result set.

Example:

SELECT * FROM students ORDER BY age DESC;

13. What is GROUP BY?

Answer:
Groups rows with same values.

Example:

SELECT class, COUNT(*) FROM students GROUP BY class;

14. What is HAVING?

Answer:
Filters grouped data.

Example:

SELECT class, COUNT(*) 
FROM students 
GROUP BY class 
HAVING COUNT(*) > 30;

15. What is a JOIN?

Answer:
Combines rows from multiple tables.

Example:

SELECT students.name, classes.class_name
FROM students
JOIN classes ON students.class_id = classes.id;

16. Types of JOINs?

Answer:

  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL JOIN

17. What is INNER JOIN?

Answer:
Returns matching records only.

Example:

SELECT * FROM students
INNER JOIN classes ON students.class_id = classes.id;

18. What is LEFT JOIN?

Answer:
Returns all records from left table.

Example:

SELECT * FROM students
LEFT JOIN classes ON students.class_id = classes.id;

19. What is NULL?

Answer:
NULL means no value.

Example:

SELECT * FROM students WHERE phone IS NULL;

20. What is COUNT()?

Answer:
Counts number of rows.

Example:

SELECT COUNT(*) FROM students;

21. What is SUM()?

Answer:
Calculates total.

Example:

SELECT SUM(fees) FROM payments;

22. What is AVG()?

Answer:
Calculates average.

Example:

SELECT AVG(age) FROM students;

23. What is DISTINCT?

Answer:
Removes duplicate values.

Example:

SELECT DISTINCT class FROM students;

24. What is LIKE?

Answer:
Searches patterns.

Example:

SELECT * FROM students WHERE name LIKE 'A%';

25. What is BETWEEN?

Answer:
Filters a range.

Example:

SELECT * FROM students WHERE age BETWEEN 14 AND 18;

26. What is IN?

Answer:
Matches multiple values.

Example:

SELECT * FROM students WHERE class IN ('9', '10');

27. What is a View?

Answer:
A virtual table created from a query.

Example:

CREATE VIEW student_view AS
SELECT name, age FROM students;

28. What is an Index?

Answer:
Improves query performance.

Example:

CREATE INDEX idx_name ON students(name);

29. What is a Stored Procedure?

Answer:
A set of SQL statements saved in database.

Example:

CREATE PROCEDURE GetStudents()
BEGIN
  SELECT * FROM students;
END;

30. What is normalization?

Answer:
Process of reducing data redundancy.

Example:
Separating students and classes into different tables.

Leave a Reply

Your email address will not be published. Required fields are marked *