EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL Interview Questions
 

SQL Interview Questions

Priya Pedamkar
Article byPriya Pedamkar

SQL Interview Questions

The demand for SQL professionals continues to grow as organizations rely on data for decision-making, analytics, and application development. If you are preparing for an SQL interview in 2026, understanding the latest interview trends and practicing commonly asked questions can give you a strong advantage.

 

 

To help you prepare, we have compiled a comprehensive list of the most frequently asked SQL Interview Questions and Answers for 2026. Whether you are a fresher starting your career or an experienced database professional looking for a new opportunity, these interview questions will help you strengthen your SQL concepts, improve your confidence, and increase your chances of landing your dream job.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

These top interview questions are divided into two parts:

Part 1 – SQL Interview Questions (Basic)

This first part covers basic interview questions and answers.

Q1. What is SQL?

Answer:
SQL stands for Structured Query Language, and it is used to communicate with databases. This standard language performs several tasks: retrieving, updating, inserting, and deleting data from a database.

Q2. Write the query to find the employee record with the highest salary.

Answer:

Select * from table_name where salary = (select max(salary) from table_name);

For example

Select * from employee where salary =(select max(salary) from employee);

Q3. Write the query to find the 2nd-highest salary in the employee table.

Answer:
These are the basic SQL interview questions. There are multiple ways to solve this question; the three are the easiest solutions.

1st: Select max (salary) from employee where salary not in (select max(salary) from employee).

Note: This solution only finds the 2nd-highest salary. If the question gets changed, find the 3rd- or 4th-highest salary; otherwise, this will not work. It would be best if you executed the query below to find the nth highest salary.

2nd: Select salary from employee where salary in (select salary from employee where level = &topnth connect by prior salary> Salary group by level).

Note: If you run the query above, it will prompt for the value of topnth. If you enter 2, it will show the result for 2; if you enter 3, it will show the result for 3. Likewise, this query is generic.

3rd: Select salary from employee where salary in (select salary from (select unique salary from employee order by salary desc) group by rownum, salary having rownum = &topnth).

Execute the same as the 2nd query.

Q4. Write the query to find the 2nd lowest salary in the employee table.

Answer:
There are multiple ways to solve this question; the two below are the easiest.

1st: Select min (salary) from employee where salary is not in (select min(salary) from employee).

Note: This solution only finds the 2nd-lowest salary. If the question gets changed, find the 3rd or 4th lowest salary, then this will not work. You need to execute the following query to find the nth highest salary.

2nd: Select salary from employee where salary is in (select salary from employee where level = &lownth connect by prior salary < Salary group by level).

Note: If you run the above query, it will ask to enter the value of lownth. If you enter 2, it will show the result for 2; if you enter 3, it will show the result for 3. Likewise, this query is generic.

Q5. What is the difference between the NVL and NVL2 functions?

Answer:
Both functions are used to convert a NULL value to an actual value

NVL: Syntax

NVL (EXPR1, EXPR2)
  • EXPR1: Is the source value or expression that may contain NULL.
  • EXPR2: This is the target value for converting NULL.

Note: If EXPR1 is character data, then EXPR2 may be any data type.

For example:

select NVL (100,200) from dual

Output: 100

Select NVL(null,200) from dual;

Output: 200

NVL2: Syntax

NVL2(expr1,expr2,expr3)

If expr1 is not null, NVL2 returns expr2. If expr1 is null, then NVL2 returns expr3.

The data type of the return value is always the same as the data type of expr2 unless expr2 is character data.

Example:

select nvl2(100,200,300) from dual;

Output: 200

Select nvl2 (null,200,300) from dual;

Output:

300

Q6. Write a query to find distinct domains in the email column; consider the employee table below, for example.

Name Email
Anubhav [email protected]
Basant [email protected]
Sumit [email protected]
Amit [email protected]

So write the query to get results only for @gmail.com, @yahoo.in, and @hotmail.com (since we have two gmail.com addresses and need to fetch only distinct domains).

Answer:

Select distinct (substr (Email, Instr (Email,’@’,1,1))) from employee;

Part 2 – SQL Interview Questions (Advanced)

Let us now have a look at the advanced Interview Questions.

Q7. Write a query to find duplicate names and their frequencies in the Employee table below.

Use the GROUP BY clause to group records by name and the COUNT() function to count how many times each name appears. Then, use the HAVING clause to display only the names that occur more than once.

Name Age Salary
Anubhav 26 50000
Anurag 29 60000
Basant 27 40000
Rahul 28 45000
Anubhav 27 48000

Answer:

Select Name, count(1) as frequency from Employee
Group by Name having count(1) > 1

Q8. Write the query to remove the duplicates from a table without using a temporary table.

Answer:
These are advanced SQL interview questions. Delete from Employee where name in (Select name from employee group by age, salary having count(*) > 1));

Or

Delete from employee where rowid not in (select max (rowid) from employee group by name);

Q9. Write the query to find odd and even records from the table.

Answer:

For even records:

SELECT *
FROM employee
WHERE empno IN (
    SELECT empno
    FROM employee
    GROUP BY empno, rownum
    HAVING MOD(rownum, 2) = 0
);

For odd records:

SELECT *
FROM employee
WHERE empno IN (
    SELECT empno
    FROM employee
    GROUP BY empno, rownum
    HAVING MOD(rownum, 2) != 0
);

MOD(rownum, 2) returns 0 for even row numbers and 1 for odd row numbers, allowing you to retrieve even or odd records.

Q10. Write a SQL query to create a new table with data and structure copied from another table, and create an empty table with the same structure as some other table.

Answer:
Create a new table with data and structure copied from another table

Select * into new table from an existing table;

Create an empty table with the same structure as some other table

Select * into new_table from existing_table where 1=2;

Or

Create table new table like an existing table;

Q11. Write a SQL query to find the common records between two tables.

Answer:

Use the INTERSECT operator to return only the rows that exist in both tables. Both SELECT statements must have the same number of columns with compatible data types.

Select * from table_one
Intersect
Select * from table_two;

The INTERSECT operator compares the results of both queries and returns only the matching records. It automatically removes duplicate rows from the final result.

Q12. Write an SQL query to find records that are present in one table but missing from another.

Answer:

Use the MINUS operator to return rows that exist in the first table but not in the second table.

Select * from table_one
Minus
Select * from table_two;

Q13. What is SQL? Describe the importance of SQL in RDBMS?

Answer:

SQL is a Structured Query Language. SQL is used to communicate with the database. SQL is the heart of a relational database management system (RDBMS). It is the language used to perform all the operations in a relational database. When you issue a command to the RDBMS in SQL, the RDBMS interprets your command and takes necessary actions.

Q14. What is the difference between SQL and PL/SQL?

Answer:

SQL PL/SQL
It is a Structured Query Language. It is Procedural language, an extension of SQL.
In SQL, you can execute a single command at a time. In pl/SQL, you can execute multiple lines of code at a time.
In SQL, commands are executed using DDL (Data Definition Language) and DML (Data Manipulation Language). In PL/SQL, you can write multiple code lines that have procedures, functions, packages, etc.
SQL commands can be used in PL/SQL. PL/SQL cannot be used in SQL.
An example of SQL is:

Select * from Table_name where condition

An example of PL/SQL is:

BEGIN
dbms_output.put_line (‘HELLO EDUCBA WORLD’);
END;
/

Q15. What are the main components of SQL?

Answer:

The main components of SQL are DDL, DML, DCL (Data Control Language), TCL (Transaction Control Language).

Data Definition Language: Tables are the only way to store data; all information must be organized into tables. Suppose you want to store some information (Name, city) about the company in the database. To store this, you need to create a table; you can do so using the table command.

Code:

Create table company (name char (10), city char (10));

Using DDL, you can also alter or drop objects.

Data Manipulation Language: DML, as the name suggests, allows you to manipulate data in an existing table. You can perform many operations on a table using DML, such as insertion, updating, and deletion.

  • Adding a row to a table

Code:

Insert into company values (‘XYZ’, ‘Sydney’);

  • Updating data in a table

Code:

Update company set city = ‘Melbourne’ where name = ‘XYZ’

Data Control Language:

  • DCL: This allows you to control access to the data.
  • Grant: Grants permission to one or more users to operate.
  • Revoke: Withdraw the access permission given by the grant statement.

Transaction Control Language: TCL includes commit, rollback, and save points to data.

Q16. What is the difference between the delete and the truncate commands?

Answer:

  • The DELETE command can be used to delete rows from a particular table, and the WHERE clause can be used for a condition. Commit and Rollback functions can be performed on the delete command after the delete statement.
  • TRUNCATE is used to remove all rows from the table. The Truncate operation cannot be rolled back.

Q17. Write a SQL query to find the 3rd highest salary from the table without using the TOP/limit keyword?

Answer:
Use a correlated subquery to count the number of distinct salaries greater than the current salary. If exactly two distinct salaries are higher, then the current salary is the 3rd-highest.

SELECT salary
FROM EDUCBA_Employee E1
WHERE 2 = (
    SELECT COUNT(DISTINCT E2.salary)
    FROM EDUCBA_Employee E2
    WHERE E2.salary > E1.salary
);

The subquery counts the distinct salaries that are higher than each employee’s salary. When the count equals 2, it means only two unique salaries rank above it, so the query returns the 3rd highest salary.

Q18. How will you perform pattern-matching operations in SQL?

Answer: 

The LIKE operator is used for pattern matching, and it can be used in two ways.

  • %: It matches zero or more characters.

Code:

Select * from employee where name like ‘X%’

  • _(Underscore): It matches exactly one character.

Code:

Select * from employee where name like ‘XY_’

Q19. Write a query to get employee names ending with a vowel.

Answer:

Use a pattern-matching condition to find employee names whose last character is a vowel (A, E, I, O, or U).

Code:

Select EMP_ID, EMP_NAME from EDUCBA_EMPLOYEE where EMP_NAME like '%[aeiou]'

The % wildcard matches any number of characters before the last letter. The [AEIOUaeiou] pattern checks whether the final character of the employee name is a vowel.

Q20. How will you copy rows from one table to another table?

Answer:

The INSERT command will be used to add a row to a table by copying from another table. In this case, a subquery is used in place of the VALUES clause.

Q21. What is the difference between the ‘WHERE’ clause and the ‘HAVING’ clause?

Answer:

HAVING clause can only be used with the SELECT statement. HAVING clause is used with the GROUP BY clause, and if the GROUP BY clause is not used, then the HAVING clause behaves like a WHERE clause. The HAVING clause works with the GROUP BY clause to filter grouped results, while the WHERE clause filters individual rows after the FROM clause and before the GROUP BY clause groups the data.

Q22. How will you get a first name, salary, and round the salary to the nearest thousand?

Answer:

Code:

SELECT FIRST_NAME, SALARY, ROUND (SALARY, -3) FROM EDUCBA_EMPLOYEE;

Q23. Display the first name and experience of the employees?

Answer:

Code:

SELECT FIRST_NAME, HIRE_DATE, FLOOR((SYSDATE-HIRE_DATE)/365) FROM EDUCBA_EMPLOYEE;

Q24. Write a query to get the first name and last name after converting the first letter of each name to upper case and the rest to lower case.

Answer:

Code:

SELECT INITCAP(FIRST_NAME), INITCAP(LAST_NAME) FROM EDUCBA_EMPLOYEE;

Q25. Display the length of the first name for employees where the last name contains the character ‘b’ after the 3rd position?

Answer:

Code:

SELECT FIRST_NAME, LAST_NAME FROM EDUCBA_EMPLOYEE WHERE INSTR(LAST_NAME,'B') > 3;

Q26. Change the salary of employee 115 to 8000 if the existing salary is less than 6000?

Answer:

Code:

UPDATE EDUCBA_EMPLOYEE SET SALARY = 8000 WHERE EMPLOYEE_ID = 115 AND SALARY < 6000;

Q27. How will you insert a new employee into the employees with all the required details?

Answer:

Code:

INSERT INTO EDUCBA_EMPLOYEE

(EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, PHONE_NUMBER, SALARY) VALUES

(207, ‘ANGELA’, ‘SNYDER’, ‘ANGELA’, ‘215 253 4737’, 12000);

The INSERT INTO statement adds a new row to the EDUCBA_EMPLOYEE table. It specifies the columns that will receive values, followed by the corresponding values in the VALUES clause. The values must appear in the same order as the listed columns and have compatible data types.

Q28. Display employees who joined in May?

Answer:

Use the TO_CHAR() function to extract the month from the HIRE_DATE column and filter the records where the month is May.

Code:

SELECT *
FROM EDUCBA_EMPLOYEE
WHERE TO_CHAR(HIRE_DATE, 'MON') = 'MAY';

The TO_CHAR(HIRE_DATE, ‘MON’) function converts the date into a three-letter month abbreviation (such as JAN, FEB, or MAY). The WHERE clause then returns only the employees whose hire month is May.

Q29. What is the meaning of “TRIGGER” in SQL?

Answer:

A TRIGGER is a special type of stored program that automatically executes when a specific event, such as an INSERT, UPDATE, or DELETE operation, occurs on a table or view. Developers commonly use triggers to enforce business rules, validate data, maintain audit logs, or automatically update related tables.

Example:

CREATE TRIGGER trg_UpdateLog
AFTER UPDATE
ON Employee
FOR EACH ROW
BEGIN
INSERT INTO Employee_Log (EMP_ID, Updated_On)
VALUES (NEW.EMP_ID, CURRENT_TIMESTAMP);
END;

Recommended Articles

This guide covers essential SQL Interview Questions and Answers to help candidates prepare for interviews with confidence. Explore the articles below to strengthen your SQL knowledge, improve your query-writing skills, and gain a deeper understanding of important database concepts.

  1. TSQL Interview Questions
  2. NoSQL Interview Questions And Answers
  3. Cloud Computing Interview Questions
  4. Interview Question for Java

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2026 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

🚀 Limited Time Offer! - 🎁 ENROLL NOW