SQL Interview Questions for Freshers: Questions You Should Prepare

SQL interview questions for freshers

SQL Interview Questions for Freshers: 25 Questions to Prepare

If you’re a fresher preparing for an IT interview, SQL is one of those skills you should not ignore.

You don’t have to be an expert in SQL to attend an entry-level interview. But you should be comfortable with the basics and know how to write simple queries.

Depending on the job, the interviewer may ask you questions about SELECT, WHERE, joins, GROUP BY, subqueries, or even ask you to solve a small SQL problem.

In this article, we’ll go through some common SQL interview questions for freshers in simple language.

I’ve also included examples wherever they make the question easier to understand.

1. What is SQL?

SQL stands for Structured Query Language.

It is used to work with data stored in relational databases.

With SQL, you can:

  • Read data
  • Add data
  • Update data
  • Delete data
  • Create tables
  • Modify tables
  • Filter data
  • Join data from different tables
IDNameDepartmentSalary
1RahulIT50000
2PriyaHR45000
3ArjunIT60000

If you want to see all employees, you can write:

SELECT * FROM employees;

That’s the basic idea of SQL: you ask the database for the data you need.

2. What is a database?

A database is a place where data is stored and organized so that it can be easily accessed and managed.

For example, a company might have separate tables for:

  • Employees
  • Customers
  • Products
  • Orders
  • Departments
  • Payments

Instead of keeping thousands of records in random files, a database keeps the information organized.


3. What is a table?

A table is where data is stored in a relational database.

It consists of rows and columns.

For example:

employee_idnamesalary
101Rahul50000
102Priya55000
103Arjun60000

Here:

  • employee_id, name, and salary are columns.
  • Each employee record is a row.

If you’re new to databases, you can think of a table as being similar to a spreadsheet.


4. What is a primary key?

A primary key is used to uniquely identify each record in a table.

For example:

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(100),
    salary INT
);

Here, employee_id is the primary key.

Two employees should not have the same primary key value.

A primary key also cannot contain NULL.

Simple interview answer

A primary key is a column or combination of columns that uniquely identifies each record in a table.


5. What is a foreign key?

A foreign key is used to create a relationship between two tables.

Suppose we have these two tables.

Employees

employee_idnamedepartment_id
1Rahul10
2Priya20

Departments

department_iddepartment_name
10IT
20HR

department_id in the Employees table can refer to department_id in the Departments table.

That’s how tables can be connected.


6. What is NULL in SQL?

NULL means that a value is missing or unknown.

For example:

NameManager
RahulNULL
PriyaAmit

Rahul’s manager information is not available.

To find NULL values, use:

SELECT *
FROM employees
WHERE manager IS NULL;

Don’t write:

WHERE manager = NULL;

Use IS NULL or IS NOT NULL.


7. What is the SELECT statement?

SELECT is used to retrieve data from a table.

To get everything:

SELECT * FROM employees;

To get only specific columns:

SELECT name, salary
FROM employees;

This is probably one of the first SQL commands you should learn.


8. What is the WHERE clause?

WHERE is used to filter records.

For example, if you want employees whose salary is greater than 50,000:

SELECT *
FROM employees
WHERE salary > 50000;

Only the matching records will be returned.

You can also use conditions such as:

=
>
<
>=
<=
<>

along with AND, OR, and NOT.


9. What is ORDER BY?

ORDER BY is used to sort the result.

For example, to show employees from the highest salary to the lowest:

SELECT name, salary
FROM employees
ORDER BY salary DESC;

For lowest to highest:

SELECT name, salary
FROM employees
ORDER BY salary ASC;

ASC means ascending.

DESC means descending.


10. What is GROUP BY?

GROUP BY is used when you want to group records based on a column.

For example, suppose you want to know how many employees are in each department:

SELECT department, COUNT(*)
FROM employees
GROUP BY department;

The database groups employees by department and then counts them.

You will often see GROUP BY used with functions such as:

  • COUNT()
  • SUM()
  • AVG()
  • MIN()
  • MAX()

11. What are aggregate functions?

Aggregate functions perform calculations on multiple rows.

Some commonly used aggregate functions are:

FunctionWhat it does
COUNT()Counts records
SUM()Adds values
AVG()Finds average
MAX()Finds highest value
MIN()Finds lowest value

Example:

SELECT AVG(salary)
FROM employees;

This gives the average salary.


12. What is HAVING?

HAVING is used to filter groups.

For example:

SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

This returns only departments that have more than five employees.

A simple way to remember it:

WHERE filters rows.

HAVING filters groups.

This is a very common interview question, so make sure you understand the difference.


13. What is a JOIN?

A JOIN is used to get related data from different tables.

For example, you might have:

Employees

IDNameDept_ID
1Rahul10
2Priya20

Departments

Dept_IDDepartment
10IT
20HR

You can combine the information using a JOIN:

SELECT
    e.name,
    d.department
FROM employees e
JOIN departments d
    ON e.dept_id = d.dept_id;

The result would look like:

NameDepartment
RahulIT
PriyaHR

14. What are the different types of JOINs?

The main JOINs you should know are:

INNER JOIN

Returns records that have a match in both tables.

LEFT JOIN

Returns all records from the left table and matching records from the right table.

RIGHT JOIN

Returns all records from the right table and matching records from the left table.

FULL OUTER JOIN

Returns matching and non-matching records from both tables, where supported by the database.

SELF JOIN

A table is joined with itself.

For a fresher interview, INNER JOIN and LEFT JOIN are especially important to understand properly.


15. What is the difference between INNER JOIN and LEFT JOIN?

This is a question you should definitely prepare.

Suppose you have 10 employees but only 8 of them have a matching department.

With an INNER JOIN, you get the 8 matching employees.

With a LEFT JOIN, you get all 10 employees. The two employees without a matching department will have NULL for the department columns.

Easy way to remember:

INNER JOIN:
Give me matching records.

LEFT JOIN:
Give me everything from the left table, even if there is no match.


16. What is a subquery?

A subquery is simply a query inside another query.

For example, suppose you want to find employees who earn more than the average salary.

You can write:

SELECT name, salary
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
);

The inner query finds the average salary.

The outer query finds employees earning more than that average.


17. How do you find the second-highest salary?

This is a very common SQL interview question.

One simple approach is:

SELECT MAX(salary) AS second_highest_salary
FROM employees
WHERE salary < (
    SELECT MAX(salary)
    FROM employees
);

Here, the inner query finds the highest salary.

The outer query then finds the highest salary below that value.

Interview tip

Don’t just memorize this query.

Understand what each part is doing. Interviewers may change the question slightly and ask you to find the third-highest salary, highest salary by department, or top three salaries.


18. How do you find duplicate records?

Suppose you have an email column and want to find emails that appear more than once.

You can use:

SELECT email, COUNT(*) AS total
FROM employees
GROUP BY email
HAVING COUNT(*) > 1;

The important part here is:

HAVING COUNT(*) > 1

It tells SQL to return only values that occur multiple times.


19. What is DISTINCT?

DISTINCT removes duplicate values from the result.

For example:

SELECT DISTINCT department
FROM employees;

If the table contains:

IT
IT
HR
IT
Finance

the query returns:

IT
HR
Finance

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

This is another question that comes up regularly.

DELETE

Removes rows from a table.

DELETE FROM employees
WHERE department = 'HR';

You can use a WHERE condition to remove selected rows.

TRUNCATE

Removes all rows from a table while keeping the table structure, subject to the database system’s rules.

TRUNCATE TABLE employees;

DROP

Removes the table itself.

DROP TABLE employees;

Easy way to remember:

DELETE → remove rows

TRUNCATE → remove all rows

DROP → remove the table

Transaction and rollback behavior can differ between database systems, so be careful with blanket statements about whether TRUNCATE can be rolled back.


21. What is a CTE?

CTE stands for Common Table Expression.

It lets you give a temporary name to the result of a query.

Example:

WITH high_salary AS (
    SELECT *
    FROM employees
    WHERE salary > 50000
)
SELECT *
FROM high_salary;

CTEs are useful when a query becomes complicated and you want to make it easier to read.


22. What is a window function?

Window functions are used to perform calculations across related rows without combining those rows into one row.

For example:

SELECT
    name,
    department,
    salary,
    ROW_NUMBER() OVER (
        PARTITION BY department
        ORDER BY salary DESC
    ) AS row_num
FROM employees;

Here, employees are numbered based on their salary within each department.

Common window functions include:

  • ROW_NUMBER()
  • RANK()
  • DENSE_RANK()
  • LAG()
  • LEAD()

If you’re applying for a data analyst or data engineer role, spend extra time practicing window functions.


23. What is the difference between RANK and DENSE_RANK?

Suppose salaries are:

60000
60000
50000
40000

Using RANK():

1
1
3
4

Using DENSE_RANK():

1
1
2
3

The difference is that RANK() skips a number after a tie, while DENSE_RANK() doesn’t.


24. What is an index?

An index helps a database find data more efficiently for suitable queries.

For example:

CREATE INDEX idx_email
ON employees(email);

Indexes can improve read performance, but they aren’t free.

They use additional storage and can add work when data is inserted, updated, or deleted.

So you shouldn’t simply create indexes on every column.


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

Both are used to combine results from multiple SELECT statements.

UNION

Removes duplicate rows.

SELECT city FROM customers
UNION
SELECT city FROM suppliers;

UNION ALL

Keeps duplicates.

SELECT city FROM customers
UNION ALL
SELECT city FROM suppliers;

If you don’t need duplicate removal, UNION ALL is generally the more direct choice.

SQL Queries You Should Practice Before an Interview

Knowing definitions is good, but writing queries is more important.

Before your interview, practice these:

Beginner level

  1. Find all employees from the IT department.
  2. Find employees earning more than ₹50,000.
  3. Sort employees by salary.
  4. Find the highest salary.
  5. Find the lowest salary.
  6. Count the number of employees.
  7. Find the average salary.

Intermediate level

  1. Find the second-highest salary.
  2. Find duplicate records.
  3. Find employees earning more than the average salary.
  4. Find the highest salary in each department.
  5. Find departments having more than five employees.
  6. Find employees who don’t have a matching department.
  7. Find the top three salaries.

Important SQL topics

  1. JOINs
  2. Subqueries
  3. CTEs
  4. Window functions
  5. CASE statements
  6. GROUP BY and HAVING

How Should Freshers Prepare for SQL Interviews?

Don’t try to learn everything in one day.

Start with the basics:

Day 1: SELECT, WHERE, ORDER BY

Day 2: Aggregate functions and GROUP BY

Day 3: JOINs

Day 4: Subqueries and CTEs

Day 5: Window functions

Day 6: Practice SQL problems

Day 7: Take a mock interview and explain your answers aloud

Even if you have studied SQL before, writing queries yourself is important.

Reading a query and writing the same query from memory are two very different things.


A Small Tip for Your First SQL Interview

If the interviewer gives you a SQL problem and you don’t immediately know the answer, don’t panic.

Start by explaining what you’re trying to find.

For example:

“First, I need to identify the department with the highest salary. I’ll group the records by department and then use an aggregate function.”

Thinking aloud can help the interviewer understand your approach.

You don’t have to solve every question instantly.

What matters is showing that you understand the problem and can work toward a solution.


Frequently Asked Questions

Is SQL important for freshers?

SQL can be important for many entry-level IT roles, especially software development, testing, data analytics, data engineering, and database-related positions. The exact requirement depends on the job.

Which SQL topics should freshers learn first?

Start with SELECT, WHERE, ORDER BY, aggregate functions, GROUP BY, HAVING, and JOINs. After that, move to subqueries, CTEs and window functions.

Is SQL difficult for beginners?

The basics of SQL are fairly easy to start with. The difficulty increases as you work with multiple tables and more complex problems. Regular practice makes a big difference.

What SQL questions are commonly asked in fresher interviews?

Interviewers commonly ask about JOINs, primary and foreign keys, GROUP BY, WHERE vs HAVING, aggregate functions, subqueries, duplicate records, second-highest salary, and basic SQL queries.


Final Thoughts

If you’re a fresher preparing for an SQL interview, don’t focus only on memorizing definitions.

Try to write queries yourself.

Start with simple questions and slowly move toward joins, subqueries and window functions.

And whenever you learn a new SQL concept, try to think of a small real-world example. That will make it much easier to remember during an interview.

Your goal isn’t to memorize 100 SQL answers. Your goal is to understand how to get the answer from the data.


If you want resume tips CLICK HERE

Latest Jobs Check Out Our Fresher Hiring Updates

Subscribe to our YouTube Channel

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top