
SQL Questions and Answers
Top 100 SQL Interview Questions for Freshers
SQL (Structured Query Language) is the backbone of modern database management, enabling professionals to create, manipulate, and manage relational databases efficiently. Mastering SQL is essential for data analysts, software developers, and database administrators, as it facilitates querying, updating, and optimizing data stored in relational databases. A strong foundation in SQL allows professionals to retrieve and manipulate complex data structures, write optimized queries, manage database transactions, and ensure data integrity. Candidates aspiring for database-related roles at IDM TechPark should be well-versed in both theoretical and practical aspects of SQL. To help you succeed, we have compiled a comprehensive list of the Top 100 SQL Interview Questions along with expert-level answers. Mastering these SQL concepts will not only enhance your technical skills but also provide a competitive advantage in securing a database management role and excelling in the field of data-driven applications.
1. What is SQL?
Answer: SQL (Structured Query Language) is a standard language for managing and manipulating relational databases.
2. What are the different types of SQL commands?
Answer:
-
DDL (Data Definition Language) – CREATE, ALTER, DROP
-
DML (Data Manipulation Language) – INSERT, UPDATE, DELETE
-
DQL (Data Query Language) – SELECT
-
TCL (Transaction Control Language) – COMMIT, ROLLBACK, SAVEPOINT
-
DCL (Data Control Language) – GRANT, REVOKE
3. What is a Primary Key?
Answer: A Primary Key uniquely identifies each row in a table and does not allow NULL values.
4. What is a Foreign Key?
Answer: A Foreign Key is a column that establishes a relationship between two tables by referencing the Primary Key of another table.
5. What is the difference between CHAR and VARCHAR?
Answer:
-
CHAR(n) – Fixed-length string (e.g., CHAR(10) always takes 10 bytes).
-
VARCHAR(n) – Variable-length string (e.g., VARCHAR(10) uses only the necessary space).
6. What is the difference between WHERE and HAVING?
Answer:
-
WHERE filters rows before aggregation.
-
HAVING filters rows after aggregation (used with GROUP BY).
7. How do you fetch unique records from a table?
Answer: Use the DISTINCT keyword:
sql
CopyEdit
SELECT DISTINCT column_name FROM table_name;
8. What is an Index in SQL?
Answer: An Index improves the speed of queries by allowing faster retrieval of rows.
9. What are the types of Indexes?
Answer:
-
Clustered Index – Sorts and stores data in the table.
-
Non-clustered Index – Creates a separate structure to improve query performance.
10. How do you add a new column to an existing table?
Answer:
sql
CopyEdit
ALTER TABLE table_name ADD column_name datatype;
11. How do you change a column’s data type?
Answer:
sql
CopyEdit
ALTER TABLE table_name MODIFY column_name new_datatype;
12. How do you delete a table in SQL?
Answer:
sql
CopyEdit
DROP TABLE table_name;
13. How do you remove duplicate rows in SQL?
Answer:
sql
CopyEdit
DELETE FROM table_name WHERE rowid NOT IN (SELECT MIN(rowid) FROM table_name GROUP BY column_name);
14. What is the difference between DELETE and TRUNCATE?
Answer:
-
DELETE removes specific rows with a condition and can be rolled back.
-
TRUNCATE removes all rows without logging and cannot be rolled back.
15. What is LIMIT in SQL?
Answer: LIMIT restricts the number of rows returned in a query.
sql
CopyEdit
SELECT * FROM table_name LIMIT 5;
16. How do you sort query results?
Answer: Use ORDER BY:
sql
CopyEdit
SELECT * FROM table_name ORDER BY column_name ASC|DESC;
17. What is the default sorting order in ORDER BY?
Answer: Ascending (ASC) is the default sorting order.
18. How do you rename a table in SQL?
Answer:
sql
CopyEdit
ALTER TABLE old_table_name RENAME TO new_table_name;
19. What are Joins in SQL?
Answer: Joins combine rows from two or more tables based on a related column.
20. What are the types of SQL Joins?
Answer:
-
INNER JOIN – Returns matching rows from both tables.
-
LEFT JOIN – Returns all rows from the left table and matching rows from the right table.
-
RIGHT JOIN – Returns all rows from the right table and matching rows from the left table.
-
FULL JOIN – Returns all rows when there is a match in either table.
21. How do you use CASE in SQL?
Answer:
sql
CopyEdit
SELECT column_name, CASE WHEN condition1 THEN 'Result1' WHEN condition2 THEN 'Result2' ELSE 'Default Result' END AS alias_name FROM table_name;
22. How do you check for NULL values?
Answer:
sql
CopyEdit
SELECT * FROM table_name WHERE column_name IS NULL;
23. What is the default port number of MySQL?
Answer: 3306
24. What is GROUP BY used for?
Answer: It groups rows with the same values in a specified column.
sql
CopyEdit
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
25. What is COALESCE in SQL?
Answer: It returns the first non-null value from a list.
sql
CopyEdit
SELECT COALESCE(column1, column2, 'Default Value') FROM table_name;