Basic SQL Interview Questions
Basic SQL Interview Questions
Interview Questions
Ashish Zope
Senior Data Engineering
Basic SQL Interview Questions Ashish Zope | LinkedIn
1. What is SQL?
2. What is a primary key in SQL?
3. What is a foreign key in SQL?
4. What are the different types of joins in SQL?
5. What is normalization?
6. What are the different normal forms in database normalization?
7. What is denormalization?
8. What is the difference between WHERE and HAVING clause?
9. What is the difference between UNION and UNION ALL?
10. What is a LEFT JOIN in SQL?
11. What is a subquery in SQL?
12. What is a correlated subquery?
13. What is the GROUP BY clause used for?
14. What are aggregate functions in SQL?
15. What is the difference between CHAR and VARCHAR in SQL?
16. What is the COUNT() function in SQL?
17. What is an INNER JOIN?
18. What is an alias in SQL?
19. What is a RIGHT JOIN in SQL?
20. What is a view in SQL?
21. What is a stored procedure in SQL?
22. What is indexing in SQL?
23. What is the difference between TRUNCATE and DELETE?
24. What is a trigger in SQL?
25. What is the difference between DROP, DELETE, and TRUNCATE?
26. What is a UNIQUE constraint in SQL?
27. What is a CHECK constraint in SQL?
28. What is a DEFAULT constraint?
29. What is a CROSS JOIN in SQL?
30. What is the COALESCE() function in SQL?
31. What is the NVL() function in SQL?
32. What is a LEFT JOIN vs RIGHT JOIN in SQL?
33. What is a UNION in SQL?
34. What is a CASE statement in SQL?
35. What is a database view?
36. What are transactions in SQL?
37. What are the different types of joins in SQL?
38. What is an EXISTS clause in SQL?
39. What is a cursor in SQL?
40. What are constraints in SQL?
41. What is a PARTITION BY clause in SQL?
42. What is the difference between DELETE and TRUNCATE in terms of performance?
Page No. 1 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Page No. 2 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
1. What is SQL?
Answer: SQL (Structured Query Language) is a standard programming language used to manage and
manipulate relational databases.
Answer: A primary key is a unique identifier for each row in a table, ensuring that no two rows can
have the same value in this key.
Syntax:
Answer: A foreign key is a column or a set of columns that establishes a link between the data in two
tables.
Syntax:
Answer: SQL supports INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN.
Syntax for different joins:
Page No. 3 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
5. What is normalization?
Answer: Normalization is the process of organizing data to reduce redundancy and improve data
integrity. It involves dividing large tables into smaller ones.
7. What is denormalization?
Answer: Denormalization is the process of merging normalized tables to improve query performance,
at the cost of data redundancy.
Answer: WHERE is used to filter rows before grouping, and HAVING is used to filter groups after
aggregation.
Syntax:
Answer:
UNION removes duplicate records.
UNION ALL includes duplicates.
Page No. 4 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Syntax:
Answer: A LEFT JOIN returns all rows from the left table and the matched rows from the right table.
Unmatched rows in the right table will have NULL values.
Syntax:
Answer: A correlated subquery is a subquery that references columns from the outer query.
Syntax:
Page No. 5 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Answer: The GROUP BY clause is used to group rows that have the same values in specified columns
and apply aggregate functions like SUM(), COUNT(), etc.
Syntax:
Answer: Aggregate functions perform a calculation on a set of values and return a single value.
Examples include SUM(), COUNT(), AVG(), MIN(), MAX().
Syntax:
Answer:
CHAR is a fixed-length character data type.
VARCHAR is a variable-length character data type.
Syntax:
Answer: COUNT() returns the number of rows that match a specified condition.
Syntax:
Answer: INNER JOIN returns rows that have matching values in both tables.
Syntax:
Page No. 6 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Answer: An alias is a temporary name assigned to a table or column for the duration of a query.
Syntax:
Answer: A RIGHT JOIN returns all rows from the right table and the matched rows from the left table.
Unmatched rows in the left table will have NULL values.
Syntax:
Answer: A stored procedure is a group of SQL statements that can be executed as a single unit.
Syntax:
Page No. 7 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Answer: Indexes are used to speed up query performance by providing quick access to rows in a table.
Syntax:
Answer:
DELETE removes rows based on a condition and can be rolled back.
TRUNCATE removes all rows and cannot be rolled back.
Syntax:
Answer: A trigger is a set of SQL statements that automatically execute when an event (like INSERT,
UPDATE, DELETE) occurs on a table.
Syntax:
Answer:
DROP: Deletes the table or database itself.
DELETE: Removes rows from a table based on a condition.
TRUNCATE: Removes all rows from a table but keeps the structure.
Page No. 8 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Syntax:
Answer: A UNIQUE constraint ensures that all values in a column are unique.
Syntax:
Answer: A CHECK constraint ensures that values in a column satisfy a specific condition.
Syntax:
Answer: A DEFAULT constraint provides a default value to a column when no value is specified.
Syntax:
Page No. 9 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Answer: COALESCE() returns the first non-null value from a list of arguments.
Syntax:
Answer: NVL() replaces NULL values with a specified replacement value (used in Oracle).
Syntax:
Answer:
LEFT JOIN returns all rows from the left table and the matched rows from the right table.
RIGHT JOIN returns all rows from the right table and the matched rows from the left table.
Syntax:
Answer: UNION combines the result sets of two or more SELECT queries and removes duplicates.
Syntax:
Page No. 10 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
SELECT column1,
CASE
WHEN condition1 THEN 'result1'
WHEN condition2 THEN 'result2'
ELSE 'default_result'
END
FROM table;
Answer: A transaction is a sequence of operations executed as a single unit. It ensures ACID properties:
Atomicity, Consistency, Isolation, Durability.
Answer: SQL has five types of joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS
JOIN.
Page No. 11 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Answer: A cursor is a database object used to retrieve, manipulate, and iterate through query results
one row at a time.
Answer: Constraints ensure that the data in the database adheres to certain rules. Examples include
PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, and NOT NULL.
Answer: The PARTITION BY clause is used in window functions to divide the result set into partitions
and perform calculations on each partition.
Syntax:
SELECT column,
ROW_NUMBER() OVER (PARTITION BY column ORDER BY column) AS RowNum
FROM table;
42. What is the difference between DELETE and TRUNCATE in terms of performance?
Answer:
DELETE logs each row deletion and can be rolled back.
TRUNCATE is faster as it does not log individual row deletions and cannot always be rolled back.
Syntax:
Answer: The WITH clause (Common Table Expression, CTE) allows defining a temporary result set to be
referenced in a query.
Syntax:
Page No. 12 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
WITH CTE_Name AS (
SELECT column1, column2
FROM table
WHERE condition
)
SELECT * FROM CTE_Name;
Answer: A materialized view stores the result of a query physically and can be refreshed periodically.
Syntax (for creating a materialized view in Oracle):
Answer:
ISNULL() replaces NULL with a specified value (two arguments).
COALESCE() returns the first non-NULL value from a list (multiple arguments).
Syntax:
Answer: Referential integrity ensures that relationships between tables remain consistent via foreign
keys.
Answer: A recursive CTE is a CTE that references itself. It is used to work with hierarchical data.
Syntax:
WITH RecursiveCTE AS (
SELECT column1
FROM table
WHERE condition
Page No. 13 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
UNION ALL
SELECT column1
FROM table
JOIN RecursiveCTE ON condition
)
SELECT * FROM RecursiveCTE;
49. What is the difference between INNER JOIN and CROSS JOIN?
Answer:
INNER JOIN returns only matching rows.
CROSS JOIN returns the Cartesian product of the tables.
Syntax:
Answer: The GROUPING function distinguishes NULL values generated by ROLLUP or CUBE.
Syntax:
Answer: A surrogate key is a system-generated key (e.g., an auto-increment column) used to uniquely
identify rows.
Page No. 14 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Answer:
RAISEERROR is older and used to raise errors in SQL Server.
THROW is newer and better for exception handling.
Syntax:
Answer: The CHECK constraint ensures that all values in a column satisfy a specific condition.
Syntax:
Answer: FULL OUTER JOIN returns all rows when there is a match in either table.
Syntax:
Answer: A transaction log records all changes made to the database to ensure data integrity and
recovery.
Answer: The DISTINCT keyword removes duplicate rows from the result set.
Syntax:
Page No. 15 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Answer: A synonym is an alias for a database object, used to simplify object referencing.
Syntax:
Answer:
SCOPE_IDENTITY() returns the last identity in the current scope.
@@IDENTITY returns the last identity value for the entire session.
IDENT_CURRENT() returns the last identity for a specified table.
Syntax:
SELECT SCOPE_IDENTITY();
SELECT @@IDENTITY;
SELECT IDENT_CURRENT('table_name');
Answer: A temporary table is a table that exists only for the duration of a session or transaction.
Syntax:
Answer: ROLLUP is used with GROUP BY to calculate subtotals and a grand total.
Syntax:
Page No. 16 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn
Answer: An execution plan shows how SQL queries are executed and helps in optimizing performance.
Syntax (for SQL Server):
Answer:
MINUS is used in Oracle to return the difference between two queries.
EXCEPT is used in SQL Server for the same purpose.
Syntax:
Answer: Indexes speed up data retrieval by creating a data structure that allows faster query
performance.
Syntax:
Answer: A deadlock occurs when two transactions are waiting for each other to release locks, causing a
cycle of dependency.
Syntax:
SAVEPOINT savepoint_name;
Answer: A covering index includes all the columns needed by a query, so the query can be resolved
entirely using the index.
Syntax:
Page No. 18 of 18
Thank You
Ashish Zope
Senior Data Engineer at Bajaj Finserv
If you find this helpful, Repost and follow for more content.
Submit corrections in comments, if any.
Ashish Zope
Senior Data Engineering