SQL - Q & A

100 SQL Interview Questions & Answers

100 SQL Interview Questions & Answers

🔹 Basics & Core Concepts

Q1. What is SQL?

SQL is a language used to store, retrieve, and manage data in relational databases.

Q2. What is a database?

A database is an organized collection of data.

Q3. What is a table?

A table stores data in rows and columns.

Q4. What is a primary key?

A primary key uniquely identifies records and cannot be NULL.

Q5. What is a foreign key?

A foreign key links tables by referencing a primary key.

Q6. What is a candidate key?

A column that can uniquely identify rows.

Q7. What is a composite key?

A key made of multiple columns.

Q8. What is NULL?

Represents missing or unknown value.

Q9. DELETE vs TRUNCATE?

DELETE removes rows conditionally, TRUNCATE removes all rows.

Q10. WHERE vs HAVING?

WHERE filters rows, HAVING filters groups.

Q11. What is DDL? CREATE, ALTER, DROP commands.
Q12. What is DML? INSERT, UPDATE, DELETE commands.
Q13. What is DCL? GRANT and REVOKE commands.
Q14. What is TCL? COMMIT, ROLLBACK commands.
Q15. What are constraints? Rules enforced on table columns.
Q16. What is UNIQUE constraint? Ensures all values are unique.
Q17. What is CHECK constraint? Restricts values in a column.
Q18. What is DEFAULT constraint? Provides default value.
Q19. What is SQL Injection? A security attack using malicious SQL.
Q20. How to prevent SQL Injection? Use prepared statements and parameterized queries.

🔹 Queries & Clauses

Q21. What is SELECT? Retrieves data from tables.
Q22. What is DISTINCT? Removes duplicate records.
Q23. What is ORDER BY? Sorts query results.
Q24. What is GROUP BY? Groups similar data.
Q25. What is LIMIT? Limits rows returned.
Q26. What is BETWEEN? Filters range values.
Q27. What is IN? Checks values in list.
Q28. What is LIKE? Search using patterns.
Q29. What are wildcards? % and _ characters.
Q30. What is alias? Temporary name for table/column.
Q31. COUNT(*) vs COUNT(column)? COUNT(column) ignores NULL.
Q32. What is IS NULL? Checks NULL values.
Q33. What is UNION? Combines query results.
Q34. UNION vs UNION ALL? UNION removes duplicates.
Q35. What is subquery? Query inside another query.
Q36. What is correlated subquery? Depends on outer query.
Q37. What is EXISTS? Checks existence of rows.
Q38. What is ANY? Returns true if any match.
Q39. What is ALL? Returns true if all match.
Q40. What is CASE? Conditional logic in SQL.

🔹 Joins & Indexes

Q41. What is JOIN? Combines rows from tables.
Q42. Types of JOIN? INNER, LEFT, RIGHT, FULL.
Q43. What is INNER JOIN? Returns matching rows.
Q44. What is LEFT JOIN? All left table rows.
Q45. What is RIGHT JOIN? All right table rows.
Q46. What is FULL JOIN? All matching & non-matching rows.
Q47. What is SELF JOIN? Table joined with itself.
Q48. What is CROSS JOIN? Cartesian product.
Q49. JOIN vs Subquery? JOIN is faster.
Q50. What is index? Improves query speed.
Q51. Types of index? Clustered and Non-clustered.
Q52. What is clustered index? Physical data order.
Q53. What is non-clustered index? Logical order.
Q54. How many clustered indexes? Only one.
Q55. What is referential integrity? Maintains relationships.
Q56. What is cascade? Auto update/delete.
Q57. What is many-to-many? Uses junction table.
Q58. One-to-many? One record to many.
Q59. What is normalization? Removes redundancy.
Q60. Normal forms? 1NF, 2NF, 3NF, BCNF.

🔹 Advanced & Interview Level

Q61. What is ACID? Atomicity, Consistency, Isolation, Durability.
Q62. What is transaction? Set of SQL operations.
Q63. COMMIT? Saves changes.
Q64. ROLLBACK? Undo changes.
Q65. SAVEPOINT? Partial rollback.
Q66. What is deadlock? Two transactions waiting forever.
Q67. What is view? Virtual table.
Q68. Stored procedure? Precompiled SQL.
Q69. Function? Returns single value.
Q70. Trigger? Auto-executed action.
Q71. CTE? Common Table Expression.
Q72. Window function? ROW_NUMBER(), RANK().
Q73. RANK vs DENSE_RANK? Dense rank doesn’t skip.
Q74. Execution plan? Query execution steps.
Q75. Query optimization? Indexes, proper joins.
Q76. OLTP? Transactional processing.
Q77. OLAP? Analytical processing.
Q78. Data warehouse? Historical data storage.
Q79. Fact table? Stores metrics.
Q80. Dimension table? Descriptive data.
Q81. ETL? Extract, Transform, Load.
Q82. Schema? Database structure.
Q83. Pivot? Rows to columns.
Q84. Unpivot? Columns to rows.
Q85. Sharding? Horizontal partitioning.
Q86. Replication? Copying data.
Q87. Surrogate key? Artificial key.
Q88. Index vs constraint? Index improves speed, constraint enforces rules.
Q89. DELETE vs DROP? DELETE removes rows, DROP removes table.
Q90. Temporary table? Exists during session.
Q91. Cursor? Row-by-row processing.
Q92. CHAR vs VARCHAR? VARCHAR is dynamic.
Q93. COALESCE? Returns first non-null.
Q94. NVL? Handles NULL values.
Q95. Index disadvantages? Slower insert/update.
Q96. What is locking? Controls concurrency.
Q97. Isolation levels? Read committed, Serializable.
Q98. SQL vs NoSQL? Structured vs Unstructured.
Q99. Why use SQL? Reliable and scalable.
Q100. SQL future? Still essential in data world.

No comments:

Post a Comment