August 20, 2024

Top SQL Interview Questions for Freshers

Top SQL Interview Questions for Freshers

Are you preparing for your first SQL interview and wondering what questions you might face? Understanding the key SQL interview questions for freshers can give you more clarity.

This blog is here to help you get ready with practical questions that test your real-world problem-solving skills. We’ve gathered some of the most common basic SQL interview questions that freshers often encounter.

With this guide, you’ll be well-prepared to tackle these SQL interview questions and answers for freshers and make a strong impression in your interview.

sql beginners course desktop banner horizontal

Practice SQL Interview Questions and Answers

Below are the SQL interview questions for freshers with answers:

1. What is a database, and how does it differ from a relational database?

Answer:

A database is an organized collection of data, while a relational database organizes data into tables with relationships between them.

2. Explain the purpose of SQL in managing databases.

Answer:

SQL is used to interact with and manage relational databases, allowing for data retrieval, insertion, updating, and deletion.

3. What is the basic structure of an SQL query?

Answer:

An SQL query typically consists of clauses like SELECT, FROM, WHERE, ORDER BY, and can include other components depending on the operation.

SELECT column1, column2 FROM table WHERE condition;

4. What are tables, rows, and columns in a relational database?

Answer:

Tables store data in a structured format with rows representing records and columns representing attributes of the data.

5. How would you describe the relationship between SQL and relational databases?

Answer:

SQL is the language used to manage and query data in relational databases, which organize data into interrelated tables.

6. Write an SQL statement to create a table called Employees with columns ID, Name, and Age.

Answer:

The SQL statement uses the CREATE TABLE command to define the structure of the Employees table.

CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT
);

7. How do you insert a new record into the Employees table?

Answer:

The INSERT INTO command is used to add a new row to the Employees table.

INSERT INTO Employees (ID, Name, Age) VALUES (1, ‘John Doe’, 30);

8. Write an SQL query to update the age of an employee with ID = 1 to 35.

Answer:

The UPDATE command modifies the existing data in the table based on the specified condition.

UPDATE Employees SET Age = 35 WHERE ID = 1;

9. How do you delete a record from the Employees table where ID = 1?

Answer:

The DELETE statement removes a specific row from the table based on the condition.

DELETE FROM Employees WHERE ID = 1;

10. What SQL statement would you use to view all the records in the Employees table?

Answer:

The SELECT statement is used to retrieve all rows from the table.

SELECT * FROM Employees;

11. What is the difference between CHAR and VARCHAR data types in SQL?

Answer:

CHAR has a fixed length, while VARCHAR has a variable length, making VARCHAR more efficient for storing text of varying lengths.

12. How would you define a column Salary with a numeric data type that allows decimal values?

Answer:

The DECIMAL data type is used for defining a column with precision and scale for decimal values.

Salary DECIMAL(10, 2)

13. Write an SQL statement to create a table with a DATE and a TIME column.

Answer:

The statement uses DATE and TIME data types to store date and time values separately.

CREATE TABLE Schedule (
EventDate DATE,
EventTime TIME
);

14. How do you store a large amount of text in an SQL table?

Answer:

Use the TEXT or BLOB data type for columns that need to store large text or binary data.

CREATE TABLE Documents (
Content TEXT
);

15. How can you store a boolean value in an SQL database?

Answer:

Use the BOOLEAN data type to store true/false values.

IsActive BOOLEAN

16. Write a basic SQL query to retrieve the Name column from the Employees table.

Answer:

The SELECT statement is used to fetch specific columns from a table.

SELECT Name FROM Employees;

17. How do you filter records in SQL to only show employees older than 30?

Answer:

The WHERE clause filters results based on the specified condition.

SELECT * FROM Employees WHERE Age > 30;

18. Write an SQL query to sort the Employees table by Name in ascending order.

Answer:

The ORDER BY clause sorts the query results based on one or more columns.

SELECT * FROM Employees ORDER BY Name ASC;

19. How would you limit the results of an SQL query to the first 5 rows?

Answer:

The LIMIT clause restricts the number of rows returned by the query.

SELECT * FROM Employees LIMIT 5;

20. How can you create an alias for a column in an SQL query?

Answer:

The AS keyword creates a temporary alias for a column in the result set.

SELECT Name AS EmployeeName FROM Employees;

21. Write an SQL query to count the total number of employees in the Employees table.

Answer:

The COUNT function returns the number of rows that match the query criteria.

SELECT COUNT(*) FROM Employees;

22. How do you calculate the average age of employees in the Employees table?

Answer:

The AVG function calculates the average value of a numeric column.

SELECT AVG(Age) FROM Employees;

23. Write an SQL query to group employees by age and count how many employees are in each age group.

Answer:

The GROUP BY clause groups rows sharing a property, and the COUNT function counts the rows in each group.

SELECT Age, COUNT(*) FROM Employees GROUP BY Age;

24. How would you filter grouped data to only include groups with more than 1 employee?

Answer:

The HAVING clause filters groups based on the aggregate functions used.

SELECT Age, COUNT(*) FROM Employees GROUP BY Age HAVING COUNT(*) > 1;

25. Write an SQL query to find the maximum salary in the Employees table.

Answer:

The MAX function returns the highest value in the specified column.

SELECT MAX(Salary) FROM Employees;

26. What is an INNER JOIN, and how do you use it to combine data from two tables?

Answer:

INNER JOIN returns rows when there is a match in both tables based on a related column.

SELECT Employees.Name, Departments.DepartmentName
FROM Employees
INNER JOIN Departments ON Employees.DepartmentID = Departments.ID;

27. Write an SQL query using a LEFT JOIN to return all employees and their departments, even if some employees are not assigned to any department.

Answer:

LEFT JOIN returns all rows from the left table and matched rows from the right table.

SELECT Employees.Name, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments ON Employees.DepartmentID = Departments.ID;

28. How do you use a RIGHT JOIN to retrieve data in SQL?

Answer:

RIGHT JOIN returns all rows from the right table and the matched rows from the left table.

SELECT Employees.Name, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments ON Employees.DepartmentID = Departments.ID;

29. Write an SQL query to perform a FULL OUTER JOIN between two tables.

Answer:

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

SELECT Employees.Name, Departments.DepartmentName
FROM Employees
FULL OUTER JOIN Departments ON Employees.DepartmentID = Departments.ID;

30. How would you use a SELF JOIN to find employees who share the same manager?

Answer:

SELF JOIN joins a table with itself to relate rows within the same table.

SELECT E1.Name AS Employee, E2.Name AS Manager
FROM Employees E1
INNER JOIN Employees E2 ON E1.ManagerID = E2.ID;

31. What is a subquery, and how is it used in the WHERE clause?

Answer:

A subquery is a query nested inside another query, often used to filter results in the outer query.

SELECT Name FROM Employees WHERE ID IN (SELECT ManagerID FROM Employees);

32. Write an SQL query using a subquery to find the highest salary in the Employees table.

Answer:

The subquery finds the maximum salary, which is then used in the WHERE clause of the outer query.

SELECT * FROM Employees WHERE Salary = (SELECT MAX(Salary) FROM Employees);

33. How do you use a subquery in the SELECT clause?

Answer:

A subquery in the SELECT clause can calculate a value for each row.

SELECT Name, (SELECT AVG(Salary) FROM Employees) AS AvgSalary FROM Employees;

34. Explain the difference between a correlated subquery and a regular subquery.

Answer:

A correlated subquery depends on the outer query, while a regular subquery is independent.

SELECT Name FROM Employees E1 WHERE Salary > (SELECT AVG(Salary) FROM Employees E2 WHERE E1.DepartmentID = E2.DepartmentID);

35. Write a SQL query using a subquery in the FROM clause.

Answer:

The subquery in the FROM clause serves as a derived table for the outer query.

SELECT DepartmentID, AVG(Salary)
FROM (SELECT DepartmentID, Salary FROM Employees) AS DeptSalaries
GROUP BY DepartmentID;

36. What is a primary key, and how do you define it in a table?

Answer:

A primary key uniquely identifies each row in a table and is defined using the PRIMARY KEY constraint.

CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100)
);

37. Write an SQL statement to create a foreign key constraint.

Answer:

A foreign key constraint enforces a link between two tables by referencing the primary key of another table.

CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
EmployeeID INT,
FOREIGN KEY (EmployeeID) REFERENCES Employees(ID)
);

38. How do you ensure that a column cannot have NULL values?

Answer:

The NOT NULL constraint ensures that a column must contain a value.

CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL
);

39. What is the UNIQUE constraint, and how do you use it?

Answer:

The UNIQUE constraint ensures that all values in a column are different.

CREATE TABLE Employees (
ID INT PRIMARY KEY,
Email VARCHAR(100) UNIQUE
);

40. Write an SQL statement to set a default value for a column.

Answer:

The DEFAULT constraint sets a default value for a column if no value is provided.

CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Status VARCHAR(10) DEFAULT ‘Active’
);

41. How do you add a new column to an existing table?

Answer:

Use the ALTER TABLE statement to add a new column.

ALTER TABLE Employees ADD COLUMN Salary DECIMAL(10, 2);

42. Write an SQL statement to drop a column from a table.

Answer:

The ALTER TABLE statement is used to remove a column from a table.

ALTER TABLE Employees DROP COLUMN Salary;

43. How do you change the data type of an existing column?

Answer:

The ALTER TABLE statement with MODIFY or ALTER COLUMN (depending on the SQL dialect) changes a column’s data type.

ALTER TABLE Employees MODIFY COLUMN Age VARCHAR(3);

44. Write an SQL query to add a NOT NULL constraint to an existing column.

Answer:

The ALTER TABLE statement is used to enforce a NOT NULL constraint on an existing column.

ALTER TABLE Employees MODIFY COLUMN Name VARCHAR(100) NOT NULL;

45. How do you drop a unique constraint from a column?

Answer:

Use the ALTER TABLE statement along with DROP CONSTRAINT to remove a unique constraint.

ALTER TABLE Employees DROP CONSTRAINT unique_constraint_name;

46. What is an index in SQL, and how do you create one?

Answer:

An index improves query performance by allowing faster data retrieval, created using the CREATE INDEX statement.

CREATE INDEX idx_name ON Employees (Name);

47. How do you drop an index from a table?

Answer:

Use the DROP INDEX statement to remove an index from a table.

DROP INDEX idx_name ON Employees;

48. What are some basic concepts of query optimization?

Answer:

Query optimization includes techniques like indexing, avoiding unnecessary columns, and using joins effectively.

49. How do indexes affect the performance of insert and update operations?

Answer:

Indexes can slow down insert and update operations because the index itself needs to be updated.

50. Write an SQL statement to create a composite index on multiple columns.

Answer:

A composite index is created using multiple columns to optimize queries filtering on those columns.

CREATE INDEX idx_name_age ON Employees (Name, Age);

Final Words

Getting ready for an interview can feel overwhelming, but going through these SQL fresher interview questions can help you feel more confident. This guide focuses on the kinds of SQL developer interview questions for fresher roles that you’re likely to face.

Don’t forget to practice the SQL basic coding interview questions too! With the right preparation, you’ll ace your SQL interview and take that important step in your career.


Frequently Asked Questions

1. What are the most common interview questions for SQL?

The most common SQL interview questions often cover basic queries, joins, subqueries, and data manipulation commands like SELECT, INSERT, UPDATE, and DELETE.

2. What are the important SQL topics freshers should focus on for interviews?

Freshers should focus on understanding database normalization, joins, indexing, aggregate functions, and writing complex SQL queries.

3. How should freshers prepare for SQL technical interviews?

Freshers should practice writing and optimizing SQL queries, understand database concepts, and work on sample interview questions.

4. What strategies can freshers use to solve SQL coding questions during interviews?

Freshers should break down the problem, use proper joins, and test queries with sample data to ensure accuracy.

5. Should freshers prepare for advanced SQL topics in interviews?

Yes, depending on the role, freshers might need to know advanced topics like stored procedures, triggers, and transaction management.


Explore More SQL Resources

Explore More Interview Questions

zen-class vertical-ad
author

Thirumoorthy

Thirumoorthy serves as a teacher and coach. He obtained a 99 percentile on the CAT. He cleared numerous IT jobs and public sector job interviews, but he still decided to pursue a career in education. He desires to elevate the underprivileged sections of society through education

Subscribe

Thirumoorthy serves as a teacher and coach. He obtained a 99 percentile on the CAT. He cleared numerous IT jobs and public sector job interviews, but he still decided to pursue a career in education. He desires to elevate the underprivileged sections of society through education

Subscribe