0% found this document useful (0 votes)
17 views

Basic SQL Interview Questions

Uploaded by

vishakha chavan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Basic SQL Interview Questions

Uploaded by

vishakha chavan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Basic SQL

Interview Questions

Ashish Zope
Senior Data Engineering
Basic SQL Interview Questions Ashish Zope | LinkedIn

Basic SQL Interview Questions

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

43. Explain the use of the WITH clause in SQL.


44. What is a materialized view?
45. What is ISNULL() function in SQL? How is it different from COALESCE()?
46. What is referential integrity in SQL?
47. What is a recursive CTE?
48. What is a sequence in SQL?
49. What is the difference between INNER JOIN and CROSS JOIN?
50. Explain the use of the GROUPING function in SQL.
51. What is a surrogate key?
52. What is the difference between RAISEERROR and THROW in SQL?
53. What is the purpose of the CHECK constraint?
54. What is FULL OUTER JOIN?
55. What is a transaction log in SQL?
56. Explain the use of the DISTINCT keyword.
57. What are synonyms in SQL?
58. What is the difference between SCOPE_IDENTITY(), @@IDENTITY, and IDENT_CURRENT()?
59. What are the different types of relationships in SQL databases?
60. What is a temporary table?
61. What is the ROLLUP function in SQL?
62. What is an execution plan?
63. What is the difference between MINUS and EXCEPT?
64. What is a table alias in SQL?
65. What is sharding in SQL?
66. What is the purpose of indexing in SQL?
67. Explain DEADLOCK in SQL.
68. What is the purpose of SAVEPOINT in SQL?
69. What is a BIT data type in SQL?
70. What is a covering index?

Page No. 2 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

Let's get Started

1. What is SQL?

Answer: SQL (Structured Query Language) is a standard programming language used to manage and
manipulate relational databases.

2. What is a primary key in SQL?

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:

CREATE TABLE table_name (


column1 INT PRIMARY KEY,
column2 VARCHAR(50)
);

3. What is a foreign key in SQL?

Answer: A foreign key is a column or a set of columns that establishes a link between the data in two
tables.
Syntax:

CREATE TABLE child_table (


column1 INT,
column2 INT,
FOREIGN KEY (column2) REFERENCES parent_table (primary_key_column)
);

4. What are the different types of joins in SQL?

Answer: SQL supports INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN.
Syntax for different joins:

SELECT * FROM table1


INNER JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1


LEFT JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1

Page No. 3 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

RIGHT JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1


FULL OUTER JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1


CROSS JOIN table2;

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.

6. What are the different normal forms in database normalization?

Answer: The common normal forms are:


1NF (First Normal Form)
2NF (Second Normal Form)
3NF (Third Normal Form)
BCNF (Boyce-Codd Normal Form)

7. What is denormalization?

Answer: Denormalization is the process of merging normalized tables to improve query performance,
at the cost of data redundancy.

8. What is the difference between WHERE and HAVING clause?

Answer: WHERE is used to filter rows before grouping, and HAVING is used to filter groups after
aggregation.
Syntax:

SELECT column1, COUNT(*)


FROM table
WHERE condition
GROUP BY column1
HAVING COUNT(*) > 1;

9. What is the difference between UNION and UNION ALL?

Answer:
UNION removes duplicate records.
UNION ALL includes duplicates.

Page No. 4 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

Syntax:

SELECT column1 FROM table1


UNION
SELECT column1 FROM table2;

SELECT column1 FROM table1


UNION ALL
SELECT column1 FROM table2;

10. What is a LEFT JOIN in SQL?

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:

SELECT * FROM table1


LEFT JOIN table2 ON table1.id = table2.id;

11. What is a subquery in SQL?

Answer: A subquery is a query nested inside another query.


Syntax:

SELECT * FROM table1


WHERE column1 IN (SELECT column2 FROM table2);

12. What is a correlated subquery?

Answer: A correlated subquery is a subquery that references columns from the outer query.
Syntax:

SELECT column1 FROM table1 t1


WHERE EXISTS (
SELECT 1 FROM table2 t2
WHERE t2.column2 = t1.column1
);

13. What is the GROUP BY clause used for?

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:

SELECT column1, COUNT(*)


FROM table
GROUP BY column1;

14. What are aggregate functions in SQL?

Answer: Aggregate functions perform a calculation on a set of values and return a single value.
Examples include SUM(), COUNT(), AVG(), MIN(), MAX().
Syntax:

SELECT SUM(column) FROM table;

15. What is the difference between CHAR and VARCHAR in SQL?

Answer:
CHAR is a fixed-length character data type.
VARCHAR is a variable-length character data type.
Syntax:

CREATE TABLE table_name (


col1 CHAR(10),
col2 VARCHAR(50)
);

16. What is the COUNT() function in SQL?

Answer: COUNT() returns the number of rows that match a specified condition.
Syntax:

SELECT COUNT(*) FROM table WHERE condition;

17. What is an INNER JOIN?

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

SELECT * FROM table1


INNER JOIN table2 ON table1.id = table2.id;

18. What is an alias in SQL?

Answer: An alias is a temporary name assigned to a table or column for the duration of a query.
Syntax:

SELECT column1 AS alias_name FROM table;

SELECT t1.column1, t2.column2


FROM table1 t1, table2 t2
WHERE t1.id = t2.id;

19. What is a RIGHT JOIN in SQL?

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:

SELECT * FROM table1


RIGHT JOIN table2 ON table1.id = table2.id;

20. What is a view in SQL?

Answer: A view is a virtual table based on the result of a SELECT query.


Syntax:

CREATE VIEW view_name AS


SELECT column1, column2 FROM table;

21. What is a stored procedure in SQL?

Answer: A stored procedure is a group of SQL statements that can be executed as a single unit.
Syntax:

CREATE PROCEDURE procedure_name AS


BEGIN

Page No. 7 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

SELECT * FROM table;


END;

22. What is indexing in SQL?

Answer: Indexes are used to speed up query performance by providing quick access to rows in a table.
Syntax:

CREATE INDEX index_name ON table (column1);

23. What is the difference between TRUNCATE and DELETE?

Answer:
DELETE removes rows based on a condition and can be rolled back.
TRUNCATE removes all rows and cannot be rolled back.
Syntax:

DELETE FROM table WHERE condition;

TRUNCATE TABLE table;

24. What is a trigger in SQL?

Answer: A trigger is a set of SQL statements that automatically execute when an event (like INSERT,
UPDATE, DELETE) occurs on a table.
Syntax:

CREATE TRIGGER trigger_name


AFTER INSERT ON table
FOR EACH ROW
BEGIN
-- SQL code
END;

25. What is the difference between DROP, DELETE, and TRUNCATE?

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:

DROP TABLE table;

DELETE FROM table WHERE condition;

TRUNCATE TABLE table;

26. What is a UNIQUE constraint in SQL?

Answer: A UNIQUE constraint ensures that all values in a column are unique.

Syntax:

CREATE TABLE table_name (


column1 INT UNIQUE
);

27. What is a CHECK constraint in SQL?

Answer: A CHECK constraint ensures that values in a column satisfy a specific condition.
Syntax:

CREATE TABLE table_name (


column1 INT,
column2 INT CHECK (column2 > 0)
);

28. What is a DEFAULT constraint?

Answer: A DEFAULT constraint provides a default value to a column when no value is specified.
Syntax:

CREATE TABLE table_name (


column1 INT DEFAULT 0
);

29. What is a CROSS JOIN in SQL?

Page No. 9 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

Answer: A CROSS JOIN returns the Cartesian product of two tables.


Syntax:

SELECT * FROM table1


CROSS JOIN table2;

30. What is the COALESCE() function in SQL?

Answer: COALESCE() returns the first non-null value from a list of arguments.
Syntax:

SELECT COALESCE(column1, column2, 'default_value') FROM table;

31. What is the NVL() function in SQL?

Answer: NVL() replaces NULL values with a specified replacement value (used in Oracle).
Syntax:

SELECT NVL(column, 'replacement') FROM table;

32. What is a LEFT JOIN vs RIGHT JOIN in SQL?

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:

SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;

33. What is a UNION in SQL?

Answer: UNION combines the result sets of two or more SELECT queries and removes duplicates.
Syntax:

SELECT column1 FROM table1


UNION

Page No. 10 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

SELECT column1 FROM table2;

34. What is a CASE statement in SQL?

Answer: A CASE statement allows performing conditional logic in SQL.


Syntax:

SELECT column1,
CASE
WHEN condition1 THEN 'result1'
WHEN condition2 THEN 'result2'
ELSE 'default_result'
END
FROM table;

35. What is a database view?

Answer: A database view is a virtual table based on a SELECT query.


Syntax:

CREATE VIEW view_name AS


SELECT column1, column2 FROM table;

36. What are transactions in SQL?

Answer: A transaction is a sequence of operations executed as a single unit. It ensures ACID properties:
Atomicity, Consistency, Isolation, Durability.

37. What are the different types of joins in SQL?

Answer: SQL has five types of joins: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS
JOIN.

38. What is an EXISTS clause in SQL?

Answer: The EXISTS clause checks if a subquery returns any rows.


Syntax:

SELECT column1 FROM table


WHERE EXISTS (

Page No. 11 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

SELECT 1 FROM table2 WHERE condition


);

39. What is a cursor in SQL?

Answer: A cursor is a database object used to retrieve, manipulate, and iterate through query results
one row at a time.

40. What are constraints in SQL?

Answer: Constraints ensure that the data in the database adheres to certain rules. Examples include
PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, and NOT NULL.

41. What is a PARTITION BY clause in SQL?

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:

DELETE FROM table WHERE condition;

TRUNCATE TABLE table;

43. Explain the use of the WITH clause in SQL.

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;

44. What is a materialized view?

Answer: A materialized view stores the result of a query physically and can be refreshed periodically.
Syntax (for creating a materialized view in Oracle):

CREATE MATERIALIZED VIEW view_name AS


SELECT column1, column2
FROM table;

45. What is ISNULL() function in SQL? How is it different from COALESCE()?

Answer:
ISNULL() replaces NULL with a specified value (two arguments).
COALESCE() returns the first non-NULL value from a list (multiple arguments).
Syntax:

SELECT ISNULL(column, 'replacement') FROM table;

SELECT COALESCE(column1, column2, 'replacement') FROM table;

46. What is referential integrity in SQL?

Answer: Referential integrity ensures that relationships between tables remain consistent via foreign
keys.

47. What is a recursive CTE?

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;

48. What is a sequence in SQL?

Answer: A sequence generates a series of unique numbers.


Syntax (for creating a sequence):

CREATE SEQUENCE sequence_name


START WITH 1
INCREMENT BY 1;

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:

SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;

SELECT * FROM table1 CROSS JOIN table2;

50. Explain the use of the GROUPING function in SQL.

Answer: The GROUPING function distinguishes NULL values generated by ROLLUP or CUBE.
Syntax:

SELECT column1, SUM(column2),


GROUPING(column1) AS grp
FROM table
GROUP BY ROLLUP (column1);

51. What is a surrogate key?

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

52. What is the difference between RAISEERROR and THROW in SQL?

Answer:
RAISEERROR is older and used to raise errors in SQL Server.
THROW is newer and better for exception handling.
Syntax:

RAISERROR('Error Message', 16, 1);

THROW 50000, 'Error Message', 1;

53. What is the purpose of the CHECK constraint?

Answer: The CHECK constraint ensures that all values in a column satisfy a specific condition.
Syntax:

CREATE TABLE table_name (


column1 INT,
column2 INT CHECK (column2 >= 0)
);

54. What is FULL OUTER JOIN?

Answer: FULL OUTER JOIN returns all rows when there is a match in either table.
Syntax:

SELECT * FROM table1


FULL OUTER JOIN table2
ON table1.id = table2.id;

55. What is a transaction log in SQL?

Answer: A transaction log records all changes made to the database to ensure data integrity and
recovery.

56. Explain the use of the DISTINCT keyword.

Answer: The DISTINCT keyword removes duplicate rows from the result set.
Syntax:

Page No. 15 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

SELECT DISTINCT column1 FROM table;

57. What are synonyms in SQL?

Answer: A synonym is an alias for a database object, used to simplify object referencing.
Syntax:

CREATE SYNONYM synonym_name FOR schema.object_name;

58. What is the difference between SCOPE_IDENTITY(), @@IDENTITY, and


IDENT_CURRENT()?

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');

59. What are the different types of relationships in SQL databases?

Answer: One-to-One, One-to-Many, and Many-to-Many relationships.

60. What is a temporary table?

Answer: A temporary table is a table that exists only for the duration of a session or transaction.
Syntax:

CREATE TEMPORARY TABLE #temp_table (column1 INT);

61. What is the ROLLUP function in SQL?

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

SELECT column1, SUM(column2)


FROM table
GROUP BY ROLLUP (column1);

62. What is an execution plan?

Answer: An execution plan shows how SQL queries are executed and helps in optimizing performance.
Syntax (for SQL Server):

EXPLAIN PLAN FOR SELECT * FROM table;

63. What is the difference between MINUS and EXCEPT?

Answer:
MINUS is used in Oracle to return the difference between two queries.
EXCEPT is used in SQL Server for the same purpose.
Syntax:

SELECT * FROM table1


MINUS
SELECT * FROM table2;

SELECT * FROM table1


EXCEPT
SELECT * FROM table2;

64. What is a table alias in SQL?

Answer: A table alias is a temporary name for a table within a query.


Syntax:

SELECT t.column1 FROM table t;

65. What is sharding in SQL?

Answer: Sharding is a database partitioning technique to improve performance by distributing data


across multiple servers.

66. What is the purpose of indexing in SQL?


Page No. 17 of 18
Basic SQL Interview Questions Ashish Zope | LinkedIn

Answer: Indexes speed up data retrieval by creating a data structure that allows faster query
performance.
Syntax:

CREATE INDEX index_name ON table(column);

67. Explain DEADLOCK in SQL.

Answer: A deadlock occurs when two transactions are waiting for each other to release locks, causing a
cycle of dependency.

68. What is the purpose of SAVEPOINT in SQL?

Answer: A SAVEPOINT allows rolling

back a transaction to a specific point without affecting the entire transaction.

Syntax:

SAVEPOINT savepoint_name;

69. What is a BIT data type in SQL?

Answer: The BIT data type stores Boolean values (0 or 1).

70. What is a covering index?

Answer: A covering index includes all the columns needed by a query, so the query can be resolved
entirely using the index.
Syntax:

CREATE INDEX idx_name ON table(column1, column2);

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

You might also like