SQL Interview Questions and Answers (Complete List)
1. What is Relational Database Management System (RDBMS)?
RDBMS is a database system that stores data in the form of related tables. It uses keys to establish
relationships among the tables.
2. What is Structured Query Language?
SQL is a standard language for accessing and manipulating databases. It is used to perform tasks
such as querying data, updating data, and managing databases.
3. What is a Database?
A database is an organized collection of data stored electronically and can be accessed and
modified using database management systems.
4. What is primary key?
A primary key is a field in a table that uniquely identifies each row/record. It must contain unique
values and cannot contain NULLs.
5. What is a unique key?
A unique key constraint ensures all values in a column are different. Unlike primary keys, a table can
have multiple unique keys.
6. What is a foreign key?
A foreign key is a column that creates a relationship between two tables by referencing the primary
key of another table.
7. Difference between spreadsheets and databases?
Spreadsheets are suitable for small data analysis and manipulation. Databases handle large
volumes of structured data and support concurrent access, indexing, and query optimization.
8. What are table and fields?
A table is a collection of data organized in rows and columns. Fields (columns) define the type of
data stored, while rows are individual records.
9. Explain various SQL languages.
SQL has four categories: DDL (Data Definition Language), DML (Data Manipulation Language),
DCL (Data Control Language), and TCL (Transaction Control Language).
10. What is normalization?
Normalization is the process of organizing data to minimize redundancy and improve data integrity.
11. What is denormalization?
Denormalization is the process of combining tables to reduce the complexity of queries and improve
read performance.
12. Types of normalization?
1NF: Atomic values; 2NF: Remove partial dependency; 3NF: Remove transitive dependency; BCNF:
Every determinant is a candidate key.
13. What are views in SQL?
Views are virtual tables based on SQL queries. They don't store data but display it from underlying
tables.
14. What is join? Types?
JOIN combines rows from two or more tables. Types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL
JOIN, CROSS JOIN, SELF JOIN.
15. Types of indexes?
Clustered, Non-clustered, Unique, Composite, Full-text, Spatial.
16. What is a cursor in SQL?
A cursor is a database object used to retrieve and manipulate data row by row from a result set.
17. What is query?
A query is a request for data or information from a database using SQL.
18. What is a subquery?
A subquery is a query nested inside another SQL query.
19. What is a trigger?
A trigger is a stored procedure that executes automatically in response to certain events on a table.
20. DELETE vs TRUNCATE?
DELETE removes rows with WHERE condition and can be rolled back. TRUNCATE removes all
rows, resets identity, and cannot be rolled back.
21. Local vs Global variables?
Local variables exist within a procedure. Global variables are accessible throughout the session.
22. What are constraints?
Constraints enforce rules at the table level. Types: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN
KEY, CHECK.
23. What is data integrity?
Data integrity ensures the accuracy and consistency of data.
24. What is auto increment?
Auto increment allows a unique number to be generated automatically when a new record is
inserted.
25. What is a data warehouse?
A data warehouse is a system used for reporting and data analysis by integrating data from multiple
sources.
26. DROP vs TRUNCATE?
DROP removes the table and its structure. TRUNCATE removes only data, keeping the structure.
27. Aggregate vs Scalar functions?
Aggregate: operate on set of rows (SUM, AVG). Scalar: operate on single value (UCASE, NOW).
28. What is alias in SQL?
Alias is a temporary name given to a table or column for readability.
29. OLTP vs OLAP?
OLTP: handles transactional data. OLAP: handles analytical processing.
30. What is collation? Types?
Collation defines rules for sorting and comparing strings. Sensitivities: case, accent, kana, width.
31. Create table in SQL?
CREATE TABLE Employees (ID INT, Name VARCHAR(50));
32. Insert data in SQL?
INSERT INTO Employees (ID, Name) VALUES (1, 'John');
33. Change table name?
ALTER TABLE OldName RENAME TO NewName;
34. What is SQL Server?
SQL Server is a relational database management system developed by Microsoft.
35. What is ETL?
ETL stands for Extract, Transform, Load. It's used in data warehousing to move data.
36. Nested queries?
A query within a query. Used for complex filtering and conditions.
37. CHAR vs VARCHAR2?
CHAR: fixed-length. VARCHAR2: variable-length.
38. SQL vs PL/SQL?
SQL: query language. PL/SQL: procedural language with loops and conditions.
39. SQL vs MySQL?
SQL is a language. MySQL is a database that uses SQL.
40. What is cross join?
Returns Cartesian product of two tables.
41. User defined functions?
Functions created by users to encapsulate code and return values.
42. What is a CLAUSE?
Clause filters rows. Examples: WHERE, ORDER BY, GROUP BY.
43. Recursive stored procedure?
Procedure that calls itself.
44. UNION, MINUS, INTERSECT?
UNION: combines unique rows. MINUS: returns diff rows. INTERSECT: returns common rows.
45. TCP/IP port of SQL Server?
Default port is 1433.
46. Operator for pattern matching?
LIKE is used with % or _ for matching patterns.
47. Select unique records?
SELECT DISTINCT column_name FROM table;
48. ACID properties?
Atomicity, Consistency, Isolation, Durability - ensure reliable transactions.
49. BETWEEN vs IN?
BETWEEN: range. IN: specific values.
50. Use of SQL functions?
Perform operations like sum, avg, string manipulation, date handling.
51. Need for MERGE?
MERGE performs insert, update, or delete in one statement.
52. Dynamic SQL execution?
EXEC, sp_executesql, EXECUTE IMMEDIATE.
53. Case manipulation functions?
UPPER, LOWER, INITCAP.
54. Is semicolon used?
Optional in SQL Server; mandatory in some databases.
55. What is candidate key?
Any column or combination that can uniquely identify a record.
56. JOIN vs UNION?
JOIN merges columns. UNION merges rows.
57. ORDER BY vs GROUP BY?
ORDER BY sorts. GROUP BY groups similar data.
58. SQL query for salary range?
SELECT name FROM employees WHERE salary BETWEEN 10000 AND 20000;
59. SQL injection?
Malicious code in queries to access data. Happens via unsanitized inputs.
60. What is ENUM?
ENUM defines a column with predefined set of values.
61. ATAN vs ATAN2?
ATAN returns arc tangent. ATAN2 considers both X and Y.
62. CEIL vs FLOOR vs ROUND?
CEIL: rounds up. FLOOR: rounds down. ROUND: rounds to nearest.
63. RAND() function?
Returns random number between 0 and 1.
64. LOCALTIMESTAMP vs CURRENT_TIMESTAMP?
LOCALTIMESTAMP: no time zone. CURRENT_TIMESTAMP: includes time zone.
65. Date/time functions?
NOW(), SYSDATE(), CURRENT_DATE.
66. Date difference function?
DATEDIFF returns the number of days between dates.
67. Fetch common records?
SELECT * FROM A INTERSECT SELECT * FROM B;
68. Fetch alternate records?
Use ROW_NUMBER() or MOD function to filter even/odd rows.
69. Select unique records?
SELECT DISTINCT * FROM table;
70. First 5 characters of string?
SELECT SUBSTRING(column, 1, 5) FROM table;
71. Use of LIKE?
SELECT * FROM table WHERE name LIKE 'A%';
72. Copy a table?
CREATE TABLE new_table AS SELECT * FROM old_table;
73. Dropping table drops related objects?
Yes, like constraints, indexes, views depending on DBMS.
74. What is Live Lock?
A situation where processes keep changing states but none progress.