SQL Interview Questions and Answers for Freshers
SQL is one of the most important skills for students preparing for software development, database and data analyst interviews. These 20 questions cover database fundamentals, practical queries and concepts commonly discussed in entry-level technical interviews.
Examples use common SQL syntax. Some features and commands differ between MySQL, MariaDB, PostgreSQL and SQL Server.
SQL Fundamentals
1. What is SQL?
SQL stands for Structured Query Language. It is used to query and manage data in relational database systems.
Common operations include SELECT, INSERT, UPDATE and DELETE.
2. What is the difference between a database and a table?
A database organizes related data objects. A table stores data in rows and columns.
For example, a college database may contain students, courses and enrollments tables.
3. What is a primary key?
A primary key uniquely identifies each row in a table. Primary key columns cannot contain NULL values.
A student_id column is a typical example.
4. What is a foreign key?
A foreign key creates a relationship between tables by referencing a candidate key, commonly a primary key, in another table.
Foreign keys help maintain referential integrity.
5. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping. HAVING filters grouped results after aggregation.
Example:
SELECT department_id, COUNT(*) AS total
FROM employees
WHERE active = 1
GROUP BY department_id
HAVING COUNT(*) > 5;
This returns departments with more than five active employees.
Practical SQL Queries
6. How do you retrieve all rows from a table?
Use SELECT with an asterisk:
SELECT * FROM students;
In production applications, selecting only required columns is usually preferable.
7. How do you sort query results?
Use ORDER BY.
SELECT name, marks
FROM students
ORDER BY marks DESC;
DESC sorts from highest to lowest. ASC sorts from lowest to highest.
8. How do you find unique values?
Use DISTINCT.
SELECT DISTINCT city
FROM students;
DISTINCT removes duplicate combinations from the selected result columns.
9. How do you count records?
Use COUNT.
SELECT COUNT(*) AS total_students
FROM students;
COUNT(*) counts rows. COUNT(column_name) counts non-NULL values in that column.
10. How do you find the second-highest distinct salary?
One approach is:
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (
SELECT MAX(salary)
FROM employees
);
This returns the second-highest distinct salary. It returns NULL if no lower salary exists.
SQL JOIN Questions
11. What is an INNER JOIN?
INNER JOIN returns rows with matching values in both joined tables.
Example:
SELECT s.name, c.course_name
FROM students AS s
INNER JOIN courses AS c
ON s.course_id = c.id;
Only students with a matching course record are returned.
12. What is a LEFT JOIN?
LEFT JOIN returns all rows from the left table and matching rows from the right table.
If there is no match, columns from the right table contain NULL.
13. What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN excludes unmatched left-side rows. LEFT JOIN preserves them.
Choose the JOIN type according to the records your application needs to display.
14. What is a self JOIN?
A self JOIN joins a table to itself using aliases.
It is useful for hierarchical relationships such as employees and their managers.
Aggregation and Performance
15. What is GROUP BY?
GROUP BY organizes rows into groups so aggregate functions can calculate results for each group.
Example:
SELECT department_id, AVG(salary) AS average_salary
FROM employees
GROUP BY department_id;
16. What is an index?
An index is a database structure that can speed up data retrieval.
Indexes may also require additional storage and introduce overhead for INSERT, UPDATE and DELETE operations.
17. What is the difference between DELETE, TRUNCATE and DROP?
DELETE removes selected rows or all rows when no WHERE clause is supplied.
TRUNCATE removes all rows using a database-specific operation, often with different transaction and identity-reset behavior.
DROP removes the table definition and its data.
These operations have different consequences and should never be used casually on production databases.
18. What is normalization?
Database normalization organizes data to reduce unnecessary duplication and improve consistency.
Common normal forms include first normal form, second normal form and third normal form.
For example, separating student details from course details can avoid repeating course information in every student record.
Transactions and Security
19. What is a database transaction?
A transaction groups operations into a logical unit of work.
ACID refers to Atomicity, Consistency, Isolation and Durability.
Example:
START TRANSACTION;
UPDATE accounts
SET balance = balance - 500
WHERE id = 1;
UPDATE accounts
SET balance = balance + 500
WHERE id = 2;
COMMIT;
This is a simplified example. A real banking transfer must also validate balances, affected rows and concurrency behavior before committing.
20. What is SQL injection, and how can it be prevented?
SQL injection occurs when untrusted input is interpreted as part of a SQL command.
Use parameterized queries or prepared statements. Validate inputs and apply least-privilege database permissions.
Do not build SQL statements by directly concatenating user-provided values.
Practical SQL Exercises for Students
Exercise 1: Create a students table with student_id, name, course and marks.
Exercise 2: Write a query to display students scoring above 70.
Exercise 3: Find the average marks for each course.
Exercise 4: Create a courses table and practice INNER JOIN and LEFT JOIN.
Exercise 5: Find duplicate email addresses using GROUP BY and HAVING.
Exercise 6: Explain how an index could help a frequently searched column.
Practice these exercises on a local test database rather than a production system.
Frequently Asked Questions
Is SQL required for web developers?
SQL is valuable for backend developers and full-stack developers working with relational databases.
Which database should beginners use?
MySQL, MariaDB, PostgreSQL and SQLite are all reasonable learning choices. The fundamentals transfer between systems.
Are SQL and MySQL the same?
No. SQL is a language, while MySQL is a relational database management system that supports SQL.
How should freshers prepare for SQL interviews?
Practice SELECT queries, JOINs, grouping, subqueries, constraints, indexes and transactions. Explain your reasoning instead of only memorizing syntax.
Conclusion
SQL interview preparation should combine database theory with practical queries. Students who understand relationships, JOINs, filtering, aggregation and transactions are better prepared for technical interviews and real software projects.