SQL Server Interview Questions

SQL Server Interview Questions: SQL Server is a popular relational database management system developed by Microsoft. Organizations of all sizes widely use it to manage and store their data. Given its widespread use, many companies are seeking professionals who are well-versed in using SQL Server to manage their databases. As a result, interviewers often ask candidates questions related to SQL Server’s functionalities and best practices.

This article provides an overview of common SQL Server interview questions that candidates may encounter during a job interview. By familiarizing themselves with these questions and their answers, candidates can prepare themselves to demonstrate their expertise in SQL Server and increase their chances of securing a job offer.

SQL Server Interview Questions For Experience

Some of the most commonly asked SQL Server interview questions for experienced professionals include database design and modeling, query optimization, stored procedures and functions, indexing, security and permissions, and disaster recovery planning. Interviewers often look for a deep understanding of SQL Server’s functionalities and the ability to troubleshoot issues that may arise in a production environment.

  • The latest entry in the table?
  • Display the max of avg salary from each dept?
  • How to update all columns at a time?
  • How to find files in the directory/subdirectory and the first 5 records in Unix?
  • How to kill the process?
  • How to know how many members & login into your system in UNIX?
  • How to see hidden files in UNIX?
  • How to know the background/foreground process in Unix?
  • My table has 100 rows then I want to find out 3 to 7 rows only?
  • How do find out no of columns in the partial table?

SQL Server Interview Questions For 5 Years Experience

The concept of SQL is one of the most important concepts in databases. It provides a standardized interface that allows developers to work with the data stored in a database, using SQL statements. As part of the interview process, you must demonstrate your knowledge of this topic. Here are some examples of common questions that you may be asked about SQL and how to prepare for them.

  • Tell me about your project architecture and roles and responsibilities in your project?
  • What is the difference between union all and full outer join?
  • What is meant by cross join give an example?
  • What is meant by a complex subquery?
  • Job salesman clerk salesman: write a query to display the name which contains two „s‟ without using like?
  • My project tables have more than 5000000 records then how can u do validations without using sample testing?
  • Explain set operators with an example?
  • In my project we are having more than 5000 tables, data is loaded into the tables by using an Informatica tool, but due to some wrong connections data is not loaded in some tables. How do you display the tables in which data is not loaded?
  • What are the requirements you have for your project?
  • Write grep command with an example?
  • How do you count the number of files in a Unix?
  • Diff between substr and instr?
  • Diff between the primary key and the surrogate key?
  • How do you join dim and fact tables explain with an example?
  • Explain about scd1, scd2, scd3?
  • Explain normalization with an example?
  • How do you get the data from the client-side either weekly or monthly basics?
  • emp dept empno sal deptno deptno dname loc
  • write a query to display max sal
  • where deptno=10 and loc= Chicago and dname=sales

SQL Server Interview Questions For 7 Years Experience

  • What are union and union all? a- table: 1 to 50 records b table: 1 to 50 records and another 50 records if you use union and union all that is the output?
  • Take as the example above two tables I want to perform minus and minus all that is the output?
  • Take the above tables by using all joins? What is the output?
  • A table: Mahesh pati and b table: Mahesh if I write the like select col from a minus select col from b what is the output?
  • Select * from emp where eid=null? Is it working?
  • Given the table a table having data like 1 2 3 ? 5? I want to insert a null string instead of the “?‟ symbol?
  • Similar to the above table provides “_‟ spaces?
  • ename sal a 1k b 2k c 3k d 4k I want to find out highest salary and don‟t user subquery, rank By using row numbers?
  • Table a name Mahesh-sivala Karthik-kumar Rahul-Sharma I want the separate name and initial name I want the separate name and domain name only which having not „@‟ symbol?
  • Select a,b,c,d from table group by 1,2,3 what is the o/p?
  • Select a,b,c,d from table group by 1,2,3,4 what is the o/p?
  • How to compare 10 tables in the staging and data warehouse?
  • I want to sort data in descending order? Unix
  • If want to find out the first 5 files and the last 5 records in Unix?
  • What is the grep command?
  • I want to find files in the Unix box? What is the command?
  • what is SCD (slowly changing dimensional)?
  • How you are moving one file to another file in Unix?
  • I want to delete 2nd records without using row num, row id?

SQL Questions for Interview

  • What is primary key and unique key?
  • How many joins are there any difference?
  • Difference between join and union?
  • Select top 3 max salary employees by depts.
  • Difference between where and having and use of it?
  • Different types of Date functions?
  • What are the grouping functions?
  • Which function is used to get the current date?
  • What is a sub query?
  • What is indexing?
  • What is Database?
  • What is DBMS?
  • What is RDBMS? How is it different from DBMS?
  • What is SQL?
  • What is the difference between SQL and MySQL?
  • What are Tables and Fields?
  • What are Constraints in SQL?
  • What is a Primary Key?
  • What is a UNIQUE constraint?
  • What is a Foreign Key?
  • What is a Join? List its different types.
  • What is a Self-Join?
  • What is a Cross-Join?
  • What is an Index? Explain its different types.
  • What is the difference between Clustered and Non-clustered index?
  • What is Data Integrity?
  • What is a Query?
  • What is a Subquery? What are its types?
  • What is the SELECT statement?
  • What are some common clauses used with SELECT query in SQL?
  • What are UNION, MINUS and INTERSECT commands?
  • What is Cursor? How to use a Cursor?
  • What are Entities and Relationships?
  • What is an Alias in SQL?
  • What is a View?
  • What is Normalization?
  • What is Denormalization?
  • What are the various forms of Normalization?
  • What are the TRUNCATE, DELETE and DROP statements?
  • What is the difference between DROP and TRUNCATE statements?
  • What is the difference between DELETE and TRUNCATE statements?
  • What are Aggregate and Scalar functions?
  • What is User-defined function? What are its various types?
  • What is OLTP?
  • What are the differences between OLTP and OLAP?
  • What is Collation? What are the different types of Collation Sensitivity?
  • What is a Stored Procedure?
  • What is a Recursive Stored Procedure?
  • How to create empty tables with the same structure as another table?
  • What is Pattern Matching in SQL?
  • What is PostgreSQL?
  • How do you define Indexes in PostgreSQL?
  • How will you change the datatype of a column?
  • What is the command used for creating a database in PostgreSQL?
  • How can we start, restart and stop the PostgreSQL server?
  • What are partitioned tables called in PostgreSQL?
  • Define tokens in PostgreSQL?
  • What is the importance of the TRUNCATE statement?
  • What is the capacity of a table in PostgreSQL?
  • What are string constants in PostgreSQL?
  • How can you get a list of all databases in PostgreSQL?
  • How can you delete a database in PostgreSQL?
  • What are ACID properties? Is PostgreSQL compliant with ACID?
  • Can you explain the architecture of PostgreSQL?
  • What do you understand by multi-version concurrency control?
  • What do you understand by command enable-debug?
  • How do you check the rows affected as part of previous transactions?
  • What can you tell about WAL (Write Ahead Logging)?
  • What is the main disadvantage of deleting data from an existing table using the DROP TABLE command?
  • How do you perform case-insensitive searches using regular expressions in PostgreSQL?
  • How will you take backup of the database in PostgreSQL?
  • Does PostgreSQL support full text search?
  • What are parallel queries in PostgreSQL

SQL Interview Questions For Freshers

  • Display employees who earn more than the lowest salary in department 30
  • Display ename,dname even if there no employees working in a particular department.
  • Find the 5 highest salary in sql
  • Display details of salesmen whose name contains character ‘e’ and whose location contains character ‘o’. Also display salesmen whose name contains
  • Character ‘a’ and whose location contains character ‘a’. Use union all clause for the same.
  • Write a sql query to report all the departments that have at least five workers
  • Write a query to retrieve departments who have less than 2 employees working in it.
  • Display job-wise highest salary only if the highest salary is more than rs1500
  • List all the employees whose commission is null and working as clerk
  • Create a sql query that displays the new salaries for employees in the company, taking into account a 20% salary increase to current salaries and
  • An additional bonus of 1000.
  • Find the maximum salary paid to a ‘clerk’
  • How to display employees whose salary is between 1500 and 2500 without using between operators
  • Employee with highest salary sql
  • How to find max salary in sql
  • Display the names of the employees who are working in the company for the past 5 years
  • Query to display employee name, job, hire date, employee number; for each employee with the employee number appearing first.
  • Write a query to display details of employees who are not getting commission from table empl
  • Create a sql query that selects all the records for accounting employees at the central branch and excludes all other employees.
  • To display the average salary of all employees, department wise
  • List all the employees whose name starts with ‘s’ or ‘a’
  • List the employees who joined after 2 years of first employee
  • Find the titles of workers that earn the highest salary. Output the highest-paid title or multiple titles that share the highest salary.
  • Display ename,job,sal and annual salary of all those employees whose annual salary is between 18000 and 36000?
  • Write an sql query to fetch departments along with the total salaries paid for each of them.
  • Display all the unique job groups in the descending order?
  • Employees earning more than their managers

SQL Interview Questions For 5 Years Experienced

  • Which statement shows the department id, minimum salary, and maximum salary paid in that department, only if the minimum salary is less than 5000 and
  • Maximum salary is more than 15000?
  • Waqtd ename , job if the job name ends with consonants by using substring.
  • Print the department name and average salary of each department.
  • You want to display the employee’s last name and date hired in year 2000 to 2006 whose salary is above 5000. Which sql statement give the required
  • Output?
  • Write a query to fetch employee names and salary records. Display the employee details even if the salary record is not present for the employee.
  • An employee is starting a job with an annual salary of $40,000. The employees annual salary will increase by 3% at the end of each year with
  • The company. What will the employees annual salary, in dollars, be at the
  • End of the second year with the company?
  • Display details employees whose name has ‘a’ as second last letter
  • Write a sql query to display employee name and company name

Practice SQL Interview Questions

  • Write a query to display ename and salary of all employee
  • Display the employee number and name of employee working as clerk and earning highest salary among clerks.
  • Which query will be employed when an administrator wants to see a list of employees who work in the sass.
  • Write a sql query to fetch the employees working as sales managers
  • List all employees who have a salary greater than the average salary of their department.
  • Waqtd ename and dname of the employees who are getting comm in dept 10 or 30
  • Select empno distinct ename from emp
  • First highest salary in sql
  • 2 highest salary in sql
  • Display name of employee who earned highest salary
  • Write a query to display job title, employee name, and the difference between salary of the employee and minimum salary for the job.
  • You need to display the last names of those employees who have the letter a as the second character in their names. Which sql statement displays
  • The required results?
  • Write a query to display details of the employee who was hired 2nd
  • You need to display each employee’s name in all uppercase letters. Which function should you use?
  • Write a query that prints a list of employee names having a salary greater than $2000 per month who have been employed for less than 36 months. Sort
  • Your result by descending order of salary. [table: emp]
  • Write an oracle sql query to display the employee name employee category and basic pay
  • Write an sql query to find employees who have the highest salary in each of the departments.
  • How to find highest salary in sql
  • Display the names of employees who are working in the company for the past 5 years.
  • What changes will you make in the following sql statement to calculate the annual salary and add a bonus of ₹100 in the annual salary? Select ename,
  • Sal, 12*(sal+100) from emp;
  • List all the employees who don’t have a reporting manager
  • Write a query to select all record from employees where last name in ‘blake’, ‘scott’, ‘king’ and ‘ford’.
  • Highest salary in each department sql
  • Write a query to display no of clerks working in each department
  • Write a query to display total salary needed to pay each job in employee table.
  • Update salary of all employees by 1000
  • List the employees who are hired after 82 and before 87
  • Write a query to get the job id and maximum salary of the employees where maximum salary is greater than or equal to $4000
  • The management wants to increase salary of all employees by 10%. Write a query to display empid, current salary, increased salary and incremented
  • Amount.
  • Display those employees who joined in the company in the month of dec
  • Waqtd number of employees hired into the same department on the same day
  • Sql query to find max salary from each department
  • 3 highest salary in sql
  • Write an sql query to display the total salary of each employee adding the salary with variable value
  • Waqtd number of employees working in each department except president
  • Write a query to find the sum minimum and maximum of the salaries of the employees
  • Display the details of the employees who joined the company before their managers joined the company.
  • Retrieve the names of all employees who work in the department that has the employee with the highest salary among all employees.
  • Calculate the total monthly salary of all employees
  • Display each name of the employee as “name” and annual salary as “annual salary”
  • List all the employees except those who are working in dept 10 & 20
  • List the job and total salary of employees jobwise, for jobs other than ‘president’ and display only those rows having total salary > 5000.
  • Write a query that selects only the names of employees who are not managers.
  • Query to pick the rows from the table to pick only the name of the employees who work in the hr department
  • To display a list of employees who are from the same department which sql query can be used
  • By using the group by clause, display the enames who belongs to deptno 10 along with average salary.
  • Sql if employee is manager show all employees else show salary
  • What is the sql command to display the date of the first employee that was hired?
  • Write an sql query to print the name of employees having the highest salary in each department.

SQL Questions For Practice

  • Write a query to display all records
  • Employee wise total sales of each month and year excluding the manager
  • For each employee display the salary and salary difference from the next higher salary in increasing order.
  • Write a query to get the average salary and number of employees working the department 90
  • Display the number of employees who are getting salary less than the blake’s manager
  • List all the employees whose name starts with ‘s’ or ‘a’ in sql
  • Write a query to print empname and total salary of employees
  • Write a sql query to print department name along with department wise average salary.
  • List the ename,job,sal of the employee who get minimum salary in the company.
  • Display all the employees whose name ends with ‘s’ without using like operator
  • Which of the following is the correct query that displays the maximum salary?
  • Write a query to display employee name and salary of those employees
  • Write a query to create a table employee with empno ename designation and salary
  • Write a mysql query to display the maximum salary of the employees based on the month that they were hired.
  • Display all the employees who are joined after blake
  • Which of the following queries will return the employees who earn less than the average salary?
  • Display the bdate of all employee s in the format ‘ddthmonthyyyy’.
  • Which select statement will return the last name and hire date of an employee and his/ her manager for employees that started in the company Before their managers?
  • Find sql query to find the employee id whose salary lies in the range of 5000 and 15000.
  • List all the employees who are reporting to jones manager
  • Display the last employee record with 25 hike in salary
  • Select top 5 salary in sql
  • List deptno & min(salary) departmentwise, only if min(sal) is greater than the min(sal) of deptno 20.
  • Which of the following query finds employees with an experience more than 5?
  • Display the manager who is having maximum number of employees working under him
  • Find the last names of all employees whose salaries are the same as the minimum salary for any department.
  • Which of the following queries will fetch department with the highest average salary from the given table?
  • What will be the sql query to delete the records of employees who get salary less than 5000
  • Write a query to display the department name, manager name, and city.
  • What would be the output of below sql statement select empname from employee order by 2 desc
  • Write a query to get total salary paid for each department
  • Select max salary from each department
  • Write an sql query to find the employees who earn more than their managers.

SQL Interview Questions For Data Analyst

  • Display department and month-wise maximum salary
  • Display the name of the employee who has got maximum bonus in sql
  • Select min(sal), max(sal), city from dept group by city; will display the minimum and maximum salary from each
  • Count the total salary deptno wise where more than 2 employees exist
  • Waqtd name of the employee hired before the last employee
  • Find the employees who have been hired in the last 3 months
  • Write a query to find where employee salary is more than average salary within each department
  • Display how many employees joined in each month of the current year.
  • Display the list of employees with the same salary
  • Sql query for average salary of each department
  • How to get highest salary in sql
  • Number of employees in oracle
  • Write a query to display the number of employees only if they are working as manager or analyst and their annual sal should end with a zero, in each department.
  • Write an sql query to fetch the names of workers who earn the highest salary.
  • Write a query to display total salary needed to pay employees working as clerk
  • Display last employee record according to empno department names of all employees
  • Query to display the job type with maximum and minimum employees
  • Create a report to display the last name and job title of all employees who do not have a manager.
  • Write a query to retrieve the email addresses of all the employees who have an office code of 6 or report to employees with employee number ‘1088’.
  • Arrange them in the reverse alphabetical order of their first names.
  • Display the details of those employees who have a manager working in the department that is us based. Also, display the output in ascending order of
  • Their ’employee_id’.
  • Identify a query to display the name of all employees who do not have a manager
  • Write a query to get the average salary for all departments employing more than 10 employees.
  • Find all employees who earn more than the average salary of all employees of their company
  • Display all the employee who are ‘salesman’s having ‘e’ as the last but one character in ename but salary having exactly 4 character
  • Which sql command will select the employee with the post starting with m
  • Find the employee names working in dallas location
  • Name of the employee and his location of all the employees.
  • Write a query to find all the employees whose salary is between 10000 to 30000
  • Select count of employees in each department
  • Waqtd the hiredate which are duplicated in emp table
  • What will the following statement display: select f_name, l_name, salary from employee_data where f_name like ‘%k%’;
  • To list all employees whose salary is greater than or equal to 2000.
  • Write a query to show duplicate data from managers table considering name and salary

SQL Server Interview Questions For 10 Years Experience

  • Waqtd the annual salary of the employee whose name is smith
  • Sql query to display the employees hired after 1981
  • Select id from company where employees 10000 order by asc
  • Employee designation and no of post count office wise not match
  • Which clause would you use in a select statement to limit the display to those employees whose salary is greater then 5000?

Basic SQL Questions Asked In the Interview With Answers

Here, we have listed some of the frequently asked SQL questions in interviews. So we argue to all Developers or testers to go through these common sql interview questions and answers.

Display employee number and total salary for each employee
Ans: Select Empno, Sal + Comm
From Emp;

Display the names of employees whose name starts with alphabet S
Ans: Select Ename
From Emp
Where Ename Like ‘s%’;

Display the names of employees whose names have the second alphabet A in their names.
Ans: Select Ename
From Emp
Where Ename Like ‘_s%’;

Display the employee name and department name for each employee
Ans: Select Ename, Dname
From Emp E, Dept D
Where E.deptno = D.deptno;

Display employee number, name and location of the department in which he is working
Ans: Select Empno, Ename, Loc
From Emp E, Dept D
Where E.detpno = D.deptno;

Display ename,dname even if there no employees working in a particular department(use outer join)
Ans: Select name, Dname
From Emp E, Dept D
Where E.deptno (+) = D.deptno;

select ename if ename exists more than once
Ans: Select Distinct(Ename)
From Emp E
Where Ename In (Select Ename From Emp Where E.empno<>empno);

Display the dept no with highest annual remuneration bill as compensation
Ans: Select Deptno, sum(Sal)
From Emp
Group By Deptno Having Sum(Sal)=(Select Max(Sum(Sal)) From Emp Group By Deptno);

List out the lowest-paid employees working for each manager, exclude any groups where minsal is less than 1000 sort the output by sal
Ans : Select E.ename,e.mgr,e.sal
From Emp E
Where Sal In (Select Min(Sal) From Emp Where Mgr=e.mgr) And
E.sal>1000 Order By Sal;

Display current date
Ans: Select Sysdate
From Dual;

Display various jobs along with the total salary for each of the job where the total salary is greater than 40000?
Ans: Select Job, sum(Sal)
From Emp
Group By Job Having Sum(Sal)>40000;

Display the name of an employee who earns the Highest Salary?
Ans: Select Ename, Sal
From Emp
Where Sal>=(Select Max(Sal) From Emp );

Display the employee number and name for an employee working as a clerk and earning the highest salary among the clerks?
Ans : select ename,empno
from emp
where sal=(select max(sal) from emp wherejob=’CLERK’) and job=’CLERK’ ;

Display the employee names who are Working in Chicago?
Ans: Select E.ename, D.loc
From Emp E, Dept D
Where E.deptno = D.deptno And D.loc= ‘Chicago’;

Display the job groups having Total Salary greater than the maximum salary for Managers?
Ans: Select Job, sum (Sal)
From Emp
Group By Job Having Sum(Sal) >( Select Max ( Sal) From Emp Where Job=’manager’);

Display the names of employees from department number 10 with a salary greater than that of ANY employee working in other departments?
Ans: Select Ename, Deptno
From Emp
Where Sal>any(Select Min(Sal) From Emp Where Deptno!=10 Group By Deptno) And Deptno=10 ;

Display the names of employees from department number 10 with a salary greater than that of ALL employee working in other departments?
Ans: Select Ename, Deptno
From Emp where Sal > All ( Select Max ( Sal) From Emp Where Deptno !=10 Group By Deptno) And Deptno=10 ;

Display the names of employees in Upper Case?
Ans: Select Upper ( Ename)
From Emp;

Display the names of employees in Lower Case?
Ans: Select Lower ( Ename)
From Emp;

Display the length of all the employee names?
Ans: Select Length ( Ename) From Emp;

Display the name of employee Concatenate with Employee Number?
Ans : Select Ename ||’ ‘|| Empno
From Emp;

Display the information from the employee table. Where ever the job Manager is found it should be displayed as Boss?
Ans : Select Ename, Replace ( Job, ‘manager’, ‘boss’)
From Emp;

Display those who are not managers?
Ans: Select Ename
From Emp
Where Job != ‘manager’;

Display the details of those employees who are in the sales department and grade is 3?
Ans: Select E.ename, D.dname, Grade
From Emp E, Dept D, Salgrade
Where E.deptno = D.deptno And Dname= ‘sales’ And Grade=3;

Display that department whose name starts with”S” while location name ends with “K”?
Ans: Select E.ename, D.loc
From Emp E, Dept D
Where D.loc Like(‘%k’) And Ename Like(‘s%’);

Display those employees whose manager name is Jones?
Ans: Select E.ename Superior, E1.ename Subordinate
From Emp E, E1
Where E.empno= E1.mgr And E.ename=’jones’;

Display those employees whose salary is more than 3000 after giving 20% increment?
Ans: Select Ename, Sal, ( Sal + ( Sal *0.20))
From Emp
Where ( Sal + ( Sal *0.20))>3000;

Display employee name, dept name, salary, and commission for that sal in between 2000 to 5000 while the location is Chicago?
Ans : Select E.ename, D.dname, E.sal, E.comm
From Emp E, Dept D
Where E.deptno = D.deptno And Sal Between 2000 And 5000;

Display those employees whose salary is greater than his manager’s salary?
Ans : Select E.ename, E.sal, E1.ename, E1.sal
From Emp E, E1
Where E.mgr = E1.empno And E.sal > E1.sal;

Display the grade and employees name for the deptno 10 or 30, but the grade is not 4 while joined the company before 31-DEC-82?
Ans : Select Ename,grade,deptno,sal
From Emp, salgrade
Where ( Grade, sal) In ( Select Grade, sal From Salgrade, emp Where Sal Between Losal And Hisal)
And Grade!=4 And Deptno In (10,30) And Hiredate<’31-Dec-82′;

Delete those employees who joined the company before 31-Dec-82 while their department Location is New York or Chicago?
Ans : Select E.ename,e.hiredate,d.loc
From Emp E,dept D
Where E.deptno=d.deptno And Hiredate<’31-dec-82′ And D.loc In(‘new York’,’chicago’);

List out all the employee names ,job,salary,grade and deptname for every one in a company except ‘CLERK’ . Sort on salary display the highest salary?
Ans : Select E.ename, E.job, E.sal, D.dname, Grade
From Emp E,salgrade, dept D
Where (E.deptno = D.deptno And E.sal Between Losal And Hisal )
Order By E.sal Desc;

Display employee name, sal, comm, and netpay for those employees whose netpay is greater than or equal to any other employee salary of the company?
Ans : Select Ename,sal,nvl(Comm,0),sal+nvl(Comm,0)
From Emp
Where Sal+nvl(Comm,0) >any (Select E.sal From Emp E );

Display those employees whose salary is less than his manager but more than the salary of other managers?
Ans : Select E.ename Sub,e.sal
From Emp E,e1,dept D
Where E.deptno=d.deptno And E.mgr=e1.empno And E.sal<e1.sal And E.sal >any (Select E2.sal From Emp E2, E,dept D1
Where E.mgr=e2.empno And D1.deptno=e.deptno);

Delete those records from emp table whose deptno not available in dept table?
Ans: Delete From Emp E
Where E.deptno Not In (Select Deptno From Dept);

Display those enames whose salary is out of grade available in a salgrade table?
Ans: Select Empno, sal
From Emp
Where Sal<(Select Min(Losal) From Salgrade )
Or Sal>(Select Max(Hisal) From Salgrade);

Display those employees whose salary is a unique value?
Ans: Select Ename, Sal
From Emp
Where Mod(Sal,2)!=0;

Display those employees who joined the company in Dec?
Ans: Select Empno,ename
From Emp
Where Trim(To_char(Hiredate,’mon’))=trim(‘dec’);

Delete those records where no of employees in a particular department is less than 3?
Ans: Delete From Emp
Where Deptno In (Select Deptno From Emp Group By Deptno Having Count (*) < 3 ;

Display empno,ename,deptno from emp table. Instead of display department numbers display the related department name(Use decode function)?
Ans : Select Empno,ename,deptno,decode (Deptno,10,’accounting’ ,20,’research’,30,’sales’,’operations’)
Dname From Emp;

Display the Second maximum salary from the table using subquery.
Ans: Select Max(Salary)
From Employee
Where Salary Not In (Select Max(Salary) From Employee);

Display the Second maximum salary from the table using a subquery.
Ans: Select Top 1 Salary From ( Select Top 2 Salary From Employees Order By Salary Desc) As Emp
Order By Salary Asc

Basic SQL Questions Asked In the Interview With Answers

Here, we have listed some of the frequently asked SQL questions in interviews. So we argue to all Developers or testers to go through these common sql interview questions and answers.

Display employee number and total salary for each employee
Ans: Select Empno, Sal + Comm
From Emp;

Display the names of employees whose name starts with alphabet S
Ans: Select Ename
From Emp
Where Ename Like ‘s%’;

Display the names of employees whose names have the second alphabet A in their names.
Ans: Select Ename
From Emp
Where Ename Like ‘_s%’;

Display the employee name and department name for each employee
Ans: Select Ename, Dname
From Emp E, Dept D
Where E.deptno = D.deptno;

Display employee number, name and location of the department in which he is working
Ans: Select Empno, Ename, Loc
From Emp E, Dept D
Where E.detpno = D.deptno;

Display ename,dname even if there no employees working in a particular department(use outer join)
Ans: Select name, Dname
From Emp E, Dept D
Where E.deptno (+) = D.deptno;

select ename if ename exists more than once
Ans: Select Distinct(Ename)
From Emp E
Where Ename In (Select Ename From Emp Where E.empno<>empno);

Display the dept no with highest annual remuneration bill as compensation
Ans: Select Deptno, sum(Sal)
From Emp
Group By Deptno Having Sum(Sal)=(Select Max(Sum(Sal)) From Emp Group By Deptno);

List out the lowest-paid employees working for each manager, exclude any groups where minsal is less than 1000 sort the output by sal
Ans : Select E.ename,e.mgr,e.sal
From Emp E
Where Sal In (Select Min(Sal) From Emp Where Mgr=e.mgr) And
E.sal>1000 Order By Sal;

Display current date
Ans: Select Sysdate
From Dual;

Display various jobs along with the total salary for each of the job where the total salary is greater than 40000?
Ans: Select Job, sum(Sal)
From Emp
Group By Job Having Sum(Sal)>40000;

Display the name of an employee who earns the Highest Salary?
Ans: Select Ename, Sal
From Emp
Where Sal>=(Select Max(Sal) From Emp );

Display the employee number and name for an employee working as a clerk and earning the highest salary among the clerks?
Ans : select ename,empno
from emp
where sal=(select max(sal) from emp wherejob=’CLERK’) and job=’CLERK’ ;

Display the employee names who are Working in Chicago?
Ans: Select E.ename, D.loc
From Emp E, Dept D
Where E.deptno = D.deptno And D.loc= ‘Chicago’;

Display the job groups having Total Salary greater than the maximum salary for Managers?
Ans: Select Job, sum (Sal)
From Emp
Group By Job Having Sum(Sal) >( Select Max ( Sal) From Emp Where Job=’manager’);

Display the names of employees from department number 10 with a salary greater than that of ANY employee working in other departments?
Ans: Select Ename, Deptno
From Emp
Where Sal>any(Select Min(Sal) From Emp Where Deptno!=10 Group By Deptno) And Deptno=10 ;

Display the names of employees from department number 10 with a salary greater than that of ALL employee working in other departments?
Ans: Select Ename, Deptno
From Emp where Sal > All ( Select Max ( Sal) From Emp Where Deptno !=10 Group By Deptno) And Deptno=10 ;

Display the names of employees in Upper Case?
Ans: Select Upper ( Ename)
From Emp;

Display the names of employees in Lower Case?
Ans: Select Lower ( Ename)
From Emp;

Display the length of all the employee names?
Ans: Select Length ( Ename) From Emp;

Display the name of employee Concatenate with Employee Number?
Ans : Select Ename ||’ ‘|| Empno
From Emp;

Display the information from the employee table. Where ever the job Manager is found it should be displayed as Boss?
Ans : Select Ename, Replace ( Job, ‘manager’, ‘boss’)
From Emp;

Display those who are not managers?
Ans: Select Ename
From Emp
Where Job != ‘manager’;

Display the details of those employees who are in the sales department and grade is 3?
Ans: Select E.ename, D.dname, Grade
From Emp E, Dept D, Salgrade
Where E.deptno = D.deptno And Dname= ‘sales’ And Grade=3;

Display that department whose name starts with”S” while location name ends with “K”?
Ans: Select E.ename, D.loc
From Emp E, Dept D
Where D.loc Like(‘%k’) And Ename Like(‘s%’);

Display those employees whose manager name is Jones?
Ans: Select E.ename Superior, E1.ename Subordinate
From Emp E, E1
Where E.empno= E1.mgr And E.ename=’jones’;

Display those employees whose salary is more than 3000 after giving 20% increment?
Ans: Select Ename, Sal, ( Sal + ( Sal *0.20))
From Emp
Where ( Sal + ( Sal *0.20))>3000;

Display employee name, dept name, salary, and commission for that sal in between 2000 to 5000 while the location is Chicago?
Ans : Select E.ename, D.dname, E.sal, E.comm
From Emp E, Dept D
Where E.deptno = D.deptno And Sal Between 2000 And 5000;

Display those employees whose salary is greater than his manager’s salary?
Ans : Select E.ename, E.sal, E1.ename, E1.sal
From Emp E, E1
Where E.mgr = E1.empno And E.sal > E1.sal;

Display the grade and employees name for the deptno 10 or 30, but the grade is not 4 while joined the company before 31-DEC-82?
Ans : Select Ename,grade,deptno,sal
From Emp, salgrade
Where ( Grade, sal) In ( Select Grade, sal From Salgrade, emp Where Sal Between Losal And Hisal)
And Grade!=4 And Deptno In (10,30) And Hiredate<’31-Dec-82′;

Delete those employees who joined the company before 31-Dec-82 while their department Location is New York or Chicago?
Ans : Select E.ename,e.hiredate,d.loc
From Emp E,dept D
Where E.deptno=d.deptno And Hiredate<’31-dec-82′ And D.loc In(‘new York’,’chicago’);

List out all the employee names ,job,salary,grade and deptname for every one in a company except ‘CLERK’ . Sort on salary display the highest salary?
Ans : Select E.ename, E.job, E.sal, D.dname, Grade
From Emp E,salgrade, dept D
Where (E.deptno = D.deptno And E.sal Between Losal And Hisal )
Order By E.sal Desc;

Display employee name, sal, comm, and netpay for those employees whose netpay is greater than or equal to any other employee salary of the company?
Ans : Select Ename,sal,nvl(Comm,0),sal+nvl(Comm,0)
From Emp
Where Sal+nvl(Comm,0) >any (Select E.sal From Emp E );

Display those employees whose salary is less than his manager but more than the salary of other managers?
Ans : Select E.ename Sub,e.sal
From Emp E,e1,dept D
Where E.deptno=d.deptno And E.mgr=e1.empno And E.sal<e1.sal And E.sal >any (Select E2.sal From Emp E2, E,dept D1
Where E.mgr=e2.empno And D1.deptno=e.deptno);

Delete those records from emp table whose deptno not available in dept table?
Ans: Delete From Emp E
Where E.deptno Not In (Select Deptno From Dept);

Display those enames whose salary is out of grade available in a salgrade table?
Ans: Select Empno, sal
From Emp
Where Sal<(Select Min(Losal) From Salgrade )
Or Sal>(Select Max(Hisal) From Salgrade);

Display those employees whose salary is a unique value?
Ans: Select Ename, Sal
From Emp
Where Mod(Sal,2)!=0;

Display those employees who joined the company in Dec?
Ans: Select Empno,ename
From Emp
Where Trim(To_char(Hiredate,’mon’))=trim(‘dec’);

Delete those records where no of employees in a particular department is less than 3?
Ans: Delete From Emp
Where Deptno In (Select Deptno From Emp Group By Deptno Having Count (*) < 3 ;

Display empno,ename,deptno from emp table. Instead of display department numbers display the related department name(Use decode function)?
Ans : Select Empno,ename,deptno,decode (Deptno,10,’accounting’ ,20,’research’,30,’sales’,’operations’)
Dname From Emp;

Display the Second maximum salary from the table using subquery.
Ans: Select Max(Salary)
From Employee
Where Salary Not In (Select Max(Salary) From Employee);

Display the Second maximum salary from the table using a subquery.
Ans: Select Top 1 Salary From ( Select Top 2 Salary From Employees Order By Salary Desc) As Emp
Order By Salary Asc

Conclusion:

It’s important to gain insight into essential SQL topics, and these interview questions about SQL servers can help. Practicing them before attending your next SQL server interview is highly recommended. If you have any questions or doubts, don’t hesitate to ask and post them in the comments section provided below.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment